C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Circular Queue Implementation using Array

C program for Circular Queue using Array, including enqueue, dequeue, peek, and display operations — an improved version of the linear queue that efficiently reuses memory.

C Program: Circular Queue Implementation using Array

C

#include <stdio.h>

#include <stdlib.h>

 

#define SIZE 5   // Define the maximum size of the circular queue

 

int queue[SIZE];

int front = -1;

int rear = -1;

 

// Function to check if the queue is full

int isFull() {

    return ((front == 0 && rear == SIZE - 1) || (rear + 1 == front));

}

 

// Function to check if the queue is empty

int isEmpty() {

    return (front == -1);

}

 

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

void enqueue(int value) {

    if (isFull()) {

        printf("\nQueue Overflow! Cannot insert %d\n", value);

        return;

    }

 

    if (front == -1)  // First element insertion

        front = 0;

 

    rear = (rear + 1) % SIZE;  // Circular increment

    queue[rear] = value;

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

}

 

// Function to delete (dequeue) an element from the circular queue

void dequeue() {

    if (isEmpty()) {

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

        return;

    }

 

    printf("\n%d dequeued from queue.\n", queue[front]);

 

    if (front == rear) {  // Only one element

        front = rear = -1;

    } else {

        front = (front + 1) % SIZE;  // Circular increment

    }

}

 

// Function to peek (view the front element)

void peek() {

    if (isEmpty()) {

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

        return;

    }

 

    printf("\nFront element is: %d\n", queue[front]);

}

 

// Function to display the circular queue

void display() {

    if (isEmpty()) {

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

        return;

    }

 

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

    int i = front;

    while (1) {

        printf("%d ", queue[i]);

        if (i == rear)

            break;

        i = (i + 1) % SIZE;

    }

    printf("\n");

}

 

// Main function

int main() {

    int choice, value;

 

    printf("=== CIRCULAR QUEUE IMPLEMENTATION USING ARRAY ===\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 :

=== CIRCULAR QUEUE IMPLEMENTATION USING ARRAY ===

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: 1
Enter value to enqueue: 30
30 enqueued to queue.

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

Enter your choice: 2
10 dequeued from queue.

Enter your choice: 1
Enter value to enqueue: 40
40 enqueued to queue.

Enter your choice: 4
Queue elements are:
20 30 40

Explanation

Operation

Description

Enqueue

Adds an element to the rear of the circular queue (wraps around when full).

Dequeue

Removes an element from the front (wraps to the next index).

Peek

Displays the element at the front without removing it.

Display

Shows all elements in the correct circular order.