DS Lab · Experiment 7
Queue using a linked list (pointers)
CStacks and queuesJNTUH R22 · CS306PC
Aim
To write a menu-driven C program that implements a queue using a singly linked list (pointers), with the operations enqueue (insert), dequeue (delete), peek and display, and reports underflow.
Algorithm
- Define a node with
dataandnext. Keep two pointers:front(first node) andrear(last node). Both areNULLwhen the queue is empty. - Enqueue x: allocate a new node with
next = NULL. If the queue is empty, make bothfrontandrearpoint to it. Otherwise setrear->next = newandrear = new. - Dequeue: if
frontisNULL, report underflow. Otherwise savefront, movefronttofront->next, print and free the saved node. IffrontbecameNULL, setrear = NULLtoo. - Peek: show
front->dataif the queue is not empty. - Display: follow
nextfromfrontto the end and print each node. - Repeat the menu until the user chooses Exit, then free every remaining node.
Program
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *front = NULL, *rear = NULL; /* both NULL: queue is empty */
void enqueue(int value) {
struct node *newnode = malloc(sizeof(struct node));
if (newnode == NULL) {
printf("Queue overflow (no memory left)\n");
return;
}
newnode->data = value;
newnode->next = NULL;
if (rear == NULL) { /* first node */
front = newnode;
rear = newnode;
} else {
rear->next = newnode;
rear = newnode;
}
printf("Inserted %d\n", value);
}
void dequeue(void) {
if (front == NULL) {
printf("Queue underflow, nothing to delete\n");
return;
}
struct node *temp = front;
front = front->next;
if (front == NULL) { /* queue became empty */
rear = NULL;
}
printf("Deleted %d\n", temp->data);
free(temp);
}
void peek(void) {
if (front == NULL) {
printf("Queue is empty\n");
} else {
printf("Front element is %d\n", front->data);
}
}
void display(void) {
if (front == NULL) {
printf("Queue is empty\n");
return;
}
printf("Queue (front to rear): ");
for (struct node *temp = front; temp != NULL; temp = temp->next) {
printf("%d ", temp->data);
}
printf("\n");
}
void free_queue(void) {
while (front != NULL) {
struct node *temp = front;
front = front->next;
free(temp);
}
rear = NULL;
}
int main(void) {
int choice, value;
printf("QUEUE USING LINKED LIST\n");
printf("1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. Exit\n");
do {
printf("\nEnter your choice: ");
if (scanf("%d", &choice) != 1) {
printf("Invalid input\n");
break;
}
switch (choice) {
case 1:
printf("Enter value to insert: ");
if (scanf("%d", &value) == 1) {
enqueue(value);
}
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);
free_queue();
printf("All nodes freed. Exiting.\n");
return 0;
}Output
QUEUE USING LINKED LIST
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit
Enter your choice: 2
Queue underflow, nothing to delete
Enter your choice: 1
Enter value to insert: 10
Inserted 10
Enter your choice: 1
Enter value to insert: 20
Inserted 20
Enter your choice: 1
Enter value to insert: 30
Inserted 30
Enter your choice: 4
Queue (front to rear): 10 20 30
Enter your choice: 2
Deleted 10
Enter your choice: 3
Front element is 20
Enter your choice: 1
Enter value to insert: 40
Inserted 40
Enter your choice: 4
Queue (front to rear): 20 30 40
Enter your choice: 5
All nodes freed. Exiting.Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
Keeping a rear pointer makes enqueue O(1): without it, every insertion would have to walk to the end of the list. Dequeue removes the head, which is also O(1).
There is no fixed size, so overflow can only happen when malloc returns NULL. The nodes still in the queue at Exit are freed by free_queue.
Common mistakes
Forgetting to set
rear = NULLwhen the last node is dequeued.rearthen points to freed memory, and the next enqueue writes into it.Inserting at the front and deleting at the rear. Deleting the last node of a singly linked list needs the node before it, which takes O(n) time.
Not setting
newnode->next = NULL, so the last node points to garbage and display runs off into random memory.Forgetting to free the nodes that remain when the program ends.
Viva questions
Try to answer each one aloud before you open it.
Why do we keep both front and rear pointers?
frontlets us delete in O(1) andrearlets us insert in O(1). With only one pointer, one of the two operations would need a walk through the whole list.How do you know the queue is empty?
frontisNULL. In this programrearis also set toNULLwhenever that happens.What are the advantages over the array queue?
There is no fixed size, and there is no wasted space at the front after deletions, so it never shows false overflow.
What is a deque?
A double-ended queue, where elements can be inserted and deleted at both the front and the rear.