5.4Queue with a Circular Linked List
Dequeue: removing the node after rear
In the circular queue that keeps only rear, the front node is rear->next. dequeue takes this node out of the ring: rear skips it and points to the next one. In this lesson you see this linking, the special case where a single node leaves, and how memory is released in C.
- How the front node is taken out of the ring
- Why
rearis set to NULL when the single node leaves - Why dequeue never moves
rear
1Idea
First the code checks whether the queue is empty: if rear is NULL there is nothing to remove. The C code returns false, while Java and Python throw an error. If the queue is not empty, the front node is kept in a pointer called first and its value is read.
Then first is taken out of the ring. The rear node pointed to the front; now it must point to the node after the front: rear->next = first->next. So the ring skips first, and the new front is again found in one step through rear->next. rear itself does not change: nothing was added at the back.
In a one-node queue, first and rear are the same node. When it leaves, nobody is left in the ring; rear is set to NULL. Without this, rear points to a freed node and becomes a dangling pointer. In C, first is released with free at the end in both cases.
2Step by step
The drawing removes one element from a queue holding 10, 20 and 30. The front is on the left, the rear on the right. The arrow curving underneath is the rear node's next.
- Start.
rearpoints to 30; thenextof 30 goes back to 10 at the front. - The queue is not empty. The front node,
rear->next, is kept infirst. - The value is read: 10 will be returned.
firstandrearare not the same.rear->next = first->next: 30 now points to 20 and the ring skips 10.- In C,
firstis released withfree. In Java and Python the garbage collector does this. - Result: 20 and 30 remain in the ring. The front is
rear->next, that is 20. The function returns 10.
struct Node { int data; struct Node* next;}; struct Queue { struct Node* rear; // the front is rear->next}; bool dequeue(struct Queue* q, int* value) { if (q->rear == NULL) return false; // empty queue struct Node* first = q->rear->next; // the front node *value = first->data; if (first == q->rear) { // there was one node q->rear = NULL; } else { q->rear->next = first->next; // rear points to the new front } free(first); return true;}class Node { int data; Node next; Node(int data) { this.data = data; }} class CircularLinkedQueue { Node rear = null; // the front is rear.next int dequeue() { if (rear == null) throw new IllegalStateException("queue is empty"); Node first = rear.next; // the front node int value = first.data; if (first == rear) { // there was one node rear = null; } else { rear.next = first.next; // rear points to the new front } return value; }}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 dequeue(self): if self.rear is None: raise IndexError("queue is empty") first = self.rear.next # the front node value = first.data if first is self.rear: # there was one node self.rear = None else: self.rear.next = first.next # rear points to the new front return valueThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
The length of the queue does not matter: one check, one read and one link.
No extra room is needed; one node of memory is given back.
In the two-pointer linked-list version, dequeue moves front ahead. Here there is no front, so the same job is done by moving rear->next ahead; both are O(1).
4Common mistake
The most common mistake is skipping the one-node case:
q->rear->next = first->next; // one node: first points to itselffree(first); // rear still points to firstWith one node, first->next is first again, so the link changes nothing. The node is freed, but rear keeps pointing to it. The queue does not look empty; the next peek or dequeue reads freed memory. In Java and Python the program does not crash, but the same error happens: the removed element keeps coming back as if it were still in the queue.
5Try it
The ring holds 5, 9 and 12; rear points to 12. After dequeue, what are rear and rear->next?
The answer is A. Dequeue removes 5 at the front. rear does not change and stays on 12; rear->next skips 5 and points to 9. The new front is 9.
Show the answer
The answer is A. Dequeue removes 5 at the front. rear does not change and stays on 12; rear->next skips 5 and points to 9. The new front is 9.
When is the condition first == rear true in dequeue?
The answer is B. first is the front node and rear the rear node. If they are the same, the queue holds one node. The empty queue was already handled by the rear == NULL check.
Show the answer
The answer is B. first is the front node and rear the rear node. If they are the same, the queue holds one node. The empty queue was already handled by the rear == NULL check.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.