Queue Implementation in C Using Linked List (With Source Code & Explanation)

A queue is a linear data structure that follows the FIFO (First In First Out) principle. In this tutorial, you’ll learn how to implement a queue using a linked list in C, including enqueue, dequeue, and display operations.


Why Use Linked List for Queue?

Queues implemented using arrays have a fixed size and can lead to overflow. By using a linked list, we eliminate this limitation, allowing the queue to grow dynamically based on memory availability.


Queue Operations

OperationDescription
ENQUEUEAdd an element at the rear of the queue
DEQUEUERemove an element from the front
DISPLAYShow all elements from front to rear

C Program: Queue Using Linked List

#include <stdio.h>
#include <stdlib.h>

// Node structure
struct Node {
int data;
struct Node* next;
};

struct Node* front = NULL;
struct Node* rear = NULL;

// ENQUEUE operation
void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}

printf("%d enqueued to queue\n", value);
}

// DEQUEUE operation
void dequeue() {
if (front == NULL) {
printf("Queue is empty\n");
return;
}

struct Node* temp = front;
printf("%d dequeued from queue\n", front->data);
front = front->next;
free(temp);

if (front == NULL) {
rear = NULL;
}
}

// DISPLAY operation
void display() {
if (front == NULL) {
printf("Queue is empty\n");
return;
}

struct Node* temp = front;
printf("Queue elements: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Main function
int main() {
int choice, value;

do {
printf("\nMenu:\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Try again.\n");
}
} while (choice != 4);

return 0;
}

Output Example

Menu:
1. Enqueue
2. Dequeue
3. Display
4. Exit

Enter your choice: 1
Enter value to enqueue: 10
10 enqueued to queue

Enter your choice: 1
Enter value to enqueue: 20
20 enqueued to queue

Enter your choice: 3
Queue elements: 10 -> 20 -> NULL

Enter your choice: 2
10 dequeued from queue

Enter your choice: 3
Queue elements: 20 -> NULL

Key Benefits of Linked List Queue

  • No fixed size
  • Dynamically grow and shrink
  • No wasted memory
  • Useful in real-time systems and process scheduling
Previous Article

Stack Implementation in C Using Linked List (With Code & Explanation)

Next Article

Doubly Linked List in C – Insert and Delete Operations

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨