C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Circular Queue Implementation using Linked List

Concept Overview

A Circular Queue is a linear data structure that follows the FIFO (First In, First Out) principle but connects the last node back to the first node to form a circle.
When implemented using a Linked List, it eliminates the problem of a full queue due to fixed size (as in arrays).

Operations

  1. Enqueue – Insert an element at the rear.
  2. Dequeue – Remove an element from the front.
  3. Display – Show all elements in the queue.
  4. Peek – View the element at the front without removing it.

 

C Program: Circular Queue Implementation using Linked List

Structure Definition

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node *next;

};

 

struct Node *front = NULL;

struct Node *rear = NULL;

Function Implementations

1. Enqueue Operation

 

void enqueue(int value) {

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

    if (!newNode) {

        printf("Memory allocation failed\n");

        return;

    }

    newNode->data = value;

    newNode->next = NULL;

 

    if (front == NULL) {

        front = rear = newNode;

        rear->next = front;  // Circular link

    } else {

        rear->next = newNode;

        rear = newNode;

        rear->next = front;  // Maintain circular connection

    }

    printf("%d enqueued successfully\n", value);

}

2. Dequeue Operation

 

void dequeue() {

    if (front == NULL) {

        printf("Queue is empty\n");

        return;

    }

 

    struct Node *temp = front;

 

    // Single element case

    if (front == rear) {

        front = rear = NULL;

    } else {

        front = front->next;

        rear->next = front;  // Maintain circular link

    }

 

    printf("Dequeued element: %d\n", temp->data);

    free(temp);

}

3. Peek Operation

 

void peek() {

    if (front == NULL) {

        printf("Queue is empty\n");

    } else {

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

    }

}

4. Display Operation

 

void display() {

    if (front == NULL) {

        printf("Queue is empty\n");

        return;

    }

 

    struct Node *temp = front;

    printf("Circular Queue elements: ");

    do {

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

        temp = temp->next;

    } while (temp != front);

    printf("\n");

}

Example Usage (Main Function)

 

int main() {

    int choice, value;

 

    do {

        printf("\n--- Circular Queue using Linked List ---\n");

        printf("1. Enqueue\n2. Dequeue\n3. Peek\n4. Display\n5. 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("Exiting program...\n");

                break;

            default:

                printf("Invalid choice! Please try again.\n");

        }

    } while (choice != 5);

 

    return 0;

}

C

#include <stdio.h>

#include <stdlib.h>

 

// Node structure for Circular Queue

struct Node {

    int data;

    struct Node *next;

};

 

struct Node *front = NULL;

struct Node *rear = NULL;

 

// Function to insert an element in the queue

void enqueue(int value) {

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

    if (newNode == NULL) {

        printf("Memory allocation failed!\n");

        return;

    }

    newNode->data = value;

    newNode->next = NULL;

 

    if (front == NULL) {

        // First node creation

        front = rear = newNode;

        rear->next = front;  // Circular link

    } else {

        rear->next = newNode;

        rear = newNode;

        rear->next = front;  // Maintain circular connection

    }

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

}

 

// Function to delete an element from the queue

void dequeue() {

    if (front == NULL) {

        printf("Queue is empty. Dequeue not possible.\n");

        return;

    }

 

    struct Node *temp = front;

 

    if (front == rear) {

        // Only one element in queue

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

        front = rear = NULL;

    } else {

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

        front = front->next;

        rear->next = front;  // Maintain circular link

    }

 

    free(temp);

}

 

// Function to view the front element

void peek() {

    if (front == NULL) {

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

    } else {

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

    }

}

 

// Function to display all elements in the circular queue

void display() {

    if (front == NULL) {

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

        return;

    }

 

    struct Node *temp = front;

    printf("Circular Queue elements: ");

    do {

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

        temp = temp->next;

    } while (temp != front);

    printf("\n");

}

 

// Main function

int main() {

    int choice, value;

 

    while (1) {

        printf("\n--- Circular Queue using Linked List ---\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 the value to enqueue: ");

                scanf("%d", &value);

                enqueue(value);

                break;

            case 2:

                dequeue();

                break;

            case 3:

                peek();

                break;

            case 4:

                display();

                break;

            case 5:

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

                exit(0);

            default:

                printf("Invalid choice! Please try again.\n");

        }

    }

 

    return 0;

}

Output

 
OUTPUT :

--- Circular Queue using Linked List ---
1. Enqueue
2. Dequeue
3. Peek
4. Display
5. Exit

Enter your choice: 1
Enter the value to enqueue: 10
10 enqueued successfully.

Enter your choice: 1
Enter the value to enqueue: 20
20 enqueued successfully.

Enter your choice: 1
Enter the value to enqueue: 30
30 enqueued successfully.

Enter your choice: 4
Circular Queue elements: 10 20 30 

Enter your choice: 2
Dequeued element: 10

Enter your choice: 4
Circular Queue elements: 20 30 

Enter your choice: 3
Front element: 20

Enter your choice: 5
Exiting program...