5.4Queue with a Circular Linked List
A queue with one pointer
The linked-list queue keeps two pointers: front and rear. If the list is circular, one of them is enough. Since the last node's next points to the first node, the front node is always rear->next. In this lesson you see the structure, the empty queue, isEmpty and peek.
- Why the front node in a circular list is
rear->next - Why
rearis kept and notfront - What the empty queue and the one-node queue look like
1Idea
In a circular linked list the last node's next is not NULL; it points to the first node. In a queue the first node is the front element and the last node is the rear element. So whoever knows rear reaches the front in one step too: rear->next. A separate front pointer is not needed.
So why rear and not front? If you kept only front, you would reach the front at once, but to add at the end you would have to find the last node. For that you traverse the ring from start to end, and enqueue becomes O(n). With rear you reach both ends in one step: the rear is rear, the front is rear->next.
In an empty queue rear is NULL; isEmpty only checks that. In a one-element queue the node is both the front and the rear, and its next points to itself: rear->next == rear. peek reads rear->next->data after the empty-queue check.
This structure keeps one pointer instead of two. The saving is small: one pointer per queue. Its real value is that it shows a use of the circular list. The same idea shows up in the round-robin scheduling of operating systems.
2Step by step
The drawing shows the empty queue first, then a queue holding 10, 20 and 30, and finally a one-element queue. The front is on the left, the rear on the right. The arrow curving underneath is the last node's next.
createsetsrearto NULL. The queue is empty; there is no other pointer.isEmptylooks atrear. It is NULL, so the queue is empty: it returns true.- A three-element queue.
rearpoints to 30; thenextof 30 goes back to 10. peekfirst checks for an empty queue. Then it goes torear->next: the front node is 10.- Peek returns 10. No pointer changed.
- A one-element queue: the node points to itself.
rearandrear->nextare the same node; peek still readsrear->next->data.
struct Node { int data; struct Node* next;}; struct Queue { struct Node* rear; // last node; the front is rear->next}; void create(struct Queue* q) { q->rear = NULL;} bool isEmpty(struct Queue* q) { return q->rear == NULL;} bool peek(struct Queue* q, int* value) { if (q->rear == NULL) return false; // empty queue *value = q->rear->next->data; // the one after rear return true;}class Node { int data; Node next; Node(int data) { this.data = data; }} class CircularLinkedQueue { Node rear = null; // the front is rear.next boolean isEmpty() { return rear == null; } int peek() { if (rear == null) throw new IllegalStateException("queue is empty"); return rear.next.data; // the one after rear }}class Node: def __init__(self, data): self.data = data self.next = None class CircularLinkedQueue: def __init__(self): self.rear = None # the front is rear.next def is_empty(self): return self.rear is None def peek(self): if self.rear is None: raise IndexError("queue is empty") return self.rear.next.data # the one after rearThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
create, isEmpty and peek do one check and at most two reads.
The queue holds n nodes. Compared with the two-pointer version it takes one pointer less per queue.
Enqueue and dequeue stay O(1) in this structure too: the lessons on enqueue and dequeue show how they work with rear alone.
4Common mistake
The most common mistake is using rear to read the front:
*value = q->rear->data; // reads the rear, not the frontrear points to the element that came last. The front element of the queue is the one after rear: rear->next. This mistake does not show in a one-element queue, because there both are the same node; once there are two elements, peek returns the wrong value.
5Try it
In a circular linked-list queue, rear points to 40 and rear->next points to 15. What does peek return?
The answer is B. The front element is the node after rear. Since rear->next points to 15, peek returns 15; 40 is the element that came last.
Show the answer
The answer is B. The front element is the node after rear. Since rear->next points to 15, peek returns 15; 40 is the element that came last.
Why does this structure not keep only front instead of rear?
The answer is B. From front you reach the front but not the rear. From rear you reach both in one step: the rear is rear, the front is rear->next.
Show the answer
The answer is B. From front you reach the front but not the rear. From rear you reach both in one step: the rear is rear, the front is rear->next.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.