0% found this document useful (0 votes)
41 views8 pages

Program of Circular Queue

This program implements a circular queue data structure using C. It defines a queue structure with front and rear pointers and an array to store data. Functions are included to initialize the queue, check if it is empty or full, enqueue and dequeue elements, and print the queue contents. The main function demonstrates using the queue by providing a menu to insert, delete, print or quit and handling the different queue operations.

Uploaded by

AbhinavSingh
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)
41 views8 pages

Program of Circular Queue

This program implements a circular queue data structure using C. It defines a queue structure with front and rear pointers and an array to store data. Functions are included to initialize the queue, check if it is empty or full, enqueue and dequeue elements, and print the queue contents. The main function demonstrates using the queue by providing a menu to insert, delete, print or quit and handling the different queue operations.

Uploaded by

AbhinavSingh
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
You are on page 1/ 8

/*Program of Circular Queue*/

#include<stdio.h>
#include<conio.h>
# define MAX 5
typedef struct Q
{
int rear,front;
int data[MAX];
} Q;
void initialize(Q *P);
int empty (Q *P);
int full(Q *P);
void enqueue(Q *P,int x);
int delqueue(Q *P);
void print(Q *P);

void main()
{
Q q;
int op ,x;
initialize(&q);
clrscr();

do
{
printf("\n\n 1) Insert \n 2) Delete \n 3) Print \n 4)Quit \n");
printf("\n Enter your choice:");

scanf("%d",&op);

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

case 2 : if(!empty(&q))
{
x=delqueue(&q);
printf("\n Deleted Data =%d",x);
}
else
printf("\nCircular Queue is empty!!!");
break;

case 3 : print(&q);
break;
}
}
while(op!=4) ;
}

void initialize(Q *P)


{
P->rear=-1;
P->front=-1;
}
int empty(Q *P)
{
if (P->rear==-1)
return 1;
return 0;
}
int full(Q *P)
{
if((P->rear+1)%MAX==P->front)
return 1;
return 0;
}
void enqueue(Q *P, int x)
{
if(P->rear==-1)
{
P->rear=P->front=0;
P->data[P->rear]=x;
}
else
{
P->rear=(P->rear+1)%MAX;
P->data[P->rear]=x;

}
}
int delqueue(Q *P)
{
int x;
x=P->data[P->front];
if(P->rear==P->front)
{
P->rear=-1;
P->front=-1;
}
else
P->front=(P->front+1)%MAX;
return x;
}
void print(Q *P)
{
int i;
if(empty(P))
return ;
printf("\n");
for(i=P->front;i!=P->rear;i=(i+1)%MAX)
printf("%d \t",P->data[i]);
printf("%d \t",P->data[i]);

You might also like