algoritmauzmanı
Sign in
TREN
5.4Queue with a Circular Linked List
Lesson 5.4.3

Dequeue: removing the node after rear

In the circular queue that keeps only , the front node is rear->next. 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.

What you will learn
  • How the front node is taken out of the ring
  • Why rear is set to NULL when the single node leaves
  • Why dequeue never moves rear
5 minTime O(1)Memory O(1)C · Java · Python

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 . Without this, rear points to a freed node and becomes a . In C, first is released with 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.

10?0x1a4020?0x1a9030?0x2c10rearfirst
  1. Start. rear points to 30; the next of 30 goes back to 10 at the front.
  2. The queue is not empty. The front node, rear->next, is kept in first.
  3. The value is read: 10 will be returned.
  4. first and rear are not the same. rear->next = first->next: 30 now points to 20 and the ring skips 10.
  5. In C, first is released with free. In Java and Python the garbage collector does this.
  6. 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 value

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1)

The length of the queue does not matter: one check, one read and one link.

Memory
O(1)

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 .

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 first

With 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 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?

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?

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.