[go: up one dir, main page]

0% found this document useful (0 votes)
10 views3 pages

Circular Queue

Uploaded by

sohailschool69
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)
10 views3 pages

Circular Queue

Uploaded by

sohailschool69
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
You are on page 1/ 3

ENQUQUE (INSERTION) and DEQUEUE (DELETION) in a circular queue.

#include <stdio.h>
#define MAX 5 // maximum size of the queue

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

// Function to insert (enqueue) an element


void enqueue(int value) {
if ((front == 0 && rear == MAX - 1) || (rear + 1) % MAX == front) {
printf("Queue Overflow! Cannot insert %d\n", value);
} else {
if (front == -1) { // first element
front = 0;
rear = 0;
} else {
rear = (rear + 1) % MAX;
}
queue[rear] = value;
printf("%d inserted into the queue.\n", value);
}
}

// Function to delete (dequeue) an element


void dequeue() {
if (front == -1) {
printf("Queue Underflow! Nothing to delete.\n");
} else {
printf("%d deleted from the queue.\n", queue[front]);
if (front == rear) {
// queue becomes empty
front = -1;
rear = -1;
} else {
front = (front + 1) % MAX;
}
}
}

// Function to display the queue


void display() {
if (front == -1) {
printf("Queue is empty.\n");
} else {
printf("Queue elements: ");
int i = front;
while (1) {
printf("%d ", queue[i]);
if (i == rear) break;
i = (i + 1) % MAX;
}
printf("\n");
}
}

int main() {
int choice, value;
do {
printf("\n--- Circular Queue Menu ---\n");
printf("1. Enqueue (Insert)\n");
printf("2. Dequeue (Delete)\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);

switch(choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
} while(choice != 4);

return 0;
}
INPUT/OUTPUT:
--- Circular Queue Menu ---
1. Enqueue (Insert)
2. Dequeue (Delete)
3. Display
4. Exit
Enter choice:

Case1: INSERTION
Enter choice: 1
10 inserted into the queue.
Enter choice: 1
20 inserted into the queue.
Enter choice: 1
30 inserted into the queue.

Enter choice: 3
Queue elements: 10 20 30

Case2: DELETION
Enter choice: 2

Enter choice: 3
Queue elements: 20 30

EXIT:
Exiting…

You might also like