0% found this document useful (0 votes)
104 views

Deque (Data Structures) - Javatpoint

The document discusses deques, which are a generalization of queues that allow insertion and deletion from both ends. It defines queues and deques, describes the different types of deques, and provides examples of operations like insertion, deletion, and peeking. It also discusses applications of deques and provides an implementation in C with functions like insert, delete, display and examples.

Uploaded by

Govinda Das
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Deque (Data Structures) - Javatpoint

The document discusses deques, which are a generalization of queues that allow insertion and deletion from both ends. It defines queues and deques, describes the different types of deques, and provides examples of operations like insertion, deletion, and peeking. It also discusses applications of deques and provides an implementation in C with functions like insert, delete, display and examples.

Uploaded by

Govinda Das
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Deque (or double-ended queue)

In this article, we will discuss the double-ended queue or deque. We should first see a brief
description of the queue.

What is a queue?

A queue is a data structure in which whatever comes first will go out first, and it follows the FIFO
(First-In-First-Out) policy. Insertion in the queue is done from one end known as the rear end or the
tail, whereas the deletion is done from another end known as the front end or the head of the
queue.

The real-world example of a queue is the ticket queue outside a cinema hall, where the person who
enters first in the queue gets the ticket first, and the person enters last in the queue gets the ticket
at last.

What is a Deque (or double-ended queue)

The deque stands for Double Ended Queue. Deque is a linear data structure where the insertion and
deletion operations are performed from both ends. We can say that deque is a generalized version
of the queue.

Though the insertion and deletion in a deque can be performed on both ends, it does not follow the
FIFO rule. The representation of a deque is given as follows -

Types of deque

There are two types of deque -

Input restricted queue

Output restricted queue

Input restricted Queue

In input restricted queue, insertion operation can be performed at only one end, while deletion can
be performed from both ends.
Output restricted Queue

In output restricted queue, deletion operation can be performed at only one end, while insertion
can be performed from both ends.

Operations performed on deque

There are the following operations that can be applied on a deque -

Insertion at front

Insertion at rear

Deletion at front

Deletion at rear

AD

Machine learning in SQL. Easy.


MindsDB Automates and Abstracts Machine Learn More
Learning Models through Virtual AI Tables

We can also perform peek operations in the deque along with the operations listed above. Through
peek operation, we can get the deque's front and rear elements of the deque. So, in addition to the
above operations, following operations are also supported in deque -

Get the front item from the deque

Get the rear item from the deque

Check whether the deque is full or not


Checks whether the deque is empty or not

Now, let's understand the operation performed on deque using an example.

Insertion at the front end

In this operation, the element is inserted from the front end of the queue. Before implementing the
operation, we first have to check whether the queue is full or not. If the queue is not full, then the
element can be inserted from the front end by using the below conditions -

If the queue is empty, both rear and front are initialized with 0. Now, both will point to the
first element.

Otherwise, check the position of the front if the front is less than 1 (front < 1), then reinitialize
it by front = n - 1, i.e., the last index of the array.

Insertion at the rear end

In this operation, the element is inserted from the rear end of the queue. Before implementing the
operation, we first have to check again whether the queue is full or not. If the queue is not full, then
the element can be inserted from the rear end by using the below conditions -
AD

If the queue is empty, both rear and front are initialized with 0. Now, both will point to the
first element.

Otherwise, increment the rear by 1. If the rear is at last index (or size - 1), then instead of
increasing it by 1, we have to make it equal to 0.
Deletion at the front end

In this operation, the element is deleted from the front end of the queue. Before implementing the
operation, we first have to check whether the queue is empty or not.

If the queue is empty, i.e., front = -1, it is the underflow condition, and we cannot perform the
deletion. If the queue is not full, then the element can be inserted from the front end by using the
below conditions -

If the deque has only one element, set rear = -1 and front = -1.

Else if front is at end (that means front = size - 1), set front = 0.
AD

Else increment the front by 1, (i.e., front = front + 1).

Deletion at the rear end


In this operation, the element is deleted from the rear end of the queue. Before implementing the
operation, we first have to check whether the queue is empty or not.

If the queue is empty, i.e., front = -1, it is the underflow condition, and we cannot perform the
deletion.

If the deque has only one element, set rear = -1 and front = -1.

If rear = 0 (rear is at front), then set rear = n - 1.

Else, decrement the rear by 1 (or, rear = rear -1).

Check empty

This operation is performed to check whether the deque is empty or not. If front = -1, it means that
the deque is empty.

Check full

This operation is performed to check whether the deque is full or not. If front = rear + 1, or front =
0 and rear = n - 1 it means that the deque is full.

The time complexity of all of the above operations of the deque is O(1), i.e., constant.

Applications of deque
Deque can be used as both stack and queue, as it supports both operations.

Deque can be used as a palindrome checker means that if we read the string from both ends,
the string would be the same.

Implementation of deque
Now, let's see the implementation of deque in C programming language.
#include <stdio.h>    
#define size 5  
int deque[size];    
int f = -1, r = -1;    
//  insert_front function will insert the value from the front    
void insert_front(int x)    
{    
    if((f==0 && r==size-1) || (f==r+1))    
    {    
        printf("Overflow");    
    }    
    else if((f==-1) && (r==-1))    
    {    
        f=r=0;    
        deque[f]=x;    
    }    
    else if(f==0)    
    {    
        f=size-1;    
        deque[f]=x;    
    }    
    else    
    {    
        f=f-1;    
        deque[f]=x;    
    }    
}    
    
// insert_rear function will insert the value from the rear    
void insert_rear(int x)    
{    
    if((f==0 && r==size-1) || (f==r+1))    
    {    
        printf("Overflow");    
    }    
    else if((f==-1) && (r==-1))    
    {    
        r=0;    
        deque[r]=x;    
    }    
    else if(r==size-1)    
    {    
        r=0;    
        deque[r]=x;    
    }    
    else    
    {    
        r++;    
        deque[r]=x;    
    }    
    
}    
    
// display function prints all the value of deque.    
void display()    
{    
    int i=f;    
    printf("\nElements in a deque are: ");    
        
    while(i!=r)    
    {    
        printf("%d ",deque[i]);    
        i=(i+1)%size;    
    }    
     printf("%d",deque[r]);    
}    
    
// getfront function retrieves the first value of the deque.    
void getfront()    
{    
    if((f==-1) && (r==-1))    
    {    
        printf("Deque is empty");    
    }    
    else    
    {    
        printf("\nThe value of the element at front is: %d", deque[f]);    
    }    
        
}    
    
// getrear function retrieves the last value of the deque.    
void getrear()    
{    
    if((f==-1) && (r==-1))    
    {    
        printf("Deque is empty");    
    }    
    else    
    {    
        printf("\nThe value of the element at rear is %d", deque[r]);    
    }    
        
}    
    
// delete_front() function deletes the element from the front    
void delete_front()    
{    
    if((f==-1) && (r==-1))    
    {    
        printf("Deque is empty");    
    }    
    else if(f==r)    
    {    
        printf("\nThe deleted element is %d", deque[f]);    
        f=-1;    
        r=-1;    
            
    }    
     else if(f==(size-1))    
     {    
         printf("\nThe deleted element is %d", deque[f]);    
         f=0;    
     }    
     else    
     {    
          printf("\nThe deleted element is %d", deque[f]);    
          f=f+1;    
     }    
}    
    
// delete_rear() function deletes the element from the rear    
void delete_rear()    
{    
    if((f==-1) && (r==-1))    
    {    
        printf("Deque is empty");    
    }    
    else if(f==r)    
    {    
        printf("\nThe deleted element is %d", deque[r]);    
        f=-1;    
        r=-1;    
            
    }    
     else if(r==0)    
     {    
         printf("\nThe deleted element is %d", deque[r]);    
         r=size-1;    
     }    
     else    
     {    
          printf("\nThe deleted element is %d", deque[r]);    
          r=r-1;    
     }    
}    
    
int main()    
{    
    insert_front(20);    
    insert_front(10);    
    insert_rear(30);    
    insert_rear(50);    
    insert_rear(80);    
    display();  // Calling the display function to retrieve the values of deque    
    getfront();  // Retrieve the value at front-end  
    getrear();  // Retrieve the value at rear-end   
    delete_front();    
    delete_rear();    
    display(); // calling display function to retrieve values after deletion   
    return 0;    
}     

Output:

So, that's all about the article. Hope, the article will be helpful and informative to you.

← Prev Next →

AD

Shop Latest Gadgets


JioMart

Youtube For Videos Join Our Youtube Channel: Join Now

You might also like