**NEED JAVA HELP ASAP**

santana9

Banned
Jan 19, 2003
154
0
0
Hey guys this is probably really simple so here it is. I want to display the contents of a queue from rear to front. I can only get it to display from front to rear. Here is the code that I have written for the display:

class MyQueue
{
private int maxSize;
private long numberToInsert;
private long[] queArray;
private int front;
private int rear;
private int nItems;
//--------------------------------------------------------------
public MyQueue(int s) // constructor
{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}

public void display() // displays array contents
{

if (nItems==0) { //if queue is empty displays standard message
System.out.print("The queue, rear --> front: ");
}

else if (front < rear) { // if front is less then rear
System.out.print("The queue, rear --> front: ");
for(int j=front; j<=rear; j++) // goes through queue starting at the front until it reaches the rear
{
System.out.print(queArray[j] + " "); // display it
}
System.out.println("");
}


else if (front == rear) { // if front is at the same location as rear
System.out.print("The queue, rear --> front: ");
for (int j=front; j==rear; j++)
{
System.out.print(queArray[j]); // display it // prints out single value
}
System.out.println("");
}

else if (front > rear) { // if rear less then front
System.out.print("The queue, rear --> front: ");
for (int j=front; j<=maxSize-1; j++) // starts at front goes to end of queue
{
System.out.print(queArray[j] + " "); // displays those values
}
for (int j=0; j<=rear; j++) // starts at beginning of queue until reaching front
{
System.out.print(queArray[j] + " "); // displays those values
}
System.out.println("");
}

} // end display

Basically there are four cases. If the queue is empty, front < rear, front > rear, and front = rear. Please help for I am stuck and cant get it too display properly. If you would like the whole file just send me a pm.
 

mrwiseguy

Junior Member
Mar 6, 2003
13
0
0
It all depends on how you insert into the queue.
Usaually the queue is FIFO. This is what I assume here

first find the position of the first element inserted. (this in your case is given by rear).

Cases.

1. Rear < front ----> error queue empty
2. Rear=front ---> only one element (not exactly a special case)
3. Rear > front ..

x=rear;
while (x >= front)
{
SOP(queue[x])
--x;
}