13. Program for
implementing Circular Queue in an array?
Output
Program
#include<stdio.h>
#include<conio.h>
#define MAX 3
int cq[MAX],front=-1,rear=-1;
void ins()
{
int item;
if((rear==MAX-1)&&(front==0)||(front==rear+1))
{
printf("\tQueue Overflow\n");
return;
}
if(front==-1)
{
front=0;
rear=0;
}
else if(rear==MAX-1)
{
rear=0;
}
else
{
rear++;
}
printf("Enter the item:\t");
scanf("%d",&item);
cq[rear]=item;
}
void del()
{
if(front==-1)
{
printf("\tQueue Underflow\n");
return;
}
printf("\nThe deleted element is %d",cq[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==MAX-1)
{
front=0;
}
else
{
front++;
}
}
void disp()
{
int i;
if(front==-1)
printf("\tQueue Empty\n");
else
{
printf("\nElements of Queue are:\n");
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("%d ",cq[i]);
for(i=front;i<MAX;i++)
printf("%d\t",cq[i]);
}
else
{
for(i=front;i<=rear;i++)
printf("%d\t",cq[i]);
}
}
}
void main()
{
int ch;
char c;
clrscr();
do
{
printf("Enter the choice:\t1.Insert\t2.Delete\t3.Display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
ins();
break;
case 2:
del();
break;
case 3:
disp();
break;
default:
printf("Invalid choice");
break;
}
printf("\nDo you want to continue:(y/n)?\n");
scanf(" %c",&c);
}
while((c=='y')||(c=='Y'));
getch();
}
Output
Enter the choice: 1.Insert 2.Delete 3.Display
1
Enter the item: 1
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert 2.Delete 3.Display
1
Enter the item: 2
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert 2.Delete 3.Display
1
Enter the item: 3
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert 2.Delete 3.Display
3
Elements of Queue are:
1 2 3
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert 2.Delete 3.Display
1
Queue Overflow
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert 2.Delete 3.Display
2
The deleted element is 1
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert 2.Delete 3.Display
2
The deleted element is 2
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert 2.Delete 3.Display
3
Elements of Queue are:
3
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert 2.Delete 3.Display
1
Enter the item: 1
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert 2.Delete 3.Display
3
Elements of Queue are:
1 3
Do you want to continue:(y/n)?
n
No comments:
Post a Comment