15. Program for
implementing dequeue(deque) in an array?
Output
Program
#include<stdio.h>
#include<conio.h>
#define MAX 5
int deque[MAX],front=-1,rear=-1,ch,item;
char c;
/*-------Insert at front-------*/
/*-------Insert at front-------*/
void insfro()
{
if(((front==0)&&(rear==MAX-1))||(front==rear+1))
{
printf("\tDequeue Overflow\n");
return;
}
if((front==-1)&&(rear==-1))
{
front=0;
rear=0;
}
else
if(front==0)
{
front=MAX-1;
}
else
{
front=front-1;
}
printf("\nEnter the item:\t");
scanf("%d",&item);
deque[front]=item;
}
/*----------------Insert at rear--------------*/
/*----------------Insert at rear--------------*/
void insrear()
{
if(((front==0)&&(rear==MAX-1))||(front==rear+1))
{
printf("\tDequeue Overflow\n");
return;
}
if((front==-1)&&(rear==-1))
{
front=0;
rear=0;
}
else if(rear==MAX-1)
{
rear=0;
}
else
{
rear=rear+1;
}
printf("\nEnter the item:\t");
scanf("%d",&item);
deque[rear]=item;
}
/*-----------Deletion from front------------*/
/*-----------Deletion from front------------*/
void delfro()
{
if((rear==-1)&&(front==-1))
{
printf("\tDeque Underflow\n");
return;
}
else
{
printf("\nThe deleted element is %d",deque[front]);
}
if(rear==front)
{
front=-1;
rear=-1;
}
else if(front==MAX-1)
{
front=0;
}
else
{
front=front+1;
}
}
/*----------Deletion from rear-----------*/
/*----------Deletion from rear-----------*/
void delrear()
{
if((rear==-1)&&(front==-1))
{
printf("\tDeque Underflow\n");
return;
}
else
{
printf("\nThe deleted element is %d",deque[rear]);
}
if(front==rear)
{
front=-1;
rear=-1;
}
else if(rear==0)
{
rear=MAX-1;
}
else
{
rear=rear-1;
}
}
/*-------------Displaying the Deque-------------*/
/*-------------Displaying the Deque-------------*/
void display()
{
int i;
if((front==-1)&&(rear==-1))
{
printf("\tDeque is empty\n");
return;
}
printf("\nThe dequeue elements are:\n");
if(front<=rear)
{
for(i=front;i<=rear;i++)
{
printf("%d\t",deque[i]);
}
}
else
{
for(i=0;i<=rear;i++)
printf("%d\t",deque[i]);
for(i=front;i<=MAX-1;i++)
printf("%d\t",deque[i]);
}
}
void main()
{
clrscr();
do
{
printf("Enter the choice:\t1.Input Restricted\t2.Output Restricted\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
do
{
printf("Enter the choice:\t1.Insertion at rear\t2.Deletion from front\n\t\t\t3.Deletion from rear\t4.Display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insrear();
break;
case 2:
delfro();
break;
case 3:
delrear();
break;
case 4:
display();
break;
default:
printf("\twrong choice\n");
break;
}
printf("\nDo you want to continue:(y/n)?\n");
scanf(" %c",&c);
}
while((c=='y')||(c=='Y'));
break;
case 2:
do
{
printf("Enter the choice:\t1.Insert at front\t2.Insert
at rear\n\t\t\t3.Deletion from front\t4.Display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insfro();
break;
case 2:
insrear();
break;
case 3:
delfro();
break;
case 4:
display();
break;
default:
printf("\twrong choice\n");
break;
}
printf("\nDo you want to continue:(y/n)?\n");
scanf(" %c",&c);
}
while((c=='y')||(c=='Y'));
break;
default:
printf("\twrong choice\n");
break;
}
printf("\nDo you want to continue:(y/n)?\n");
scanf(" %c",&c);
}
while((c=='y')||(c=='Y'));
getch();
}
Output
Enter the choice: 1.Input Restricted 2.Output Restricted
1
Enter the choice: 1.Insertion at rear 2.Deletion from front
3.Deletion from rear 4.Display
1
Enter the item: 1
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion at rear 2.Deletion from front
3.Deletion from rear 4.Display
1
Enter the item: 2
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion at rear 2.Deletion from front
3.Deletion from rear 4.Display
1
Enter the item: 3
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion at rear 2.Deletion from front
3.Deletion from rear 4.Display
1
Enter the item: 4
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion at rear 2.Deletion from front
3.Deletion from rear 4.Display
1
Enter the item: 5
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion at rear 2.Deletion from front
3.Deletion from rear 4.Display
4
The dequeue elements are:
1 2 3 4 5
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion at rear 2.Deletion from front
3.Deletion from rear 4.Display
1
Dequeue Overflow
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion at rear 2.Deletion from front
3.Deletion from rear 4.Display
2
The deleted element is 1
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion at rear 2.Deletion from front
3.Deletion from rear 4.Display
3
The deleted element is 5
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion at rear 2.Deletion from front
3.Deletion from rear 4.Display
4
The dequeue elements are:
2 3 4
Do you want to continue:(y/n)?
n
Do you want to continue:(y/n)?
y
Enter the choice: 1.Input Restricted 2.Output Restricted
2
Enter the choice: 1.Insert at front 2.Insert at rear
3.Deletion from front 4.Display
1
Enter the item: 1
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert at front 2.Insert at rear
3.Deletion from front 4.Display
4
The dequeue elements are:
1 2 3 4
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert at front 2.Insert at rear
3.Deletion from front 4.Display
2
Enter the item: 5
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert at front 2.Insert at rear
3.Deletion from front 4.Display
4
The dequeue elements are:
1 2 3 4 5
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert at front 2.Insert at rear
3.Deletion from front 4.Display
3
The deleted element is 1
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert at front 2.Insert at rear
3.Deletion from front 4.Display
4
The dequeue elements are:
2 3 4 5
Do you want to continue:(y/n)?
n
Do you want to continue:(y/n)?
n
No comments:
Post a Comment