#include<stdio.h>
#include<conio.h>
#define max 10
int queue[max];
int front=-1,rear=-1,item;
void insert_rear()
{
if(rear==max-1)
printf("\nqueue is full");
else
{
printf("\nenter the
element");
scanf("%d",&item);
if(rear==-1&&front==-1)
{
rear=0;
front=0;
}
else rear++;
queue[rear]=item;
printf("\ninserted element
%d",item);
}
}
void insert_front()
{
if(front==0)
printf("\ncannot add from front
end");
else
{
printf("\nenter the
element");
scanf("%d",&item);
if(rear==-1&&front==-1)
{
rear=0;
front=0;
}
else front--;
queue[front]=item;
printf("\ninserted element
%d",item);
}
}
void delete_front()
{
if (front==-1)
printf("\nqueue is empty");
else
{
item=queue[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else front++;
printf("\nDeleted element
%d",item);
}
}
void delete_rear()
{
if(rear==-1)
printf("\nQueue is
empty");
else
{
item=queue[rear];
if(front==rear)
{
front=-1;
rear=-1;
}
else
rear--;
printf("\nDeleted
element %d",item);
}
}
void display()
{
int i;
if(front==-1&&rear==-1)
printf("\nQueue is
empty");
else
{
for(i=front;i<=rear;i++)
printf("%d",queue[i]);
}
}
void main()
{
int a;
printf("\n1:insert from front\n2:insert
from rear\n3:delete from front\n4:delete from rear\n5:display\n6:exit");
do
{
printf(“\nenter the choice”);
scanf("%d",&a);
switch(a)
{
case 1:insert_front();
break;
case 2:insert_rear();
break;
case 3:delete_front();
break;
case 4:delete_rear();
break;
case 5:display();
break;
case 6:exit(0);
break;
default:printf("\ninvalid
input..........");
}
}
while(a!=6);
}
/*
OUTPUT:
1:Insert from front
2:Insert from rear
3:Delete from front
4:Delete from rear
5:display
6: exit
enter the choice 1
enter the element 1
inserted element 1
enter the choice 1
cannot add from
front end
enter the choice 2
enter the element 2
inserted element 2
enter the choice 2
enter the element 3
inserted element 3
enter the choice 3
Deleted element 1
enter the choice 4
Deleted element 3
enter the choice 5
2
enter the choice 6
*/
No comments:
Post a Comment