/* An instance of this class represents a queue. ** To do so, it uses a circular chain of Link1 nodes in which the 'next' ** field of the node that holds the item at the rear of the queue points ** to the node holding the item at the front of the queue. This applies ** even in the case that the queue has only one item on it, so that the ** two nodes just mentioned are one and the same node. (In that case, ** the node's 'next' field points to the node itself.) ** The 'rear' instance variable of this class points to the node that ** holds the item at the rear of the queue, except when the queue is ** empty, in which case 'rear' has value null. ** ** Authors: R. McCloskey and < STUDENTS' NAMES > ** Date: October 2025 */ public class QueueViaLink1Circ implements Queue { // instance variables // ------------------ private int numItems; // # of items in this queue private Link1 rear; // reference to Link1 node holding rear item, // or null if queue is empty // constructor // ----------- public QueueViaLink1Circ() { rear = null; numItems = 0; } // observers // --------- public int sizeOf() { return numItems; } public boolean isEmpty() { return numItems == 0; } public T frontOf() { return rear.next.item; // alternative: // Link1 front = rear.next; // return front.item; } @Override /* Returns a string containing the items in this queue, ** in order from front to rear, enclosed within a pair of ** square brackets. */ public String toString() { StringBuilder s = new StringBuilder(); s.append('['); if (!isEmpty()) { Link1 pntr = rear; // Here is one of the few occasions when a do-while loop // is superior to a while loop (because the loop guard should // not be tested until after the first loop iteration). do { pntr = pntr.next; s.append(' '); s.append(pntr.item); } while (pntr != rear); } s.append(" ]"); return s.toString(); } // mutators // -------- public void dequeue() { if (numItems == 1) { // equivalently: rear.next == rear rear = null; } else { // queue has at least two items in it // MISSING CODE } numItems--; } public void enqueue(T item) { if (rear == null) { // equivalently: numItems == 0 // Make a new rear node whose 'next' field points to the node itself rear = new Link1(item, null); rear.next = rear; } else { // queue has at least one item in it // MISSING CODE } numItems++; } }