TEAM CODING-BCA

CIRCULAR QUEUE

CIRCULAR  QUEUE

#include<stdio.h>
#include<stdlib.h>
int q[10],front=-1,rear=-1;
void insert();
void deletion();
void disply();
void insert()
{
if(rear==9 && front==0 || front==rear+1)
{
printf("overflow");
return;
}
if (front==-1)
{
front=0;
rear=0;
}
else
{
if (rear==9)
rear=0;
else
rear++;
}

printf("eneter the data");
scanf("%d",&q[rear]);
}
void deletion()
{
if(front==-1)
{
printf("undrflow");
return;
}
printf("deleted data is %d",q[front]);
if (front==rear)
{
front=-1;
rear=-1;
}
else
{
if (front==9)
front=0;
else
front++;

}

}
void display()
{
int i;
if(front==-1)
{
printf("underflow");
return;
}
if (rear>=front)
{
printf("the queue is:");
for(i=front;i<=rear;i++)
{
printf("%d ",q[i]);
}
}
else
{
printf("the queue is:");
for(i=0;i<=rear;i++)
{
printf("%d ",q[i]);
}
for (i=front;i<9;i++)
{
printf("%d ",q[i]);
}
}

}
int main()
{
int ch;
while(1)
{
printf(" 1 to insert 2 to delete 3 to display 4 to exit  ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
return 0;
}

No comments:

Post a Comment