#include<stdio.h>
#define MAX 10
int que[MAX];
int front=-1;
int rear=-1;
void insert();
void del();
void disp();
main()
{
int ch;
while(1)
{
printf("\n..................MENU................\n");
printf("select your choice\n");
printf("1.insert\n");
printf("2.delete\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
exit(1);
default :
printf("invalid choice\n");
}
}
}
void insert()
{
int val;
if((front==0&&rear==MAX-1)||(front==rear+1))
{
printf("queue overflow\n");
return;
}
if(front==-1)
{
front=0;
rear=0;
}
else
if(rear==MAX-1)
{
rear=0;
}
else
rear=rear+1;
printf("enter the element for insertion in queue:");
scanf("%d",&val);
que[rear]=val;
}
void del()
{
if(front==-1)
{
printf("queue underflow\n");
return;
}
printf("element deleted from queue is=%d",que[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else
if(front==MAX-1)
{
front=0;
}
else
front=front+1;
}
void disp()
{
int f=front,r=rear;
if(front==-1)
{
printf("queue is empty\n");
return;
}
printf("queue elements:\n");
if(f<=r)
while(f<=r)
{
printf("%d\t",que[f]);
f++;
}
else
{
while(f<=MAX-1)
{
printf("%d\t",que[f]);
f++;
}
f=0;
while(f<=r)
{
printf("%d\t",que[f]);
f++;
}
}
}
OUTPUT
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:1
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:2
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:3
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:3
queue elements:
1 2 3
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:2
element deleted from queue is=1
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:2
element deleted from queue is=2
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:2
element deleted from queue is=3
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:8
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:3
queue elements:
8
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:4
Process returned 1 (0x1) execution time : 53.552 s
Press any key to continue.
#define MAX 10
int que[MAX];
int front=-1;
int rear=-1;
void insert();
void del();
void disp();
main()
{
int ch;
while(1)
{
printf("\n..................MENU................\n");
printf("select your choice\n");
printf("1.insert\n");
printf("2.delete\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
exit(1);
default :
printf("invalid choice\n");
}
}
}
void insert()
{
int val;
if((front==0&&rear==MAX-1)||(front==rear+1))
{
printf("queue overflow\n");
return;
}
if(front==-1)
{
front=0;
rear=0;
}
else
if(rear==MAX-1)
{
rear=0;
}
else
rear=rear+1;
printf("enter the element for insertion in queue:");
scanf("%d",&val);
que[rear]=val;
}
void del()
{
if(front==-1)
{
printf("queue underflow\n");
return;
}
printf("element deleted from queue is=%d",que[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else
if(front==MAX-1)
{
front=0;
}
else
front=front+1;
}
void disp()
{
int f=front,r=rear;
if(front==-1)
{
printf("queue is empty\n");
return;
}
printf("queue elements:\n");
if(f<=r)
while(f<=r)
{
printf("%d\t",que[f]);
f++;
}
else
{
while(f<=MAX-1)
{
printf("%d\t",que[f]);
f++;
}
f=0;
while(f<=r)
{
printf("%d\t",que[f]);
f++;
}
}
}
OUTPUT
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:1
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:2
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:3
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:3
queue elements:
1 2 3
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:2
element deleted from queue is=1
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:2
element deleted from queue is=2
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:2
element deleted from queue is=3
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:1
enter the element for insertion in queue:8
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:3
queue elements:
8
..................MENU................
select your choice
1.insert
2.delete
3.display
4.exit
enter your choice:4
Process returned 1 (0x1) execution time : 53.552 s
Press any key to continue.