Implementation of Queues using two Stack in Java
How to implement Queue using two Stack in Java ?
In this page we will discuss how to implement queue using stack in java . In other words we will be making a queue in which operations such as enqueue and dequeue are done based on push and pop operations .
Let us see how to do this thing in detail.
Code for enqueue and Dequeue
Enqueue
static void addqueue(int a) { // Move all elements from p1 to p2 while (!p1.isEmpty()) { p2.push(p1.pop()); } p1.push(a); while (!p2.isEmpty()) { p1.push(p2.pop()); } }
Dequeue
static int deletequeue() { // first stack is empty if (p1.isEmpty()) { System.out.println("empty queue"); } int a = p1.peek(); p1.pop(); return a; }
Code for Implementation Queue using two Stacks in Java
Run
import java.util.*; class Main { static class Queue { static Stack < Integer > p1 = new Stack < Integer > (); static Stack < Integer > p2 = new Stack < Integer > (); static void addqueue (int a) { // Move all elements from p1 to p2 while (!p1.isEmpty ()) { p2.push (p1.pop ()); } p1.push (a); while (!p2.isEmpty ()) { p1.push (p2.pop ()); } } // Dequeue an item from the queue static int deletequeue () { // first stack is empty if (p1.isEmpty ()) { System.out.println ("empty queue"); } // Return top of p1 int a = p1.peek (); p1.pop (); return a; } } public static void main (String[]args) { Queue queue = new Queue (); queue.addqueue (2); queue.addqueue (4); queue.addqueue (6); queue.addqueue (8); System.out.println (queue.deletequeue ()); System.out.println (queue.deletequeue ()); System.out.println (queue.deletequeue ()); System.out.println (queue.deletequeue ()); System.out.println (queue.deletequeue ()); } }
Output:
2 4 6 8 empty queue
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Stacks
- Introduction to Stack in Data Structure
Click Here - Operations on a Stack
Click Here - Stack: Infix, Prefix and Postfix conversions
Click Here - Stack Representation in –
C | C++ | Java - Representation of a Stack as an Array. –
C | C++ | Java - Representation of a Stack as a Linked List. –
C | C++ | Java - Infix to Postfix Conversion –
C | C++ | Java - Infix to prefix conversion in –
C | C++ | Java - Postfix to Prefix Conversion in –
C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
Click Here - Queues Program in C and implementation
Click Here - Implementation of Queues using Arrays | C Program
Click Here - Types of Queues in Data Structure
Click Here - Application of Queue Data Structure
Click Here - Insertion in Queues Program (Enqueuing) –
C | C++ | Java - Deletion (Removal) in Queues Program(Dequeuing) –
C | C++ | Java - Reverse a Queue –
C | C++ | Java - Queues using Linked Lists –
C | C++ | Java - Implement Queue using Stack –
C | C++ | Java - Implement Queue using two Stacks –
C | C++ | Java
Circular Queues
- Circular queue in Data Structure
Click Here - Applications of Circular Queues
Click Here - Circular queue in –
C | C++ | Java - Circular queue using Array –
C | C++ | Java - Circular Queue using Linked Lists –
C | C++ | Java
Priority Queue
Stacks
- Introduction to Stack in Data Structure
- Operations on a Stack
- Stack: Infix, Prefix and Postfix conversions
- Stack Representation in – C | C++ | Java
- Representation of a Stack as an Array. – C | C++ | Java
- Representation of a Stack as a Linked List. – C | C++ | Java
- Infix to Postfix Conversion – C | C++ | Java
- Infix to prefix conversion in – C | C++ | Java
- Postfix to Prefix Conversion in – C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
- Queues Program in C and implementation
- Implementation of Queues using Arrays | C Program
- Types of Queues in Data Structure
- Application of Queue Data Structure
- Insertion in Queues Program (Enqueuing) – C | C++ | Java
- Deletion (Removal) in Queues Program(Dequeuing) – C | C++ | Java
- Reverse a Queue – C | C++ | Java
- Queues using Linked Lists – C | C++ | Java
- Implement Queue using Stack – C | C++ | Java
- Implement Queue using two Stacks – C | C++ | Java
Circular Queues
- Circular queue in Data Structure
- Applications of Circular Queues
- Circular queue in – C | C++ | Java
- Circular queue using Array – C | C++ | Java
- Circular Queue using Linked Lists – C | C++ | Java
Login/Signup to comment