Circular Queue Implementation in C Using Array (With INSERT, DELETE, DISPLAY)

A circular queue is an advanced form of a linear queue that overcomes its limitations using a circular approach. This article explains how to implement a circular queue in C using arrays with complete code and logic for INSERT, DELETE, and DISPLAY operations.


What is a Circular Queue?

In a circular queue, the last position is connected back to the first, forming a circle. This allows efficient use of storage by reusing freed space.

Unlike a simple linear queue, a circular queue does not suffer from the problem of wasted memory.


Circular Queue Operations

OperationDescription
INSERTAdds an element at the rear
DELETERemoves the front element
DISPLAYShows all elements in the circular order

C Program: Circular Queue Using Array

#include <stdio.h>
#define SIZE 5

int queue[SIZE];
int front = -1, rear = -1;

void insert(int value) {
if ((front == 0 && rear == SIZE - 1) || (rear == (front - 1 + SIZE) % SIZE)) {
printf("Queue Overflow\n");
return;
} else if (front == -1) { // First element
front = rear = 0;
} else if (rear == SIZE - 1 && front != 0) {
rear = 0;
} else {
rear++;
}

queue[rear] = value;
printf("%d inserted\n", value);
}

void delete() {
if (front == -1) {
printf("Queue Underflow\n");
return;
}

printf("%d deleted\n", queue[front]);

if (front == rear) {
front = rear = -1; // Queue is empty
} else if (front == SIZE - 1) {
front = 0;
} else {
front++;
}
}

void display() {
if (front == -1) {
printf("Queue is empty\n");
return;
}

printf("Queue elements: ");
if (rear >= front) {
for (int i = front; i <= rear; i++)
printf("%d ", queue[i]);
} else {
for (int i = front; i < SIZE; i++)
printf("%d ", queue[i]);
for (int i = 0; i <= rear; i++)
printf("%d ", queue[i]);
}
printf("\n");
}

int main() {
int choice, value;

while (1) {
printf("\n1. INSERT\n2. DELETE\n3. DISPLAY\n4. EXIT\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insert(value);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
return 0;
default:
printf("Invalid choice\n");
}
}
}

Output Example

1. INSERT
2. DELETE
3. DISPLAY
4. EXIT

Enter your choice: 1
Enter value to insert: 10
10 inserted

Enter your choice: 1
Enter value to insert: 20
20 inserted

Enter your choice: 3
Queue elements: 10 20

Enter your choice: 2
10 deleted

Enter your choice: 3
Queue elements: 20

Why Use a Circular Queue?

  • Efficient memory usage: Reuses empty slots.
  • Prevents overflow when elements are removed and space is available.
  • Best for buffered data processing, like printer queues, OS scheduling, etc.
Previous Article

Queue Implementation in C Using Array with INSERT, DELETE, DISPLAY Operations

Next Article

Menu-Driven Singly Linked List Program in C with 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 ✨