MCS-208 · Unit 5

Queues

Section 3 of 8

Block 2 · Unit 5Queues9 min · Section 3 of 8

5.3Queue Implementation Using Linked Lists

Covers IGNOU MCS-208 §5.3.2

A linked list gives a queue that never wastes cells and never fills up (until memory runs out). We keep two pointers: front to the first node, where deletions happen, and rear to the last node, where additions happen.

The representation

A linked queue holding 10, 20, 30
front                                   rear
  |                                      |
  v                                      v
+----+---+     +----+---+     +----+------+
| 10 | o-+---> | 20 | o-+---> | 30 | NULL |
+----+---+     +----+---+     +----+------+
  • Empty queue: front == NULL and rear == NULL.
  • Add: create a node with next = NULL; if the queue is empty, both front and rear point to it; otherwise rear->next = n; rear = n;.
  • Delete: save front, move front to front->next, free the old node; if front became NULL, also set rear = NULL.

Both operations are O(1)O(1). The last special case matters: after deleting the only node, rear would otherwise still point to freed memory.

Array queue versus linked queue

This answers Check Your Progress 1, Q3.

Array queueLinked queue
CapacityFixed (QUEUE_LENGTH)Limited only by memory
Wasted spaceLinear version wastes freed cells; circular version wastes noneNo unused cells, but one pointer per item
add / deleteO(1)O(1)O(1)O(1)
OverflowWhen the array is fullOnly when malloc fails
Memory layoutContiguousScattered nodes

The linked queue is better when the number of items varies a lot or has no known limit; a (circular) array queue is simpler and faster when a good maximum size is known.

Key takeaways

  • Linked queue: front = first node (delete here), rear = last node (add here).
  • Add and delete are O(1)O(1); the queue has no fixed capacity.
  • Empty queue: both pointers NULL; update both when the queue becomes empty or stops being empty.
  • Use else after an error message so the normal code does not run on an empty queue.

Practice questions

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

Question 1 · multiple choice

In a linked queue, where is a new node attached?

Show answer

Answer: B. After rear

New items join at the rear: rear->next = n; rear = n;. Deletion happens at front.

Question 2 · multiple choice

A linked queue holds exactly one node, which is deleted. What must front and rear be afterwards?

Show answer

Answer: C. Both NULL

The node was freed, so neither pointer may still point to it. Leaving rear unchanged is the classic bug.

Question 3 · multiple choice

What does this program print?

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

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

int main(void)
{
    Node a = {1, NULL}, b = {2, NULL}, c = {3, NULL};
    Node *front = &a, *rear = &a;
    rear->next = &b; rear = &b;        /* add 2 */
    front = front->next;               /* delete */
    rear->next = &c; rear = &c;        /* add 3 */
    printf("front=%d rear=%d second=%d\n", front->value, rear->value,
           front->next->value);
    return 0;
}
Show answer

Answer: D. front=2 rear=3 second=3

After adding 2 and deleting 1, the queue is [2]; adding 3 gives [2 3]. (A real delete would also free the node; here the nodes are local variables.)

Question 4 · multiple choice

What is the time complexity of add and delete in a linked queue with both front and rear pointers?

Show answer

Answer: B. O(1)O(1) and O(1)O(1)

With rear available, adding needs no traversal. Without a rear pointer, adding would be O(n)O(n).

Question 5 · multiple choice

When is a linked queue a better choice than an array queue?

Show answer

Answer: C. When the number of items varies widely and has no known upper limit

A linked queue allocates exactly what it needs. With a known small maximum, a circular array queue is simpler and saves the pointer memory.

Question 6 · short answer

Write C functions to add an item to and delete an item from a linked queue with front and rear pointers.

Show worked answer

typedef struct node { int value; struct node *next; } Node; Node *front = NULL, *rear = NULL; int add(int x) { Node *n = malloc(sizeof *n); if (n == NULL) return 0; n->value = x; n->next = NULL; if (rear == NULL) front = rear = n; else { rear->next = n; rear = n; } return 1; } int delete_front(int *x) { if (front == NULL) return 0; /* queue empty */ Node *t = front; *x = t->value; front = t->next; if (front == NULL) rear = NULL; /* queue became empty */ free(t); return 1; }

↑ ↓ move↵ openTab switch type