Circular Queue
#include<stdio.h>
#include<conio.h>
#define max 6
int cq[max];
int front=0,rear=0,count=0;
void insertcq()
{
int data;
if(count==max)
printf("circular queue is full");
else
{
printf("enter data:");
scanf("%d",&data);
cq[rear]=data;
rear=(rear+1)%max;
count++;
printf("data inserted in queue");
}
}
void deletecq()
{
if(count==0)
printf("circular queue is empty");
else
{
printf("deleted element from circular queue is %d",cq[front]);
front=(front+1)%max;
count--;
}
}
void displaycq()
{
int i,j;
if(count==0)
printf("circular queue is empty");
else
{
printf("elements in circular queue are:");
j=count;
for(i=front;j!=0;j--)
{
printf("%d ",cq[i]);
i=(i+1)%max;
}
}
}
int menu()
{
int ch;
clrscr();
printf("1.insert 2.delete 3.display 4.exit\n");
printf("enter choice:");
scanf("%d",&ch);
return ch;
}
void main()
{
int ch;
do
{
ch=menu();
switch(ch)
{
case 1:insertcq();
break;
case 2:deletecq();
break;
case 3:displaycq();
break;
case 4:exit(1);
break;
default:printf("wrong choice");
}
getch();
}while(1);
}
No comments:
Post a Comment