Stack Implementation using Deque Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 22 Likes 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 K kartik Follow 22 Improve K kartik Follow 22 Improve Article Tags : Stack DSA deque Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like