Deque also known as double eneded queue, as name suggests is a special kind
of queue in which insertions and deletions can be done at the last as well
as at the beginning.
A link-list representation of deque is such that each node points to the next
node as well as the previous node. So that insertion and deletions take constant
time at both the beginning and the last.
Now, deque can be used to implement a stack and queue. One simply needs to
understand how deque can made to work as a stack or a queue.
The functions of deque to tweak them to work as stack and queue are list below.
Input : Stack : 1 2 3 Push(4) Output : Stack : 1 2 3 4 Input : Stack : 1 2 3 Pop() Output : Stack : 1 2
Input: Queue : 1 2 3 Enqueue(4) Output: Queue : 1 2 3 4 Input: Queue : 1 2 3 Dequeue() Output: Queue : 2 3
Stack: 7 8 Stack: 7 Queue: 12 13 Queue: 13 Size of Stack is 1 Size of Queue is 1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Implement a stack using single queue
- How to implement stack using priority queue or heap?
- Stack and Queue in Python using queue Module
- Check if a queue can be sorted into another queue using a stack
- deque::at() and deque::swap() in C++ STL
- deque::front() and deque::back() in C++ STL
- deque::clear() and deque::erase() in C++ STL
- deque::operator= and deque::operator in C++ STL
- Implement Stack using Queues
- Implement a stack using singly linked list
- Design and Implement Special Stack Data Structure | Added Space Optimized Version
- Reversing a Queue using another Queue
- Level order traversal in spiral form | Using one stack and one queue
- Difference between Circular Queue and Priority Queue
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- Difference between Stack and Queue Data Structures
- Difference between Array, Queue and Stack
- Stack | Set 3 (Reverse a string using stack)
- Sort a stack using a temporary stack
- Find maximum in stack in O(1) without using additional stack
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.