C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Queue Implementation using Linked List

C Program to implement a Queue using Linked List, including enqueue, dequeue, display, and peek operations — a complete dynamic implementation ideal for your e-learning platform.

C Program: Queue Implementation Using Linked List

C

#include <stdio.h>

#include <stdlib.h>

 

// Structure for queue node

struct Node {

    int data;

    struct Node *next;

};

 

// Front and rear pointers

struct Node *front = NULL;

struct Node *rear = NULL;

 

// Function to enqueue (insert) an element into the queue

void enqueue(int value) {

    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

    if (newNode == NULL) {

        printf("\nQueue Overflow! Memory not allocated.\n");

        return;

    }

    newNode->data = value;

    newNode->next = NULL;

 

    if (rear == NULL) {

        front = rear = newNode;

    } else {

        rear->next = newNode;

        rear = newNode;

    }

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

}

 

// Function to dequeue (remove) an element from the queue

void dequeue() {

    if (front == NULL) {

        printf("\nQueue Underflow! No elements to dequeue.\n");

        return;

    }

 

    struct Node *temp = front;

    printf("\n%d dequeued from queue.\n", front->data);

 

    front = front->next;

 

    // If queue becomes empty

    if (front == NULL)

        rear = NULL;

 

    free(temp);

}

 

// Function to peek (view) the front element

void peek() {

    if (front == NULL)

        printf("\nQueue is empty.\n");

    else

        printf("\nFront element is: %d\n", front->data);

}

 

// Function to display the queue

void display() {

    if (front == NULL) {

        printf("\nQueue is empty.\n");

        return;

    }

 

    struct Node *temp = front;

    printf("\nQueue elements are:\n");

    while (temp != NULL) {

        printf("%d ", temp->data);

        temp = temp->next;

    }

    printf("\n");

}

 

// Main function

int main() {

    int choice, value;

 

    printf("=== QUEUE IMPLEMENTATION USING LINKED LIST ===\n");

 

    while (1) {

        printf("\nMenu:\n");

        printf("1. Enqueue\n");

        printf("2. Dequeue\n");

        printf("3. Peek\n");

        printf("4. Display\n");

        printf("5. 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:

                peek();

                break;

            case 4:

                display();

                break;

            case 5:

                printf("\nExiting program.\n");

                exit(0);

            default:

                printf("\nInvalid choice! Try again.\n");

        }

    }

 

    return 0;

}

Output

 
OUTPUT :

=== QUEUE IMPLEMENTATION USING LINKED LIST ===

Menu:
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. 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: 4
Queue elements are:
10 20

Enter your choice: 2
10 dequeued from queue.

Enter your choice: 3
Front element is: 20

Explanation

Operation

Description

Enqueue

Adds a new node to the end of the queue.

Dequeue

Removes a node from the front of the queue.

Peek

Displays the value of the front node without removing it.

Display

Prints all elements from front to rear.