asked 209k views
0 votes
Write a method for the Queue class in the queue.java program (Listing 4.4) that

displays the contents of the queue. Note that this does not mean simply
displaying the contents of the underlying array. You should show the queue
contents from the first item inserted to the last, without indicating to the
viewer whether the sequence is broken by wrapping around the end of the
array. Be careful that one item and no items display properly, no matter where
front and rear are.

Below is the program that needs to be modified in order to achieve the question's objective:

// queue.java
// demonstrates queue
// to run this program: C>java QueueApp
////////////////////////////////////////////////////////////////
class Queue
{
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;
//--------------------------------------------------------------
public Queue(int s) // constructor
{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
//--------------------------------------------------------------
public void insert(long j) // put item at rear of queue
{
if(rear == maxSize-1) // deal with wraparound
rear = -1;
queArray[++rear] = j; // increment rear and insert
nItems++; // one more item
}
//--------------------------------------------------------------
public long remove() // take item from front of queue
{
long temp = queArray[front++]; // get value and incr front
if(front == maxSize) // deal with wraparound
front = 0;
nItems--; // one less item
return temp;
}
//--------------------------------------------------------------
public long peekFront() // peek at front of queue
{

return queArray[front];
}
//--------------------------------------------------------------
public boolean isEmpty() // true if queue is empty
{
return (nItems==0);
}
//--------------------------------------------------------------
public boolean isFull() // true if queue is full
{
return (nItems==maxSize);
}
//--------------------------------------------------------------
public int size() // number of items in queue
{
return nItems;
}
//--------------------------------------------------------------
} // end class Queue
////////////////////////////////////////////////////////////////
class QueueApp
{
public static void main(String[] args)
{

1 Answer

4 votes

Final answer:

The displayQueue method iterates from the front to the rear of the queue and handles wrapping around the array, ensuring a correct display of all items in the proper order. It uses the modulus operator for seamless traversal and correctly manages empty or single-item queues.

Step-by-step explanation:

To add a method to the Queue class that displays the contents of the queue from the first item inserted to the last, regardless of whether the sequence is broken by wrapping around the end of the array, you can implement a new method named displayQueue. This method will manage the potential wraparound by iterating from the front to the rear index, taking into consideration the maximum size of the queue (maxSize). It will correctly handle the display for the cases where the queue is empty or contains one item.

Here is the displayQueue method you should add to the Queue class:

public void displayQueue() {
if(isEmpty()) {
System.out.println("Queue is empty");
} else {
System.out.print("Queue from front to rear: ");
int index = front;
for(int i = 0; i < nItems; i++) {
System.out.print(queArray[index] + " ");
index = (index + 1) % maxSize;
}
System.out.println();
}
}

The displayQueue method checks if the queue is empty before proceeding to print its contents. If not, it starts from the front index and prints elements in order until it reaches the rear, wrapping around the array by using the modulus operator.


answered
User Artyom Akselrod
by
8.4k points