i am getting an error in this.
basically it sorts a queue in abc order.
public class Queue2{
public static void main(String [] args){
LinkedQueue q = new LinkedQueue();
q.enqueue("d");
q.enqueue("v");
q.enqueue("b");
q.enqueue("a");
q.enqueue("d");
System.out.println(q);
System.out.println("----------------");
sort(q);
}
public static void sort(LinkedQueue q){
LinkedQueue qFinal = new LinkedQueue();
int count = 0;
String pivot = "";
boolean sorted = false;
int count3 = 0;
while(sorted == false || ! q.empty()){
pivot = q.dequeue();
if(pivot.compareTo(q.peek()) > 0){
q.enqueue(pivot);
count3++;
}
else if(pivot.compareTo(q.peek()) < 0){
qFinal.enqueue(pivot);
count++;
if(count == 4){
qFinal.enqueue(q.peek());
if(count3 == 0){
System.out.println(qFinal);
break;
}
else{
sort(qFinal);
}
}
}
}
}
}
public class LinkedQueue
{
private class Node
{
String value;
Node next;
Node(String val, Node n)
{
value = val;
next = n;
}
}
private Node front = null;
private Node rear = null;
public void enqueue(String s)
{
if (rear != null)
{
rear.next = new Node(s, null);
rear = rear.next;
}
else
{
rear = new Node(s, null);
front = rear;
}
}
public boolean empty()
{
return front == null;
}
public String peek()
{
if (empty())
throw new IllegalArgumentException();
else
return front.value;
}
public String dequeue()
{
if (empty())
throw new IllegalArgumentException();
else
{
String value = front.value;
front = front.next;
if (front == null) rear = null;
return value;
}
}
public String toString()
{
StringBuilder sBuilder = new StringBuilder();
// Walk down the list and append all values
Node p = front;
while (p != null)
{
sBuilder.append(p.value + " ");
p = p.next;
}
return sBuilder.toString();
}
}