Implementation of Queue using Stack in Java

Implementation of Queue using Stack

Implementation of Queue using Stack in java is being explained in this article. Queue is a linear data structure that follows First In First our principle in which insertion is perform from rear end and deletion is done from the front end. In this article, we will discuss the implementation of Queue using Stack in Java.

implementing linked list using stack

Steps for implementing Queue using two stacks in java:

  • Following is the implementation for the java:

    • During Enqueue operation, we can straight away push the element into the stack.

    • During Dequeue operation,

      • Pop all the elements from Main Stack recursively until Stack size is equal to 1.
      • If Stack size = 1, Pop item from Stack, and return the same item.
      • Push all popped element back to Stack.
implementing queue using stack

Algorithm for implementing Queue using stack​

enqueue(data)

  • While stack1 is not empty, push everything from stack1 to stack2.
  • Push value to stack1.
  • Push everything back to stack1.

dequeue()

  • In this function first we will check if the queue has some element or not.
  • If our queue is empty then we cannot delete any element from it.
  • Else if there element in the queue we will pop an element from the top of stack1.

print()

  • Initialize a for loop from zero till the top.
  • Print the element at every successful iteration.
  • In this way we can display our queue.

Code for Implementation Queue using Stack in Java

Run
import java.util.*;
public class Main
{
  static class QueueUsingSingleStack
  {

    Stack < Integer > stack = new Stack <> ();

    private void enqueue (int i)
    {
      stack.push (i);
    }

    private int dequeue ()
    {
      if (stack.size () == 0)
	{
	  System.out.println ("Queue is Empty");
	  return 0;
	}
      if (stack.size () == 1)
	return stack.pop ();

      int data = stack.pop ();
      int retVal = dequeue ();
      stack.push (data);
      return retVal;
    }
  }
  public static void main (String[]args) throws Exception
  {
    QueueUsingSingleStack queue = new QueueUsingSingleStack ();
      queue.enqueue (10);
      queue.enqueue (20);
      queue.enqueue (30);
      queue.enqueue (40);

      System.out.println (queue.dequeue ());
      System.out.println (queue.dequeue ());
      System.out.println (queue.dequeue ());
      System.out.println (queue.dequeue ());
      System.out.println (queue.dequeue ());

  }

}

Output:

10
20
30
40
Queue is Empty

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