Implementation of Double Ended Queue operations like rearAdd(), frontDel(), rearDel() and frontAdd() using Array in C
#include<stdio.h>
#include<conio.h>
#define max 5
int dq[max];
int front=0, rear=-1;
int choice;
char ch;
void dadd_rear (int num)
{
if (rear==(max-1))
{
printf("\n ADDING ELEMENT IN REAR IS NOT POSSIBLE ");
return;
}
rear=rear+1;
dq[rear] =num;
}
void dadd_front (int num)
{
if (front==0)
{
printf ("\nADDING ELEMENT IN FRONT IS NOT POSSIBLE");
return;
}
front=front-1;
dq [front] =num;
}
int ddel_front ()
{
int num;
if (front==rear)
{
printf ("\n QUEUE IS EMPTY");
return (0);
}
num=dq[front];
front=front+1;
return (num);
}
int ddel_rear ()
{
int num;
if (front==rear)
{
printf ("\n QUEUE IS EMPTY");
return (0);
}
num=dq[front];
rear=rear-1;
return (num);
}
void dprint ()
{
int i;
if (front<=rear)
{
printf ("\n Elements of deque:");
for (i=front; i<=rear; i++)
printf (" %d", dq[i]) ;
}
else
printf ("\n Deque is empty");
return;
}
int main ()
{
int n, item,x;
do
{
printf ("\n Press 1 to insert at the REAR end in a queue:");
printf ("\n Press 2 to delete at the FRONT end from a queue:");
printf ("\n Press 3 to insert at the FRONT end in a queue:");
printf ("\n Press 4 to delete at the REAR end from a queue:");
printf ("\n Press 5 to display deque");
printf ("\n Press 6 to exit");
printf ("\n Enter your choice:");
scanf ("%d", &n);
switch (n)
{
case 1: printf ("\n Enter the element to be inserted:");
scanf ("%d", &item);
dadd_rear (item);
break;
case 2: item=ddel_front ();
printf ("\n Deleted element is %d",item);
break;
case 3: printf ("\n Enter the element to be inserted:");
scanf ("%d", &item);
dadd_front (item);
break;
case 4: x=ddel_rear ();
printf ("\n Deleted element is %d", x);
break;
case 5: dprint ();
break;
case 6: break;
default : printf ("\n Wrong choice");
}
}
while (n<6);
return 0;
}
OUTPUT :
Post a Comment
0 Comments