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
Intuition
A conga line at a party: a new dancer holds on to the shoulders of the last dancer, and the dancer at the head leaves when the song ends. You only need to know who is first and who is last.
A linked queue holding 10, 20, 30 Copy 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) O ( 1 ) . The last special case matters: after deleting the only node, rear would otherwise still point to freed memory.
Example 5.4 A linked queue
Goal. Add 10, 20, 30; delete one; add 40; delete everything and check both pointers.
Program.
C · queue_linked.c Copy #include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node * next;
} Node;
typedef struct {
Node * front; /* first node: deletions happen here */
Node * rear; /* last node: additions happen here */
} Queue;
int add(Queue * q, int value) /* O(1) */
{
Node * n = malloc( sizeof * n);
if (n == NULL )
return 0 ; /* out of memory */
n->value = value;
n->next = NULL ;
if (q->rear == NULL ) /* empty queue: n is first and last */
q->front = q->rear = n;
else {
q->rear->next = n; /* link after the old last node */
q->rear = n;
}
return 1 ;
}
int delete_front(Queue * q, int * value) /* O(1) */
{
if (q->front == NULL )
return 0 ; /* queue empty */
Node * old = q->front;
* value = old->value;
q->front = old->next;
if (q->front == NULL ) /* the queue became empty */
q->rear = NULL ;
free(old);
return 1 ;
}
void show( const Queue * q)
{
printf( "front ->" );
for ( const Node * p = q->front; p != NULL ; p = p->next)
printf( " %d" , p->value);
printf( " <- rear \n " );
}
int main( void )
{
Queue q = { NULL , NULL };
int v;
add( & q, 10 );
add( & q, 20 );
add( & q, 30 );
show( & q);
delete_front( & q, & v);
printf( "deleted %d \n " , v);
add( & q, 40 );
show( & q);
while (delete_front( & q, & v))
printf( "deleted %d \n " , v);
printf( "front=%s rear=%s \n " , q.front ? "set" : "NULL" , q.rear ? "set" : "NULL" );
printf( "delete again: %s \n " , delete_front( & q, & v) ? "ok" : "queue empty" );
return 0 ;
}
Run it.
Output Copy front -> 10 20 30 <- rear
deleted 10
front -> 20 30 40 <- rear
deleted 20
deleted 30
deleted 40
front=NULL rear=NULL
delete again: queue empty
Result. Items leave in arrival order, and after the last deletion both front and rear are NULL, so the next delete correctly reports an empty queue.
Correction · Programs 5.2 and 5.3 (linked queue)
Study material says: Programs 5.2 and 5.3 (§5.3.2) use a node type struct queue with fields value and next, allocate it with malloc(sizeof(queue)), use variables front, rear and queueptr that are never declared, and in delete() write if (front == NULL) printf("Queue Empty"); followed by a block that runs in every case. delete() never returns the deleted value.
Correct version: Declare the node type (typedef struct node { int value; struct node *next; } Node;) and a queue structure holding front and rear; allocate with malloc(sizeof *n) and check the result; put the deletion code in an else branch (or return after the “empty” message); return a success flag and the value through a pointer, and set rear = NULL when the last node is deleted.
Why: queue on its own is not a type name in C (only struct queue is), so sizeof(queue) does not compile. Without else, the block after the printf runs even when front is NULL and then reads front->value, which is undefined behaviour (usually a crash).
Array queue versus linked queue
This answers Check Your Progress 1, Q3.
Array queue Linked queue Capacity Fixed (QUEUE_LENGTH) Limited only by memory Wasted space Linear version wastes freed cells; circular version wastes none No unused cells, but one pointer per item add / delete O ( 1 ) O(1) O ( 1 ) O ( 1 ) O(1) O ( 1 ) Overflow When the array is full Only when malloc fails Memory layout Contiguous Scattered 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 idea
A linked queue keeps front and rear pointers, adds after rear and deletes at front in O ( 1 ) O(1) O ( 1 ) ; the only subtle case is the empty queue, where both pointers must be NULL.
TEE Exam note
For “implement a queue using a linked list” draw the nodes with front and rear, write the add function (with the empty-queue case) and the delete function (with the “last node deleted → rear = NULL” case), and give the comparison table with the array queue.
Common mistake
Adding new nodes at the front of the list and deleting at the end. Deleting at the end of a singly linked list needs the node before the last one, which takes O ( n ) O(n) O ( n ) to find.
Common mistake
Forgetting to set rear = NULL after deleting the last node. The next add then links the new node to freed memory.
Key takeaways
Linked queue: front = first node (delete here), rear = last node (add here).
Add and delete are O ( 1 ) 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?
A Before front B After rear C In the middle D Before rear
Check answer
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?
A front = NULL, rear unchangedB Both still point to the node C Both NULL D rear = NULL, front unchanged
Check answer
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 Copy #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 ;
} A front=1 rear=3 second=2 B front=2 rear=2 second=3 C front=3 rear=3 second=2 D front=2 rear=3 second=3
Check answer
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?
Check answer
Show answer Answer: B. O ( 1 ) O(1) O ( 1 ) and O ( 1 ) O(1) O ( 1 )
With rear available, adding needs no traversal. Without a rear pointer, adding would be O ( n ) O(n) O ( n ) .
Question 5 · multiple choice When is a linked queue a better choice than an array queue?
A When the maximum size is small and known in advance B When memory for pointers must be saved C When the number of items varies widely and has no known upper limit D When random access to the middle item is needed
Check answer
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;
}