Stack Implementation using Deque Last Updated : 05 Mar, 2025 Comments Improve Suggest changes Like Article Like Report A doubly ended queue or deque allows insertion and deletion at both ends. In a stack, we need to do insertions and deletions at one end only. We can use either end of deque (front or back) to implement a stack, DequeIn the below implementation, we use back (or rear) of stack to do both insertions and deletions. Please note that the time complexities of all the operations (insertions and deletions at both ends) in a deque is O(1). So the time complexity of the below implementation is O(1) only. In Java, the deque implementation of stack is preferred over standard Stack class, because the standard Stack class is legacy class and mainly designed for multi threaded environment.In Python, there is no standard stack class, so we can either use list or deque. The deque implementation is preferred because it is optimized for insertion and deletion at ends. C++ #include <iostream> #include <deque> using namespace std; int main() { deque<int> stack; stack.push_back(10); stack.push_back(20); stack.push_back(30); cout << stack.back() << " popped from deque" << endl; stack.pop_back(); cout << "Top element is: " << stack.back() << endl; return 0; } Java import java.util.*; class GfG { public static void main(String[] args) { Deque<Integer> stack = new ArrayDeque<>(); stack.push(10); stack.push(20); stack.push(30); System.out.println(stack.pop() + " popped from stack"); System.out.println("Top element is: " + stack.peek()); } } Python from collections import deque stack = deque() stack.append(10) stack.append(20) stack.append(30) print(f'{stack.pop()} popped from stack') print(f'Top element is: {stack[-1]}' if stack else 'Stack is empty') Output30 popped from deque Top element is: 20 Comment More infoAdvertise with us Next Article Stack Implementation using Deque K kartik Follow Improve Article Tags : Stack DSA deque Practice Tags : DequeStack Similar Reads Implement Stack and Queue using Deque Deque also known as double ended 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 insertio 15 min read Implementation of Deque using Array - Simple A Deque (Double-Ended Queue) is a data structure that allows insertion and deletion of elements at both ends (front and rear). This flexibility makes it more versatile than a regular queue, where insertion and deletion can only happen at one end. In this article, we will explore how to implement a d 7 min read Deque Implementation in Python A deque (double-ended queue) is a data structure that allows insertion and deletion from both the front and rear in O(1) time in optimized implementations. Unlike regular queues, which are typically operated on using FIFO (First In, First Out) principles, a deque supports both FIFO and LIFO (Last In 6 min read Implementation of Deque using doubly linked list A Deque (Double-Ended Queue) is a data structure that allows adding and removing elements from both the front and rear ends. Using a doubly linked list to implement a deque makes these operations very efficient, as each node in the list has pointers to both the previous and next nodes. This means we 9 min read Implement a stack using single queue We are given a queue data structure, the task is to implement a stack using a single queue.Also Read: Stack using two queuesThe idea is to keep the newly inserted element always at the front of the queue, preserving the order of previous elements by appending the new element at the back and rotating 5 min read Like