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

A queue with one pointer

The linked-list queue keeps two pointers: and . 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 .

What you will learn
  • Why the front node in a circular list is rear->next
  • Why rear is kept and not front
  • What the empty queue and the one-node queue look like
5 minTime O(1)Memory O(n)C · Java · Python

1Idea

In a circular linked list the last node's next is not ; 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 the ring from start to end, and becomes . 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. 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.

10?0x1a4020?0x1a9030?0x2c10rear = NULLfront
  1. create sets rear to NULL. The queue is empty; there is no other pointer.
  2. isEmpty looks at rear. It is NULL, so the queue is empty: it returns true.
  3. A three-element queue. rear points to 30; the next of 30 goes back to 10.
  4. peek first checks for an empty queue. Then it goes to rear->next: the front node is 10.
  5. Peek returns 10. No pointer changed.
  6. A one-element queue: the node points to itself. rear and rear->next are the same node; peek still reads rear->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 rear

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

3Complexity

Time
O(1)

create, isEmpty and peek do one check and at most two reads.

Memory
O(n)

The queue holds n nodes. Compared with the two-pointer version it takes one pointer less per queue.

Enqueue and dequeue stay 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 front

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

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?

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.