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

Circular Queue Implementation in C

The document contains a C program that implements a circular queue using an array. It provides functionalities to insert, delete, and print elements of the queue, along with checks for empty and full states. The program runs in a loop until the user chooses to exit.

Uploaded by

Prasad balkawade
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)
4 views3 pages

Circular Queue Implementation in C

The document contains a C program that implements a circular queue using an array. It provides functionalities to insert, delete, and print elements of the queue, along with checks for empty and full states. The program runs in a loop until the user chooses to exit.

Uploaded by

Prasad balkawade
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

CODE :

#include <stdio.h>
#define MAX 10

/*
Author: prasad balkawade
Title: A program for queue using an array (Circular)
*/

typedef struct Q {
int R, F;
int data[MAX];
} Q;

void initialise(Q *P);


int empty(Q *P);
int full(Q *P);
void enqueue(Q *P, int x);
int dequeue(Q *P);
void print(Q *P);

int main() {
Q q;
int op, x;

initialise(&q);

do {
printf("\n\n1) Insert\n2) Delete\n3) Print\n4) Quit");
printf("\nEnter Your Choice: ");
scanf("%d", &op);

switch (op) {
case 1:
printf("\nEnter a value: ");
scanf("%d", &x);
if (!full(&q))
enqueue(&q, x);
else
printf("\nQueue is full!");
break;

case 2:
if (!empty(&q)) {
x = dequeue(&q);
printf("\nDeleted Data = %d", x);
} else {
printf("\nQueue is empty!");
}
break;

case 3:
print(&q);
break;

case 4:
printf("\nExiting...");
break;

default:
printf("\nInvalid Choice!");
}
} while (op != 4);

return 0;
}

void initialise(Q *P) {


P->R = -1;
P->F = -1;
}

int empty(Q *P) {


return (P->R == -1);
}

int full(Q *P) {


return ((P->R + 1) % MAX == P->F);
}

void enqueue(Q *P, int x) {


if (P->R == -1) {
P->R = P->F = 0;
} else {
P->R = (P->R + 1) % MAX;
}
P->data[P->R] = x;
}

int dequeue(Q *P) {


int x = P->data[P->F];
if (P->R == P->F) {
P->R = P->F = -1;
} else {
P->F = (P->F + 1) % MAX;
}
return x;
}

void print(Q *P) {


int i;
if (empty(P)) {
printf("\nQueue is empty!");
return;
}

printf("\nQueue elements are:\n");


for (i = P->F; i != P->R; i = (i + 1) % MAX)
printf("%d\t", P->data[i]);

printf("%d\n", P->data[i]); // Print the last element


}
OUTPUT :

1) Insert
2) Delete
3) Print
4) Quit
Enter Your Choice: 1

Enter a value: 54

1) Insert
2) Delete
3) Print
4) Quit
Enter Your Choice: 1

Enter a value: 10

1) Insert
2) Delete
3) Print
4) Quit
Enter Your Choice: 2

Deleted Data = 54

1) Insert
2) Delete
3) Print
4) Quit
Enter Your Choice: 3

Queue elements are:


10

1) Insert
2) Delete
3) Print
4) Quit
Enter Your Choice: 4

Exiting...

=== Code Execution Successful ===

You might also like