Dequeue in Data Structure

Dequeue (Double Ended Queue)

In Double Ended Queue, insert and delete operation can be occur at both ends that is front and rear of the queue.

dequeue

Example: Program for Double Ended Queue (Dequeue)

#include<stdio.h>
#include<stdlib.h>
#define MAX 30

typedef struct dequeue
{
    int data[MAX];
    int rear,front;
}dequeue;

void initialize(dequeue *p);
int empty(dequeue *p);
int full(dequeue *p);
void enqueueR(dequeue *p,int x);
void enqueueF(dequeue *p,int x);
int dequeueF(dequeue *p);
int dequeueR(dequeue *p);
void print(dequeue *p);

void main()
{
    int i,x,op,n;
    dequeue q;     
    initialize(&q);     
    do
    {
     printf("\n1.Create\n2.Insert(Rear)\n3.Insert(Front)\n4.Delete(Rear)\n5.Delet(Front)");
        printf("\n6.Print\n7.Exit\n\nEnter your choice:");
        scanf("%d",&op);
        
        switch(op)
        {
            case 1: printf("\nEnter number of elements:");
                    scanf("%d",&n);
                    initialize(&q);
                    printf("\nEnter the data:");                     
                    for(i=0;i<n;i++)
                    {
                        scanf("%d",&x);
                        if(full(&q))
                        {
                            printf("\nQueue is Full!!");
                            exit(0);
                        }
                        enqueueR(&q,x);
                    }
                    break;
                    
            case 2: printf("\nInsert Element : ");
                    scanf("%d",&x);                             
                    if(full(&q))
                    {
                        printf("\nQueue is Full!!");
                        exit(0);
                    }                     
                    enqueueR(&q,x);
                    break;
                            
            case 3: printf("\nInsert Element :");
                    scanf("%d",&x);                             
                    if(full(&q))
                    {
                        printf("\nQueue is Full!!");
                        exit(0);
                    }                     
                    enqueueF(&q,x);
                    break;
                            
            case 4: if(empty(&q))
                    {
                        printf("\nQueue is Empty!!");
                        exit(0);
                    }                             
                    x=dequeueR(&q);
                    printf("\n Deleted Element is %d\n",x);
                    break;
                    
            case 5: if(empty(&q))
                    {
                        printf("\nQueue is Empty!!");
                        exit(0);
                    }                             
                    x=dequeueF(&q);
                    printf("\nDeleted Element is %d\n",x);
                    break;
                            
            case 6: print(&q);
                    break;
                    
            default: break;
        }
    }while(op!=7);
}  
void initialize(dequeue *P)
{
    P->rear=-1;
    P->front=-1;
}  
int empty(dequeue *P)
{
    if(P->rear==-1)
        return(1);
    
    return(0);
}  
int full(dequeue *P)
{
    if((P->rear+1)%MAX==P->front)
        return(1);
    
    return(0);
}  
void enqueueR(dequeue *P,int x)
{
    if(empty(P))
    {
        P->rear=0;
        P->front=0;
        P->data[0]=x;
    }
    else
    {
        P->rear=(P->rear+1)%MAX;
        P->data[P->rear]=x;
    }
}  
void enqueueF(dequeue *P,int x)
{
    if(empty(P))
    {
        P->rear=0;
        P->front=0;
        P->data[0]=x;
    }
    else
    {
        P->front=(P->front-1+MAX)%MAX;
        P->data[P->front]=x;
    }
}  
int dequeueF(dequeue *P)
{
    int x;     
    x=P->data[P->front];     
    if(P->rear==P->front)    //delete the last element
        initialize(P);
    else
        P->front=(P->front+1)%MAX;     
    return(x);
}   
int dequeueR(dequeue *P)
{
    int x;     
    x=P->data[P->rear];     
    if(P->rear==P->front)
        initialize(P);
    else
        P->rear=(P->rear-1+MAX)%MAX;         
    return(x);
}  
void print(dequeue *P)
{
    if(empty(P))
    {
        printf("\nQueue is empty!!");
        exit(0);
    }     
    int i;
    i=P->front;     
    while(i!=P->rear)
    {
        printf("\n%d",P->data[i]);
        i=(i+1)%MAX;
    }     
    printf("\n%d\n",P->data[P->rear]);
}


Output:

1. Create

dequeue create

2. Insert (Front)

dequeue insert front

3. Insert (Rear)

dequeue insert rear

4. Display (Insert)

dequeue insert display

5. Delete (Rear)

dequeue delete rear

6. Delete (Front)

dequeue delete front

7. Display (Delete)

dequeue delete display