Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

How can we Implement a Stack using Queue in Java?



This article will discuss how to implement a Stack using a Queue in Java. 

Stack in Java

In Java, a Stack is a subclass (child class) of the Vector class, and it represents a LIFO stack of objects, which stands for Last-in-First-Out. The last element added at the top of the stack (In) can be the first element to be removed (Out) from the stack.

The following diagram will give you a clear idea about the Stack:

Stack

Queue in Java

In Java, the Queue class extends the Collection interface, and it supports the insert and remove operations using a FIFO, which stands for First-in-First-Out. The last element added to the end of the queue can become the first element to be removed from the queue.

The following diagram will give you a clear idea about the Queue:

Queue

To implement a Stack using a Queue in Java, we can use the Queue interface provided by the Java Collections Framework, which is implemented through a LinkedList. Since a stack follows LIFO (Last-In-First-Out) order and a queue follows FIFO (First-In-First-Out) order, we need to modify the behavior of the queue so that it acts like a stack.

Following are several approaches to implement a Stack using a Queue:

Implementing a Stack Using a Single Queue

This is one of the common ways to implement a stack using a single queue. In this approach, we use only one queue and modify it so that the most recently added element is always at the front of the queue.

Example

In the following example, we use the LinkedList class to create a Queue, and then implement a Stack using this Queue. -

import java.util.*;

public class StackFromQueueTest {
   //queue using linkedlist
   Queue queue = new LinkedList();
   public void push(int value) {
      int queueSize = queue.size();
      queue.add(value);
      for (int i = 0; i < queueSize; i++) {
         queue.add(queue.remove());
      }
   }
   public void pop() {
      System.out.println("An element removed from a stack is: " + queue.remove());
   }
   public static void main(String[] args) {
      StackFromQueueTest test = new StackFromQueueTest();
      test.push(10);
      test.push(20);
      test.push(30);
      test.push(40);
      System.out.println("The Stack is: " + test.queue);
      test.pop();
      System.out.println(test.queue);
   }
}

The above program implements a Stack by using a Queue and displays output as follows:

The Stack is: [40, 30, 20, 10]
An element removed from a stack is: 40
[30, 20, 10]

Using Two Queues

We can also use two queues to manage the stack operations. One queue will be used as the primary queue to store elements, while the second queue will temporarily hold elements during the push operation.

Example

The following example uses the two-queue approach to implement a stack using a queue. First, queue q1 manages the stack operation, while the second queue q2 holds elements during the push operation, ensuring the most recently added element is always at the front.

import java.util.*;

public class StackFromQueueTest {
   // create two queues
   Queue<Integer> q1 = new LinkedList<>();
   Queue<Integer> q2 = new LinkedList<>();

   public void push(int value) {
      q2.add(value);

      while (!q1.isEmpty()) {
         q2.add(q1.remove());
      }

      Queue<Integer> temp = q1;
      q1 = q2;
      q2 = temp;
   }

   public void pop() {
      if (!q1.isEmpty()) {
         System.out.println("An element removed from the stack is: " + q1.remove());
      } else {
         System.out.println("Stack is empty!");
      }
   }

   public static void main(String[] args) {
      StackFromQueueTest test = new StackFromQueueTest();
      test.push(10);
      test.push(20);
      test.push(30);
      test.push(40);

      System.out.println("The Stack is: " + test.q1);
      test.pop();
      System.out.println(test.q1);
   }
}

Following is the output of the above program:

The Stack is: [40, 30, 20, 10]
An element removed from the stack is: 40
[30, 20, 10]
Updated on: 2025-06-06T10:57:50+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements