Implementation of Queues using two Stack in Java

implementing queue using two stack

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.

Basic Operations of Queue

  • Enqueue: When we want to add an element to the end of the queue
  • Dequeue: when we want to Remove an element from the front of the queue
  • IsEmpty: Check if the queue is empty
  • IsFull: Check if the queue is full
  • Peek: Get the value of the front of the queue without removing it
implementing queue using two stack

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

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

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java