MCS-208 · Unit 5

Queues

Section 6 of 8

Block 2 · Unit 5Queues10 min · Section 6 of 8

5.6Circular Queues Using Linked Lists

Covers IGNOU MCS-208 §5.5.2

A linked queue normally needs two pointers, front and rear. If the list is made circular (the last node points back to the first), one pointer to the rear node is enough, because rear->next is the front.

One pointer for both ends

A circular linked queue holding 1, 2, 3
        +-------------------------------------------+
        v                                           |
     +---+---+      +---+---+      +---+---+        |
     | 1 | o-+----> | 2 | o-+----> | 3 | o-+--------+
     +---+---+      +---+---+      +---+---+
     (front)                        ^ rear
  • Empty queue: rear == NULL.
  • Add x: create node n. If the queue is empty, n->next = n. Otherwise n->next = rear->next (new node points to the front) and rear->next = n. Then rear = n.
  • Delete: front = rear->next. If front == rear (only one node), set rear = NULL; otherwise rear->next = front->next. Free front.

Both operations are O(1)O(1) and use a single pointer.

Key takeaways

  • Circular linked queue: last node links to the first; keep only rear.
  • Front = rear->next; empty when rear == NULL.
  • Add: link the new node between rear and the front, then move rear.
  • Delete: bypass the front node from rear; handle the one-node case.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In a non-empty circular linked queue with pointer rear, which node is deleted next?

Show answer

Answer: B. rear->next

The rear node links to the front node, the oldest item. rear itself is the newest item.

Question 2 · multiple choice

A circular linked queue holds one node n. What is true?

Show answer

Answer: A. rear == n and n->next == n

The only node is both front and rear, so it points to itself. NULL never appears inside a circular list.

Question 3 · multiple choice

What does this program print?

C · q5-cqlink.c
#include <stdio.h>

typedef struct node {
    int value;
    struct node *next;
} Node;

int main(void)
{
    Node a = {7, NULL}, b = {8, NULL}, c = {9, NULL};
    Node *rear = &a;
    a.next = &a;                                   /* queue: 7 */
    b.next = rear->next; rear->next = &b; rear = &b; /* add 8 */
    c.next = rear->next; rear->next = &c; rear = &c; /* add 9 */
    rear->next = rear->next->next;                 /* delete front */
    printf("front=%d rear=%d next=%d\n", rear->next->value, rear->value,
           rear->next->next->value);
    return 0;
}
Show answer

Answer: D. front=8 rear=9 next=9

After adding 8 and 9 the ring is 7 → 8 → 9 → 7. Deleting the front (7) makes 9 point to 8, so the ring is 8 → 9 → 8.

Question 4 · multiple choice

What is the main advantage of a circular linked queue over an ordinary linked queue?

Show answer

Answer: B. Only one pointer (rear) is needed to reach both ends.

Both kinds have O(1)O(1) operations; the circular form saves one pointer and keeps the structure compact.

Question 5 · multiple choice

Which statements add a new node n to a non-empty circular linked queue?

Show answer

Answer: D. n->next = rear->next; rear->next = n; rear = n;

The new node must first take over the link to the front. In the first option n->next ends up pointing to n itself; the second makes the list non-circular.

Question 6 · short answer

Write the delete operation for a circular linked queue that is accessed through a rear pointer.

Show worked answer

int delete_front(Node **rear, int *x) { if (*rear == NULL) return 0; /* queue empty */ Node *front = (*rear)->next; *x = front->value; if (front == *rear) *rear = NULL; /* it was the only node */ else (*rear)->next = front->next; /* skip the old front */ free(front); return 1; } The operation is O(1)O(1).

↑ ↓ move↵ openTab switch type