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
Intuition
In a ring of people holding hands, if you know who joined last, the person whose hand they hold on the other side is the one who has been there longest. One person tells you both ends of the line.
A circular linked queue holding 1, 2, 3 Copy +-------------------------------------------+
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) O ( 1 ) and use a single pointer.
Example 5.8 A circular linked queue with one pointer
Goal. Add 1, 2, 3; delete one; add 4; empty the queue, printing rear->next to show it is the front.
Program.
C · cq_linked.c Copy #include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node * next;
} Node;
/* The queue is a circular list reached through its REAR node;
rear->next is the front node. One pointer gives both ends. */
int add(Node ** rear, int value)
{
Node * n = malloc( sizeof * n);
if (n == NULL )
return 0 ;
n->value = value;
if ( * rear == NULL )
n->next = n; /* the only node points to itself */
else {
n->next = ( * rear)->next; /* new node -> front */
( * rear)->next = n; /* old rear -> new node */
}
* rear = n; /* new node is the rear */
return 1 ;
}
int delete_front(Node ** rear, int * value)
{
if ( * rear == NULL )
return 0 ;
Node * front = ( * rear)->next;
* value = front->value;
if (front == * rear) /* it was the only node */
* rear = NULL ;
else
( * rear)->next = front->next; /* rear now points to the 2nd node */
free(front);
return 1 ;
}
void show( const Node * rear)
{
if (rear == NULL ) {
printf( "queue empty \n " );
return ;
}
const Node * p = rear->next;
printf( "front ->" );
do {
printf( " %d" , p->value);
p = p->next;
} while (p != rear->next);
printf( " <- rear (rear->next is %d) \n " , rear->next->value);
}
int main( void )
{
Node * rear = NULL ;
int v;
add( & rear, 1 );
add( & rear, 2 );
add( & rear, 3 );
show(rear);
delete_front( & rear, & v);
printf( "deleted %d \n " , v);
add( & rear, 4 );
show(rear);
while (delete_front( & rear, & v))
printf( "deleted %d \n " , v);
show(rear);
return 0 ;
}
Run it.
Output Copy front -> 1 2 3 <- rear (rear->next is 1)
deleted 1
front -> 2 3 4 <- rear (rear->next is 2)
deleted 2
deleted 3
deleted 4
queue empty
Result. rear->next always gives the current front, items leave in FIFO order, and deleting the last node sets rear to NULL.
Correction · Program 5.7 (linked circular queue)
Study material says: Program 5.7 (§5.5.2) declares the link field as int *next, writes typedef struct cq *cqptr without a semicolon, assigns to the type name (cqptr = front = rear = NULL), uses queueptr and q without declaring them, and in add sets new->next = NULL and links rear->next = new, exactly as in the non-circular queue.
Correct version: The link must be struct cq *next, the typedef needs a semicolon, and variables (not the type) are initialised. For a circular queue, the last node must point to the first: on add, n->next = rear->next; rear->next = n; rear = n; (or n->next = n for the first node), and on delete the rear node’s link must skip the deleted front node.
Why: These declaration and syntax errors stop the program from compiling. Even with them fixed, setting next = NULL produces an ordinary (non-circular) linked queue, so the program does not implement a circular queue at all.
Key idea
In a circular linked queue the rear node’s link points to the front node, so one pointer (rear) gives O ( 1 ) O(1) O ( 1 ) access to both ends.
TEE Exam note
For “linked list implementation of a circular queue”: draw the ring of nodes with rear marked and rear->next as the front, write add (with the empty case n->next = n) and delete (with the one-node case), and state that both are O ( 1 ) O(1) O ( 1 ) with a single pointer.
Common mistake
Keeping a pointer to the front node only. Then adding at the rear requires walking round the whole ring, which is O ( n ) O(n) O ( n ) ; keep the rear pointer instead.
Common mistake
Forgetting the case of a single node. With one node, rear->next == rear; deleting it must set rear = NULL, not rear->next = front->next.
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?
A rearB rear->nextC rear->next->nextD The node before rear
Check answer
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?
A rear == n and n->next == nB rear == NULLC n->next == NULLD rear->next == NULL
Check answer
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 Copy #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 ;
} A front=7 rear=9 next=8 B front=8 rear=9 next=7 C front=9 rear=9 next=8 D front=8 rear=9 next=9
Check answer
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?
Check answer
Show answer Answer: B. Only one pointer (rear) is needed to reach both ends.
Both kinds have O ( 1 ) 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?
A rear->next = n; n->next = rear->next; rear = n;B n->next = NULL; rear->next = n; rear = n;C n->next = rear; rear = n;D n->next = rear->next; rear->next = n; rear = n;
Check answer
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) O ( 1 ) .