0% found this document useful (0 votes)
7 views5 pages

Circular Queue Implementation in C

A code of circular array .

Uploaded by

supriya302205
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Circular Queue Implementation in C

A code of circular array .

Uploaded by

supriya302205
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link] the program on Circular Queue ( using array) and its operations .

#include <stdio.h>

#define SIZE 5

int queue[SIZE];

int front = -1, rear = -1;

// Check if the queue is full

int isFull() {

return (front == (rear + 1) % SIZE);

// Check if the queue is empty

int isEmpty() {

return (front == -1);

// Enqueue operation

void enqueue(int value) {

if (isFull()) {

printf("Queue is Full!\n");

} else {

if (front == -1) {

front = 0;

rear = (rear + 1) % SIZE;

queue[rear] = value;

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

}
// Dequeue operation

int dequeue() {

int value;

if (isEmpty()) {

printf("Queue is Empty!\n");

return -1;

} else {

value = queue[front];

if (front == rear) {

front = rear = -1; // Reset the queue

} else {

front = (front + 1) % SIZE;

printf("%d dequeued from the queue.\n", value);

return value;

// Display the queue

void display() {

if (isEmpty()) {

printf("Queue is Empty!\n");

} else {

printf("Queue elements are: ");

for (int i = front; i != rear; i = (i + 1) % SIZE) {

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

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

}
int main() {

int choice, value;

do {

printf("\nCircular Queue Operations:\n");

printf("1. Enqueue\n2. Dequeue\n3. Display\n4. 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:

display();

break;

case 4:

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

break;

default:

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

} while (choice != 4);

return 0;

}
Output

Circular Queue Operations:

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice: 1

Enter the value to enqueue: 10

10 enqueued to the queue.

Circular Queue Operations:

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice: 1

Enter the value to enqueue: 20

20 enqueued to the queue.

Circular Queue Operations:

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice: 3

Queue elements are: 10 20

Circular Queue Operations:

1. Enqueue

2. Dequeue

3. Display
4. Exit

Enter your choice: 2

10 dequeued from the queue.

Circular Queue Operations:

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice: 3

Queue elements are: 20

Circular Queue Operations:

1. Enqueue

2. Dequeue

3. Display

4. Exit

Enter your choice: 4

Exiting...

You might also like