Explain queue by using linked list in C language

A queue is a linear data structure that follows the First In First Out (FIFO) principle. When implemented using linked lists, we can avoid queue overflow and underflow issues that occur with array-based implementations. The linked list provides dynamic memory allocation, allowing the queue to grow or shrink as needed.

Operations carried out on a queue using linked lists in C programming language are as follows −

  • Enqueue − Insert an element at the rear
  • Dequeue − Remove an element from the front
  • Display − Show all elements in the queue
  • Front − Get the front element without removing it

Syntax

struct node {
    int data;
    struct node *next;
};

struct node *front = NULL;
struct node *rear = NULL;

Enqueue Operation

The enqueue operation adds a new element at the rear of the queue −

void enqueue(int item) {
    struct node *newnode = (struct node*) malloc(sizeof(struct node));
    newnode->data = item;
    newnode->next = NULL;
    
    if (front == NULL && rear == NULL) {
        front = rear = newnode;
    } else {
        rear->next = newnode;
        rear = newnode;
    }
}

Dequeue Operation

The dequeue operation removes an element from the front of the queue −

void dequeue() {
    if (front == NULL) {
        printf("Queue is empty<br>");
    } else {
        struct node *temp = front;
        front = front->next;
        if (front == NULL) {
            rear = NULL;
        }
        free(temp);
    }
}

Complete Example

Following is the complete C program for queue implementation using linked lists −

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

struct node {
    int data;
    struct node *next;
};

struct node *front = NULL;
struct node *rear = NULL;

void enqueue(int item) {
    struct node *newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = item;
    newnode->next = NULL;
    
    if (front == NULL && rear == NULL) {
        front = rear = newnode;
    } else {
        rear->next = newnode;
        rear = newnode;
    }
    printf("Enqueued: %d<br>", item);
}

void dequeue() {
    if (front == NULL) {
        printf("Queue is empty<br>");
    } else {
        struct node *temp = front;
        printf("Dequeued: %d<br>", front->data);
        front = front->next;
        if (front == NULL) {
            rear = NULL;
        }
        free(temp);
    }
}

void display() {
    if (front == NULL) {
        printf("Queue is empty<br>");
    } else {
        struct node *temp = front;
        printf("Queue: ");
        while (temp != NULL) {
            printf("%d ", temp->data);
            temp = temp->next;
        }
        printf("<br>");
    }
}

int getFront() {
    if (front != NULL) {
        return front->data;
    }
    return -1;
}

int main() {
    enqueue(10);
    enqueue(20);
    enqueue(30);
    
    display();
    
    printf("Front element: %d<br>", getFront());
    
    dequeue();
    dequeue();
    
    display();
    
    dequeue();
    dequeue();
    
    return 0;
}
Enqueued: 10
Enqueued: 20
Enqueued: 30
Queue: 10 20 30 
Front element: 10
Dequeued: 10
Dequeued: 20
Queue: 30 
Dequeued: 30
Queue is empty

Advantages of Linked List Implementation

  • Dynamic Size − No fixed size limitation
  • No Memory Waste − Memory allocated only when needed
  • No Overflow − Queue can grow as long as memory is available

Conclusion

Implementing a queue using linked lists provides a flexible and efficient solution that eliminates the size constraints of array-based implementations. The dynamic memory allocation ensures optimal memory usage and prevents overflow conditions.

Updated on: 2026-03-15T14:01:50+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements