• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

**NEED JAVA HELP ASAP**

santana9

Banned
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.
 
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;
}

 
Back
Top