//Insertion and deletion in a Queue
#include<stdio.h>
#include<conio.h>
int front=0,rear=0,A[15],n;
void enq()
{
int val;
printf(“n Enter the no to be enqueued===> “);
scanf(“%d”,&val);
if(((front==1)&&(rear==n))||(front==(rear+1)))
printf(“n “Queue overflow” n”);
else
{
if(front==0)
{
front=1;
rear=1;
}
else if(rear==n)
rear=1;
else
rear=rear+1;
A[rear]=val;
}
}
void deq()
{
if(front==0)
{
printf(“n “Queue underflow”n”);
}
else if(front==rear)
{
front=0;
rear=0;
}
else if(front==n)
front=1;
else
front=front+1;
}
void display()
{
printf(“nn The elements in Queue are::n”);
if(front<=rear)
{
for(int i=front;i<=rear;i++)
printf(” %d “,A[i]);
}
else
{
for(int i=front;i<=n;i++)
printf(” %d “,A[i]);
for(i=1;i<=rear;i++)
printf(” %d “,A[i]);
}
}
main()
{
clrscr();
int ch;
printf(“nn INSERTION AND DELETION IN QUEUESnn”);
printf(“n Enter the maximum size of Queue::”);
scanf(“%d”,&n);
printf(“nn Enter your choice::n 1==>Enqueue,n 2==>Dequeue,n 3==>Display,and,n 4==>Exit:: “);
scanf(“%d”,&ch);
do
{
if(ch==1)
enq();
else if(ch==2)
deq();
else if(ch==3)
display();
else if(ch==4)
return 0;
else
printf(“Oops!! Wrong choice entered”);
printf(“nn Enter your choice again::”);
scanf(“%d”,&ch);
}
while(ch!=4);
}
0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.