0% found this document useful (0 votes)
3 views4 pages

Circular Queue Operations in C

The document contains a C program that implements a circular queue with basic operations such as insertion, deletion, and display. It includes a menu-driven interface allowing users to interact with the queue and demonstrates handling of overflow and underflow conditions. The program utilizes an array to store characters and manages the front and rear indices accordingly.

Uploaded by

Ms Anushree G
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)
3 views4 pages

Circular Queue Operations in C

The document contains a C program that implements a circular queue with basic operations such as insertion, deletion, and display. It includes a menu-driven interface allowing users to interact with the queue and demonstrates handling of overflow and underflow conditions. The program utilizes an array to store characters and manages the front and rear indices accordingly.

Uploaded by

Ms Anushree G
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

#include<stdio.

h>

#include<stdlib.h>

#define MAX 5

// Function prototypes

void cqInsert(char queue[], int *rear, int *front, int *count);

void cqDelete(char queue[], int *front, int *count);

void display(char queue[], int front, int count);

int main() {

char queue[MAX];

int front = 0, rear = -1, count = 0, ch;

while(1) {

// Display menu options

printf("\nMenu\n");

printf("1. Insert\n2. Delete\n3. Demonstrate Overflow and Underflow\n4. Display\n5. Exit\n");

scanf("%d", &ch);

switch (ch) {

case 1:

// Insert operation

cqInsert(queue, &rear, &front, &count);

break;

case 2:

// Delete operation

cqDelete(queue, &front, &count);

break;

case 3:

// Demonstrate Overflow and Underflow


if (count == MAX) {

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

} else if (count == 0) {

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

} else {

printf("Queue is neither Full nor Empty\n");

break;

case 4:

// Display operation

display(queue, front, count);

break;

case 5:

// Exit the program

exit(0);

default:

printf("Invalid choice, please try again.\n");

// Function to insert a character into the circular queue

void cqInsert(char queue[], int *rear, int *front, int *count) {

char ele;

printf("Enter a Character to Insert: ");

scanf(" %c", &ele);

// Check for overflow

if (*count == MAX) {

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


return;

// Increment rear circularly and insert the character

*rear = (*rear + 1) % MAX;

queue[*rear] = ele;

// Increment the count of elements in the queue

(*count)++;

// Function to delete a character from the circular queue

void cqDelete(char queue[], int *front, int *count) {

// Check for underflow

if (*count == 0) {

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

return;

// Print the character to be deleted from the front of the queue

printf("Deleted Character is %c\n", queue[*front]);

// Increment front circularly to delete the character

*front = (*front + 1) % MAX;

// Decrement the count of elements in the queue

(*count)--;

// Function to display all characters in the circular queue

void display(char queue[], int front, int count) {


int i;

if (count == 0) {

printf("Queue is Empty\n");

return;

printf("The Characters are: ");

for (i = 0; i < count; i++) {

printf("%c ", queue[(front + i) % MAX]);

printf("\n");

You might also like