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

C Program for Circular Queue Operations

Code for circular queues

Uploaded by

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

C Program for Circular Queue Operations

Code for circular queues

Uploaded by

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

Practical no:23

Title: write a c program to perform insert and delete operations on circular queue
using an array:
Code:
#include <stdio.h>
#include <stdlib.h>

#define SIZE 5 // Define the 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 == (rear + 1) % SIZE);
}

// Function to check if the queue is empty


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

// Function to insert an element into the circular queue


void enqueue(int element) {
if (isFull()) {
printf("Queue is full! Cannot insert %d.\n", element);
} else {
if (isEmpty()) {
front = 0;
}
rear = (rear + 1) % SIZE;
queue[rear] = element;
printf("Inserted %d into the queue.\n", element);
}
}

// Function to delete an element from the circular queue


void dequeue() {
if (isEmpty()) {
printf("Queue is empty! Cannot delete.\n");
} else {
printf("Deleted %d from the queue.\n", queue[front]);

if (front == rear) {
// Queue has only one element, so reset the queue
front = -1;
rear = -1;
} else {
front = (front + 1) % SIZE;
}
}
}

// Function to display the elements of the queue


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

// Main function
int main() {
int choice, value;

while (1) {
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter your 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:
exit(0);

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

return 0;
}
Output:

You might also like