Queue program #2
22. Implement Circular Queue using array that performs following operations: INSERT, DELETE,DISPLAY.
#include<stdio.h>
#define N 5
int q[N];
int f=-1;
int r=-1;
void cqinsert(int y);
int cqdelete();
void display();
void main()
{
int ch,x,a;
printf ("QUEUE OPERATION\n");
while (1)
{
printf ("------------------------------------------\n");
printf (" 1 --> INSERT \n");
printf (" 2 --> DELETE \n");
printf (" 3 --> DISPLAY \n");
printf (" 4 --> EXIT \n");
printf ("------------------------------------------\n");
printf ("Enter your choice\n");
scanf ("%d", &ch);
switch (ch)
{
case 1:
printf ("Enter the element to be insert in queue:\n");
scanf ("%d", &x);
cqinsert(x);
break;
case 2:
a=cqdelete();
if(a!=0)
printf("Delete=%d\n",a);
break;
case 3:
display();
break;
case 4:
return;
default :
printf("Invalid entered choice, please enter choice between 1 to 4.\n");
}
}
}
void cqinsert(int y)
{
if(f>r && r==f-1 || f==0 && r==N-1)
{
printf("Queue Overflow\n");
return;
}
if(f<r && r==N-1)
{
r=0;
}
else
{
r=r+1;
}
q[r]=y;
if(f==-1)
{
f=0;
return;
}
}
int cqdelete()
{
int y;
if(f==-1)
{
printf("Queue Underflow\n");
return(0);
}
y=q[f];
if(f==r)
{
f=r=-1;
return(y);
}
if(f==N-1)
{
f=0;
}
else
{
f=f+1;
}
return(y);
}
void display()
{
int j;
if(f==-1 && r==-1)
{
printf ("Queue is empty\n");
}
if (f<=r)
{
printf ("\nThe status of the queue is \n");
for (j=f;j<=r;j++)
{
printf ("%d\t", q[j]);
}
printf("\n");
}
else
{
printf ("\nThe status of the queue is \n");
for (j=f;j<=N-1;j++)
{
printf ("%d\t", q[j]);
}
for (j=0;j<=r;j++)
{
printf ("%d\t", q[j]);
}
printf("\n");
}
}
#define N 5
int q[N];
int f=-1;
int r=-1;
void cqinsert(int y);
int cqdelete();
void display();
void main()
{
int ch,x,a;
printf ("QUEUE OPERATION\n");
while (1)
{
printf ("------------------------------------------\n");
printf (" 1 --> INSERT \n");
printf (" 2 --> DELETE \n");
printf (" 3 --> DISPLAY \n");
printf (" 4 --> EXIT \n");
printf ("------------------------------------------\n");
printf ("Enter your choice\n");
scanf ("%d", &ch);
switch (ch)
{
case 1:
printf ("Enter the element to be insert in queue:\n");
scanf ("%d", &x);
cqinsert(x);
break;
case 2:
a=cqdelete();
if(a!=0)
printf("Delete=%d\n",a);
break;
case 3:
display();
break;
case 4:
return;
default :
printf("Invalid entered choice, please enter choice between 1 to 4.\n");
}
}
}
void cqinsert(int y)
{
if(f>r && r==f-1 || f==0 && r==N-1)
{
printf("Queue Overflow\n");
return;
}
if(f<r && r==N-1)
{
r=0;
}
else
{
r=r+1;
}
q[r]=y;
if(f==-1)
{
f=0;
return;
}
}
int cqdelete()
{
int y;
if(f==-1)
{
printf("Queue Underflow\n");
return(0);
}
y=q[f];
if(f==r)
{
f=r=-1;
return(y);
}
if(f==N-1)
{
f=0;
}
else
{
f=f+1;
}
return(y);
}
void display()
{
int j;
if(f==-1 && r==-1)
{
printf ("Queue is empty\n");
}
if (f<=r)
{
printf ("\nThe status of the queue is \n");
for (j=f;j<=r;j++)
{
printf ("%d\t", q[j]);
}
printf("\n");
}
else
{
printf ("\nThe status of the queue is \n");
for (j=f;j<=N-1;j++)
{
printf ("%d\t", q[j]);
}
for (j=0;j<=r;j++)
{
printf ("%d\t", q[j]);
}
printf("\n");
}
}
Output
- QUEUE OPERATION
------------------------------------------
1 --> INSERT
2 --> DELETE
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be insert in queue:
12
------------------------------------------
1 --> INSERT
2 --> DELETE
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be insert in queue:
13
------------------------------------------
1 --> INSERT
2 --> DELETE
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be insert in queue:
14
------------------------------------------
1 --> INSERT
2 --> DELETE
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be insert in queue:
15
------------------------------------------
1 --> INSERT
2 --> DELETE
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
3
The status of the queue is
12 13 14 15
------------------------------------------
1 --> INSERT
2 --> DELETE
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
2
Delete=12
------------------------------------------
1 --> INSERT
2 --> DELETE
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
2
Delete=13
------------------------------------------
1 --> INSERT
2 --> DELETE
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
1
Enter the element to be insert in queue:
16
------------------------------------------
1 --> INSERT
2 --> DELETE
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
3
The status of the queue is
14 15 16
------------------------------------------
1 --> INSERT
2 --> DELETE
3 --> DISPLAY
4 --> EXIT
------------------------------------------
Enter your choice
4
For download this program please check the link ➡➡ Download
Comments
Post a Comment