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

C Program for Circular Queue Operations

This document outlines a C program for managing a Circular Queue of characters using an array. It includes functions for inserting and deleting elements, handling overflow and underflow situations, and displaying the queue's status. The program features a menu-driven interface for user interaction.

Uploaded by

cpanusha2116
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)
8 views3 pages

C Program for Circular Queue Operations

This document outlines a C program for managing a Circular Queue of characters using an array. It includes functions for inserting and deleting elements, handling overflow and underflow situations, and displaying the queue's status. The program features a menu-driven interface for user interaction.

Uploaded by

cpanusha2116
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

Program 6:

Develop a menu driven Program in C for the following operations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit
Support the program with appropriate functions for each of the above operations

// Online C compiler to run C program online


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

#define MAX 5

char cq[MAX];
int front = -1, rear = -1;

void insert(char item);


void delete();
void display();

int main() {
int ch;
char item;

while(1) {
printf("\n\nMain Menu");
printf("\n1. Insertion");
printf("\n2. Deletion");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);

switch(ch) {
case 1:
printf("\nEnter the element to be inserted: ");
scanf(" %c", &item); // Notice the space before %c
insert(item);
break;

case 2:
delete();
break;

case 3:
display();
break;

case 4:
exit(0);

default:
printf("\nPlease enter a valid choice");
}
}
return 0;
}

void insert(char item) {


if ( (front == 0 && rear == MAX - 1) ||
(rear + 1) % MAX == front ) {

printf("\nCircular Queue Overflow");


return;
}

if (front == -1) { // first element


front = 0;
rear = 0;
} else {
rear = (rear + 1) % MAX;
}

cq[rear] = item;
printf("\nInserted: %c", item);
}

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

char item = cq[front];


printf("\nDeleted element: %c", item);
if (front == rear) { // only one element
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}

void display() {
if (front == -1) {
printf("\nCircular Queue Empty");
return;
}

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

int i = front;
while (1) {
printf(" %c", cq[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
}

You might also like