DEQUE implementation Using Arrays
Deque is a double Ended queue, where you can insert and delete from both front and rear end.
Deque is of two types:
1) INPUT restricted Deque - in it you can enter the element from only one end either from front or rear. But can delete from both ends.
2) OUTPUT restricted Deque - In it you can delete elements from only one end either from front or rear. But can insert from both ends.
1) INPUT restricted Deque - in it you can enter the element from only one end either from front or rear. But can delete from both ends.
2) OUTPUT restricted Deque - In it you can delete elements from only one end either from front or rear. But can insert from both ends.
A Deque follows properties of both stack and queue.
It follows STACK (LIFO) in such a way that we can restrict insertion and deletion from only one end, like Stack.
It follows QUEUE (FIFO) Property in such a way that we can restrict insertion from rear end and deletion from front end, and we can set any end as rear or front because elements can be deleted or inserted from both the end.
It follows STACK (LIFO) in such a way that we can restrict insertion and deletion from only one end, like Stack.
It follows QUEUE (FIFO) Property in such a way that we can restrict insertion from rear end and deletion from front end, and we can set any end as rear or front because elements can be deleted or inserted from both the end.
CODE:
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
void enqueue_front(int d[], int *f, int *r, int size)
{
int data;
if((*f==0 && *r==size-1) ||(*f==*r+1) )
printf("Deque is Full !"); /*(*f==0 && *r==size-1) this condition arrived only when \n
`rear was at end and front was at any arbitrary index behind rear.\n
Than front came back again and again by one position to enter the data,\n
and finally at index 0. Now rear is at end and all elements \n
are entered from end towards front, hence Deque become full. Don't use (*r+1)%n==*f to check full or not \n
You can use it in enqueue_rear*/
else if(*f==-1 && *r==-1)
{
*f=0; *r=0;
printf("Enter the data: ");
scanf("%d",&data);
d[*f]=data;
}
else if(*f==0)
{
*f=size-1;
printf("Enter the data: ");
scanf("%d",&data);
d[*f]=data;
} /* Peeche hi toh jaayega front agar front se enter karwaaoge, \n
kyunki agar front se enter hoga toh entered element pichle wale se bhi pehle aana chahiya*/
else
{
*f=*f-1;
printf("Enter the data: ");
scanf("%d",&data);
d[*f]=data;
}
}
void enqueue_rear(int d[], int *f, int *r, int size)
{
// Similar to enqueue in circular Queue;
int data;
if(((*r)+1)%size==*f)
printf("Deque is Full!");
else if(*f==-1 && *r==-1)
{
*f=0;*r=0;
printf("Enter the data: ");
scanf("%d",&data);
d[*r]=data;
}
else
{
*r=(*r+1)%size;
printf("Enter the data: ");
scanf("%d",&data);
d[*r]=data;
}
}
void dequeue_front(int d[], int *f, int *r, int size)
{ // Similar to dequeue in circular Queue.
if(*f==-1 && *r==-1)
printf("Queue is empty\n");
else if(*f==*r)
{
printf("Deleted element is: %d\n\nAnd Queue is now EMPTY\n",d[*f]);
*r=-1;*f=-1;
}
else
{
printf("deleted element is %d\n",d[*f]);
*f=(*f+1)%size;
}
}
void dequeue_rear(int d[], int *f, int *r, int size)
{/* rear is that from where elements are inserted, \n
Now bring back rear in order to delete the element from end. */
if (*f==-1 && *r==-1)
printf("Deque is empty !\n");
else if(*f==*r){
printf("Deleted element is: %d\n\nAnd Queue is now EMPTY\n",d[*r]);
*f=-1; *r=-1;}
else if(*r==0)
{
printf("Deleted element is: %d\n",d[*r]);
*r=size-1;
}
else
{
printf("Deleted element is: %d\n",d[*r]);
*r=*r-1;
}
}
void display(int d[], int f, int r, int size)
{
int i;
if(f==-1 && r==-1)
printf("Deque is empty !\n");
else
{
i=f;
printf("Deque is\nFront\n");
while(i!=r)
{
printf("%d\n",d[i]);
i=(i+1)%size;
}printf("%d",d[i]);printf("\nEnd\n");
}
}
int main()
{
int n,ch,choice=1,data, tp1=1,tp2=1,type;
printf("Enter the size of Queue: ");
scanf("%d",&n);
int deque[n];
int front=-1, rear=-1;
while(choice==1){
printf("Enter\n1.For Input restricted Deque\n2.For Output restricted Deque\n");
scanf("%d",&type);
if(type==1)
{
while(tp1==1)
{
printf("Enter 1. to Enqueue from front end\n");
printf("Enter 2. to Dequeue from front end \n");
printf("Enter 3. to Dequeue from rear end\n");
printf("Enter 4. to Display\n");
printf("Enter choice: ");
scanf("%d",&ch);
if(ch==1)
enqueue_front(deque, &front, &rear, n);
else if(ch==2)
dequeue_front(deque, &front, &rear,n);
else if (ch==3)
dequeue_rear(deque, &front, &rear,n);
else if(ch==4)
display(deque, front, rear, n);
else
printf("Invalid choice, ");
printf("Want to continue(0,1): ");
scanf("%d",&tp1); if(tp1!=1) choice=0;
//set choice=0 to get out of outermost loop.
}
}
else if(type==2)
{
while(tp2==1)
{
printf("Enter 1. to Enqueue from front end.\n");
printf("Enter 2. to Enqueue from rear end\n");
printf("Enter 3. to Dequeue from front end\n");
printf("Enter 4. to Display\n");
printf("Enter choice: ");
scanf("%d",&ch);
if(ch==1)
enqueue_front(deque, &front, &rear, n);
else if(ch==2)
enqueue_rear(deque, &front, &rear, n);
else if (ch==3)
dequeue_front(deque, &front, &rear,n);
else if(ch==4)
display(deque, front, rear, n);
else
printf("Invalid choice, ");
printf("Want to continue(0,1): ");
scanf("%d",&tp2); if(tp2!=1) choice=0;
}
}
else
{printf("\nSelect a Valid Deque, Want to continue(0,1): ");
scanf("%d",&choice);}
}
}
Comments
Post a Comment