DSA Assignment No.
Circular Linked Implementations of a Queue
This chain for our queue has two external references: one to the first node and one to the last
node in the chain. Recall that these references are particularly useful for a queue
implementation, since a queue’s operations affect both of its ends. Like the chains you have seen
before, the last node in this chain contains null. Such chains are sometimes called linear linked
chains, regardless of whether they have a tail reference in addition to a head reference.
In a circular linked chain, the last node references the first node, so no node contains null in its
next field. Despite the fact that each node references the next node, a circular linked chain has a
beginning and an end. We could have an external reference to the chain’s first node, but then a
traversal of the chain would be necessary to locate the last node. Having both a reference to the
first node and a reference to the last node is usually more than is necessary. Since the chain’s last
node references its first node, we can have a solitary reference to the last node and still locate
the first node quickly. Figure below illustrates such a chain.
When a class uses a circular linked chain to represent a queue, its only data field is the reference
lastNode to the chain’s last node. The implementation therefore does not have the overhead of
maintaining a data field that references the first node. Any time such a reference is needed, the
expression lastNode.getNextNode() provides it. Despite this simplification, this approach is not
necessarily better than the one we used. It is mostly just different.
Tasks
1. Write a class CircularLinkedQueue (using the approach discussed above) which
implements the QueueInterface (we used in our lectures).
Reference:
Carrano, Frank M, and Timothy M Henry. “Chapter 11: Queue, Deque, and Priority Queue
Implementations.” Data Structures and Abstractions with Java, 4th ed., Pearson, 2015, pp. 334–
335.