Implement Stack using two Queues in java

ghz 10months ago ⋅ 124 views

You can implement a stack using two queues in Java by using the following approach:

import java.util.LinkedList;
import java.util.Queue;

public class StackUsingQueues<T> {

    private Queue<T> primaryQueue;
    private Queue<T> secondaryQueue;

    public StackUsingQueues() {
        primaryQueue = new LinkedList<>();
        secondaryQueue = new LinkedList<>();
    }

    // Push element onto the stack
    public void push(T element) {
        // Add the new element to the primary queue
        primaryQueue.add(element);
    }

    // Pop element from the stack
    public T pop() {
        if (isEmpty()) {
            throw new IllegalStateException("Stack is empty");
        }

        // Move all elements except the last one from primary queue to secondary queue
        while (primaryQueue.size() > 1) {
            secondaryQueue.add(primaryQueue.remove());
        }

        // Get the last element from primary queue (which is the top of the stack)
        T poppedElement = primaryQueue.remove();

        // Swap primaryQueue and secondaryQueue
        Queue<T> temp = primaryQueue;
        primaryQueue = secondaryQueue;
        secondaryQueue = temp;

        return poppedElement;
    }

    // Peek the top element of the stack without removing it
    public T peek() {
        if (isEmpty()) {
            throw new IllegalStateException("Stack is empty");
        }

        // Move all elements except the last one from primary queue to secondary queue
        while (primaryQueue.size() > 1) {
            secondaryQueue.add(primaryQueue.remove());
        }

        // Get the last element from primary queue (which is the top of the stack)
        T peekedElement = primaryQueue.peek();

        // Move the peeked element from primary queue to secondary queue
        secondaryQueue.add(primaryQueue.remove());

        // Swap primaryQueue and secondaryQueue
        Queue<T> temp = primaryQueue;
        primaryQueue = secondaryQueue;
        secondaryQueue = temp;

        return peekedElement;
    }

    // Check if the stack is empty
    public boolean isEmpty() {
        return primaryQueue.isEmpty();
    }

    // Get the size of the stack
    public int size() {
        return primaryQueue.size();
    }

    public static void main(String[] args) {
        StackUsingQueues<Integer> stack = new StackUsingQueues<>();
        
        stack.push(1);
        stack.push(2);
        stack.push(3);

        System.out.println("Size of stack: " + stack.size());
        System.out.println("Top element of stack: " + stack.peek());
        
        System.out.println("Popped element: " + stack.pop());
        System.out.println("Size of stack: " + stack.size());
        System.out.println("Top element of stack: " + stack.peek());
    }
}

This implementation maintains two queues (primaryQueue and secondaryQueue). The push operation simply adds elements to the primaryQueue. The pop operation moves all elements except the last one from the primaryQueue to the secondaryQueue, removes the last element (top of the stack) from the primaryQueue, and swaps the primaryQueue and secondaryQueue. The peek operation is similar to pop but it does not remove the element from the stack.