Implementation of Queue using Stacks17 Mar 2025 | 5 min read Queue is a linear data structure that follows FIFO (First In First Out) principle in which insertion is performed from the rear end and the deletion is done from the front end. Stack is a linear data structure that follows LIFO (Last In First Out) principle in which both insertion and deletion are performed from the top of the stack. Let's understand the implementation of Queue using stacks. Suppose we have a queue shown as below: Now we will perform three enqueue operations shown as below: enqueue(5); enqueue(2); enqueue(3); After performing the above three enqueue operations, the queue would look like: ![]() In the above stack, we can observe that the topmost element is 3. If we perform the delete operation in the above stack, then the element 3 would be deleted from the stack. On the other hand, the deletion in Queue is performed from the front end and the front element is 5. In order to implement the Queue using Stack, we need to consider two stacks. Suppose we have two stacks named as Stack1 and Stack2 shown as below: ![]() As we can observe that above stacks are empty. Now, we will perform push operations on the Stack1. First, we will push 5, then 2 and finally we will push element 3 shown as below: ![]() Now we will pop the elements from the Stack1 one by one and push them into the Stack2 as shown as below: ![]() Once the elements are inserted into the Stack2, the topmost element is 5 so it would be popped out from the Stack 2 shown as below: ![]() Once the topmost element is popped out from the Stack2, all the elements are moved back from Stack2 to Stack 1 shown as below: ![]() There are two approaches to implement Queue using Stack:
First approach: Making a dequeue operation costly If we implement the Queue using Stack by making a dequeue operation costly means that time complexity in enqueue operation would be O(1) and the time complexity in dequeue operation would be O(n). In this case, when we insert the elements in the stack then the elements are added on the top of the stack so it takes O(1) time. In case of dequeue operation, we need to consider two stacks named as Stack1 and Stack2. First, we insert the elements in the Stack1 and then we remove all the elements from the Stack1. Once all the elements are popped from the Stack1 then they are added in the Stack2. The topmost element would be popped out from the Stack2 and then all the elements from the Stack2 are moved back to Stack1. Here, dequeue operation is performed two times on the data so time complexity is O(n). Implementation in C Output ![]() Second Approach: Making an enqueue operation costly. If we implement the Queue using Stack by making a enqueue operation costly means that time complexity in enqueue operation would be O(n) and the time complexity in dequeue operation would be O(1). First, we will consider two stacks named as stack1 and stack2. In case of enqueue operation, first all the elements will be popped from the stack1 and push it into the stack2. Once all the elements from the stack1 are pushed into the stack2, then the new element is added in the stack1. After adding the new element in the stack1, all the element are moved back from stack1 to stack2. Here, the time complexity of enqueue operation would be O(n). In stack1, the oldest element would be at the top of the stack, so time taken to perform a dequeue operation would be O(1). Implementation in C Output ![]() Next TopicImplementation of Stack using Queue |
Problem Statement: Inverse pair is an even pair of numbers [i,j], where the first element of the array is i < j < nums.length and nums[i] > nums[j] is true. If the input to the programs is two integers n and k, then return the number of...
9 min read
Let us consider the following problem to understand Segment Trees. We have an array arr[0... n-1]. We should be able to Find the sum of elements from index l to r where 0 <= l <= r <= n-1 Change the value of a specified element of the array...
6 min read
Introduction Developers and computer scientists are constantly searching for effective strategies to optimise their code in the world of problem-solving and algorithmic challenges. They have a number of potent weapons, including the "." Due to its success in resolving a variety of issues involving arrays or linked...
5 min read
What is Cartesian Tree? A cartesian tree is a type of tree data structure that is created from a set of data. A cartesian tree has to follow some of the structural variants below. The cartesian tree has to obey the min or max heap property. This property...
3 min read
Introduction In the domain of email transmission, the notion of a "" may appear confusing to the uninitiated. However, it plays an important role in ensuring the seamless delivery of your electronic mail. This article focuses on an in-depth journey into the domain of the , explaining...
4 min read
Algorithm In this article, we will discuss the cocktail sort algorithm. Cocktail sort is the variation of Bubble Sort, which traverses the list in both directions alternatively. It is different from bubble sort in the sense that bubble sort traverses the list in the forward direction...
10 min read
What is infix notation? An infix notation is a notation in which an expression is written in a usual or normal format. It is a notation in which the operators lie between the operands. The examples of infix notation are A+B, A*B, A/B, etc. As we can see...
5 min read
As the name suggests, it is the computation of a numerical or a binary component whose result can be as little as zero or as complex as ten raised to 18. The binary exponentiation concept utilizes two pillar extracts of exponentiation. We have learned in...
4 min read
Introduction: Binary Search Trees (BSTs) are powerful data structures used extensively in computer science for efficient searching, insertion, and deletion operations. One common task when working with BSTs is finding the inorder predecessor and successor for a given key. Understanding Binary Search Trees (BSTs): Before diving into inorder predecessors...
7 min read
Pythagorean Triplet problem is used to find out if there exists a Pythagorean Triplet in a given array consisting of three integers (a, b, and c) which will satisfy a² + b² = c². One of the most common problems is figuring out whether any three...
9 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India