Double Ended Queue in CPP – deque in C++

In this CPP tutorial, we are going to discuss the Double Ended Queue and its implementation. In Queue we have studied that insertion is done at the rear side and deletion is done from the front but with more advancement in data structure insertion and deletion is done at both front and rear side i.e. Double Ended Queue.

What is Double Ended Queue?

Double ended queue are implementation of queue where insertion and deletion are done at both ends. It is a sequence container with feature of contraction and expansion at both ends.

Syntax :-                  deque<data_type>deque_name;

C++ code for deque

 
 int main()
{
    deque<int>d;

     d.push_back(1);
     d.push_front(2);
 }

Operations of Deque in C++

  • deque insert() :- deque::insert() function is used to insert elements in deque.

Insertion in deque can be done in 3 ways :-

a.) Insertion a given value at a point 

Syntax:-          deque_name ( iterator_position, value );



  int main()
 {
    deque<int>d;

    deque<int>::iterator it = d.begin();
     ++it;

      d.insert(it,value);
}

  b.) Inserting n values at a point 

Syntax:-   deque_name.insert( iterator, size, value );

Code Snippet

 
 int main()
{
   deque<int>d;

    deque<int>iterator::it=deque.begin();

   d.insert( it, size, value );
}
  •  deque:: push_front()

push_front() is used to push the value in a deque from front. New value is inserted at front and container size is increased by 1.

Syntax:-       deque_name.push_front( value );

  •  deque:: push_back()

push_back() is used to push the value at the back of deque. New value is inserted at rear side of queue and container size is increased by 1.

Syntax :-         deque_name.push_back( value );

  • deque:: pop_front()

pop_front() is used to remove a value from the front of the queue. Value is deleted from the front and container size is reduced by 1.

Syntax:-            deque_name.pop_front();

 

  • deque:: pop_back()

pop_back() is used to remove a value from the back of the queue. Value is deleted from the back and container size is reduced by 1.

Syntax:-            deque_name.pop_back();

  •   deque:: front()

front() function is used to fetch the first element from the front of the deque.

Syntax:-            deque_name.front();

 

  •   deque:: back()

back() function is used to fetch the first element from the back of the deque.

Syntax:-            deque_name.back();

  •    deque:: clear()

clear() function is used to remove all elements from the deque container and reduces its size to 0.

Syntax:-            deque_name.clear();

  •   deque:: empty()

empty() function is used to check whether deque container us empty or not.

Syntax:-             deque_name.empty();

  •   deque:: size()

size() function is used to check the number of elements in deque container.

Syntax:-               deque_name.size();

  • deque:: operator=

It is used value a deque container to another if the container to which value is assigned holds elements previously then that values are destroyed and new values.

Syntax:-             deque_name1 = (deque_name2);

  • deque:: operator[]

It is used to fetch the value from the deque container at  a given position.

Syntax:-               deque_name[ position ];

Thanks For Reading !!

For any doubts, comment in the comment section below. Hope, you get the difference between queue and deque. We will discuss problems on deque in upcoming posts. So, stay tuned and Keep reading.

Also read,

Leave a Reply

Your email address will not be published. Required fields are marked *