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

Enqueue: adding after rear

In the circular queue that keeps only , places the new node in the ring between the rear and the front and moves rear to it. This is the same idea as inserting into a circular list. In this lesson you see the order of the three assignments and why the empty queue is handled on its own.

What you will learn
  • In what order the new node is linked into the ring
  • Why rear moves last
  • Why a single node points to itself in an empty queue
5 minTime O(1)Memory O(1)C · Java · Python

1Idea

In the ring the rear node's next points to the front. The new element goes to the back, that is, between the old rear and the front. First the new node points to the front: node->next = rear->next. Then the old rear points to the new node: rear->next = node. The ring is closed again.

The order matters. If you change rear->next first, you lose the address of the front node and the new node cannot point to it. The ring breaks. Last of all, rear moves to the new node; it is now the rear, and rear->next still gives the front.

If the queue is empty, rear is and there is no ring to link into. The new node forms a ring on its own: its next points to itself. So it is both the rear and the front, and rear->next == rear. The last line is the same in both cases: rear = node.

2Step by step

The drawing adds 30 to a queue holding 10 and 20. The front is on the left, the rear on the right. The arrow curving underneath is the rear node's next.

10?0x1a4020?0x1a9030?0x2c10rearnode
  1. Start. rear points to 20; the next of 20 goes back to 10 at the front.
  2. A new node is allocated and 30 is written. It is not in the ring yet.
  3. rear is not NULL, so the queue is not empty. The else branch runs.
  4. node->next = rear->next: 30 now points to 10 at the front. For now two nodes point to 10.
  5. rear->next = node: the old rear, 20, now points to 30. The ring is 10, 20, 30 and back to 10.
  6. rear moves to the new node. The front is still rear->next, that is 10.
  7. Result: 10 at the front, 30 at the rear. The function returns true.

struct Node {    int data;    struct Node* next;}; struct Queue {    struct Node* rear;   // the front is rear->next}; bool enqueue(struct Queue* q, int value) {    struct Node* node = malloc(sizeof(struct Node));    if (node == NULL) return false;   // out of memory    node->data = value;    if (q->rear == NULL) {            // empty queue        node->next = node;            // one node points to itself    } else {        node->next = q->rear->next;   // new node points to the front        q->rear->next = node;         // old rear points to the new one    }    q->rear = node;    return true;}
class Node {    int data;    Node next;    Node(int data) { this.data = data; }} class CircularLinkedQueue {    Node rear = null;    // the front is rear.next     void enqueue(int value) {        Node node = new Node(value);        if (rear == null) {          // empty queue            node.next = node;        // one node points to itself        } else {            node.next = rear.next;   // new node points to the front            rear.next = node;        // old rear points to the new one        }        rear = node;    }}
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 enqueue(self, value):        node = Node(value)        if self.rear is None:           # empty queue            node.next = node            # one node points to itself        else:            node.next = self.rear.next  # new node points to the front            self.rear.next = node       # old rear points to the new one        self.rear = node

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 allocation and three assignments.

Memory
O(1)

Each enqueue allocates one node; the operation itself uses no extra room.

Thanks to the ring, the front is reachable without front, so enqueue is as fast as in the two-pointer linked-list version.

4Common mistake

The most common mistake is swapping the two linking lines:

q->rear->next = node;         // the front's address is lostnode->next = q->rear->next;   // node points to itself

After the first line rear->next is already the new node. The second line links the new node to itself; no path to the front node is left. The ring breaks, and after rear moves, rear->next gives the new node: the queue looks as if it held only the last element, and the other nodes are lost.

5Try it

After enqueue(8) on an empty queue, what does rear->next point to?

Show the answer

The answer is B. In the empty-queue branch the new node points to itself. In a one-element queue the front and the rear are the same node, so rear->next == rear.

How many pointer assignments does enqueue make when the queue is not empty?

Show the answer

The answer is B. The new node is linked to the front, the old rear is linked to the new node and rear moves. Whatever the number of nodes, these three assignments are enough.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.