0% found this document useful (0 votes)
8 views2 pages

Write a program to perform all operation incircular queue

The document provides a C program that implements a circular queue with operations to insert, delete, and display elements. It defines a maximum size for the queue and includes functions to check if the queue is full or empty, as well as to handle the insertion and deletion of elements. The main function allows users to interact with the queue through a menu-driven interface.

Uploaded by

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

Write a program to perform all operation incircular queue

The document provides a C program that implements a circular queue with operations to insert, delete, and display elements. It defines a maximum size for the queue and includes functions to check if the queue is full or empty, as well as to handle the insertion and deletion of elements. The main function allows users to interact with the queue through a menu-driven interface.

Uploaded by

Swatilagna sahu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a program to perform the following operations on the Circular QUEUE: Insert,

Delete and Display.

#include <stdio.h>
#include <stdlib.h>

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

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

// Function to check if the circular queue is full


int isFull() {
return (front == (rear + 1) % MAX);
}

// Function to check if the circular queue is empty


int isEmpty() {
return (front == -1);
}

// Function to insert an element into the circular queue


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

// Function to delete an element from the circular queue


void delete() {
if (isEmpty()) {
printf("Queue Underflow! Cannot delete any element.\n");
} else {
printf("Element %d deleted from the queue.\n", queue[front]);
if (front == rear) { // Queue is now empty after deleting the last element
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
}

// Function to display the elements of the circular queue


void display() {
if (isEmpty()) {
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, element;

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 the element to insert: ");
scanf("%d", &element);
insert(element);
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}

return 0;
}

You might also like