You are on page 1of 3

7.

Write a C Program to simulate the working of a queue of


integers using an array. Provide the following operations:
a: Insert
b: Delete
c: Display

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define size 5
int queue[size];
int front,rear;
void insert(int);
int delete1();
void display();
void main()
{
int item,choice;
clrscr();
front=0;
rear=-1;
while(1)
{
printf("enter the choice\n");
printf("1:insert 2:delete 3:display 4:exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter item to be inserted\n");
scanf("%d",&item);
insert(item);
break;
case 2:item=delete1();
if(item==-1)
{
printf("Queue empty\n");
}
else
{
printf("Element deleted is %d\n",item);
}
break;
case 3:display();
break;
case 4:exit(0);
default:printf("Invalid choice\n");
break;
}
}
}
void insert(int ele)
{
if(rear==(size-1))
{
printf("Queue full\n");
}
else
{
rear=rear+1;
queue[rear]=ele;
}
}
int delete1()
{
int temp;
if(rear<front)
{
temp=-1;
}
else
{
temp=queue[front];
front++;
}
return(temp);
}
void display()
{
int i;
if(rear<front)
{
printf("Queue empty\n");
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d\n",queue[i]);
}
}
}

OUTPUT
enter the choice
1:insert 2:delete 3:display 4:exit
1
enter item to be inserted
11
enter the choice
1:insert 2:delete 3:display 4:exit
1
enter item to be inserted
22
enter the choice
1:insert 2:delete 3:display 4:exit
3
22
11
enter the choice
1:insert 2:delete 3:display 4:exit
2
Item deleted is 22
enter the choice
1:insert 2:delete 3:display 4:exit
2
Item deleted is 11
2
queue empty
enter the choice
1:insert 2:delete 3:display 4:exit
1
enter item to be inserted
44
enter the choice
1:insert 2:delete 3:display 4:exit
1
enter item to be inserted
55
enter the choice
1:insert 2:delete 3:display 4:exit
1
enter item to be inserted
66
enter the choice
1:insert 2:delete 3:display 4:exit
1
enter item to be inserted
77
enter the choice
1:insert 2:delete 3:display 4:exit
1
enter item to be inserted
88
enter the choice
1:insert 2:delete 3:display 4:exit
1
enter item to be inserted
99
queue full
enter the choice
1:insert 2:delete 3:display 4:exit
4