Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.
deque::pop_front()
pop_front() function is used to pop or remove elements from a deque from the front. The value is removed from the deque from the beginning, and the container size is decreased by 1.
Syntax :
dequename.pop_front()
Parameters :
No value is needed to pass as the parameter.
Result :
Removes the value present at the front
of the given deque named as dequename
Examples:
Input : mydeque = 1, 2, 3
mydeque.pop_front();
Output : 2, 3
Input : mydeque = 3, 4, 1, 7, 3
mydeque.pop_front();
Output : 4, 1, 7, 3
Errors and Exceptions
- No-Throw-Guarantee – if an exception is thrown, there are no changes in the container
- If the deque is empty, it shows undefined behavior.
CPP
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque< int > mydeque;
mydeque.push_front(3);
mydeque.push_front(2);
mydeque.push_front(1);
mydeque.pop_front();
for ( auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
}
|
Output:
2 3
Application: Input an empty deque with the following numbers and order using push_front() function and print the reverse of the deque.
Input : 1, 2, 3, 4, 5, 6, 7, 8
Output: 8, 7, 6, 5, 4, 3, 2, 1
CPP
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque< int > mydeque{}, newdeque{};
mydeque.push_front(8);
mydeque.push_front(7);
mydeque.push_front(6);
mydeque.push_front(5);
mydeque.push_front(4);
mydeque.push_front(3);
mydeque.push_front(2);
mydeque.push_front(1);
while (!mydeque.empty()) {
newdeque.push_front(mydeque.front());
mydeque.pop_front();
}
for ( auto it = newdeque.begin(); it != newdeque.end(); ++it)
cout << ' ' << *it;
}
|
Output:
8 7 6 5 4 3 2 1
Time complexity: O(1)
Auxiliary Space: O(1)
deque::pop_back()
pop_back() function is used to pop or remove elements from a deque from the back. The value is removed from the deque from the end, and the container size is decreased by 1.
Syntax :
dequename.pop_back()
Parameters :
No value is needed to pass as the parameter.
Result :
Removes the value present at the end or back
of the given deque named as dequename
Examples:
Input : mydeque = 1, 2, 3
mydeque.pop_back();
Output : 1, 2
Input : mydeque = 3, 4, 1, 7, 3
mydeque.pop_back();
Output : 3, 4, 1, 7
Errors and Exceptions
- No-Throw-Guarantee – if an exception is thrown, there are no changes in the container
- If the deque is empty, it shows undefined behaviour.
CPP
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque< int > mydeque;
mydeque.push_front(5);
mydeque.push_front(4);
mydeque.push_front(3);
mydeque.push_front(2);
mydeque.push_front(1);
mydeque.pop_back();
for ( auto it = mydeque.begin(); it != mydeque.end(); ++it)
cout << ' ' << *it;
}
|
Output:
1 2 3 4
Application :
Input an empty deque with the following numbers and order using push_front() function and print the reverse of the deque.
Input : 1, 20, 39, 43, 57, 64, 73, 82
Output : 82, 73, 64, 57, 43, 39, 20, 1
CPP
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque< int > mydeque, newdeque;
mydeque.push_front(82);
mydeque.push_front(73);
mydeque.push_front(64);
mydeque.push_front(57);
mydeque.push_front(43);
mydeque.push_front(39);
mydeque.push_front(20);
mydeque.push_front(1);
while (!mydeque.empty()) {
newdeque.push_back(mydeque.back());
mydeque.pop_back();
}
for ( auto it = newdeque.begin(); it != newdeque.end(); ++it)
cout << ' ' << *it;
}
|
Output:
82 73 64 57 43 39 20 1
Time complexity: O(1)
Auxiliary Space: O(1)
Let us see the differences in a tabular form -:
| deque::pop_front() | deque::pop_back() |
1. | It is used to remove the first element in the deque container | It is used to remove the last element in the deque container |
2. | Its syntax is -: pop_front(); | Its syntax is -: pop_back(); |
3. | It does not take any parameters. | It does not take any parameters. |
4. | It does not have any return value. | It does not have any return value. |
5. | Its complexity is constant. | Its complexity is constant. |