QUEUE OPERATION USING ARRAY
#include<stdio.h>
#include<conio.h>
#define
SIZE 5
int
i,rear,front,item,s[SIZE];
void
insert(int item,int s[]);
void
del(int s[]);
void
display(int s[]);
void
main()
{
int
ch;
clrscr();
front=0;
rear=-1;
do
{
printf("\n\n 1.INSERTION \n
2.DELETION \n 3.EXIT \n");
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\t INSERTION \n");
if(rear>=SIZE-1)
{
printf("\t\n QUEUE IS FULL\n");
}
else
{
printf("\n ENTER AN ELEMENT : ");
scanf("%d",&item);
insert(item,s);
}
display(s);
break;
case 2:
printf("\n\t DELETION \n");
if(front>rear)
{
printf("\t\nQUEUE IS EMPTY\n");
}
else
{
del(s);
}
display(s);
break;}
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\t INSERTION \n");
if(rear>=SIZE-1)
{
printf("\t\n QUEUE IS FULL\n");
}
else
{
printf("\n ENTER AN ELEMENT : ");
scanf("%d",&item);
insert(item,s);
}
display(s);
break;
case 2:
printf("\n\t DELETION \n");
if(front>rear)
{
printf("\t\nQUEUE IS EMPTY\n");
}
else
{
del(s);
}
display(s);
break;}
}
while(ch!=3);
getch();
}
void
insert(int item,int s[])
{
if(rear<SIZE)
{
rear=rear+1;
s[rear]=item;
}
}
void
del(int s[])
{
int
i;
item=s[front];
for(i=0;i<=rear;i++)
s[i]=s[i+1];
rear--;
printf("\n
DELETED ELEMENT IS %d\n\n",item);
}
void
display(int s[])
{
printf("\n");
for(i=front;i<=rear;i++)
{
printf("
\t %d",s[i]);
}
}
Output:
1.INSERTION
2.DELETION
3.EXIT
ENTER
YOUR CHOICE : 1
INSERTION
ENTER AN ELEMENT : 2
2
1.INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 1
INSERTION
ENTER AN ELEMENT : 3
2 3
1.INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 1
INSERTION
ENTER AN ELEMENT : 5
2 3 5
1.INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 2
DELETION
DELETED ELEMENT IS 2
3 5
1.INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 2
DELETION
DELETED ELEMENT IS 3
5
1.INSERTION
2.DELETION
3.EXIT
ENTER
YOUR CHOICE : 3
No comments:
Post a Comment