225. Implement Stack using Queues

 

Implementing a Stack Using a Queue in Java

In this blog post, we’ll explore how to implement a stack using a queue in Java. This is an interesting problem that demonstrates the versatility of data structures and how they can be manipulated to achieve desired functionalities.

Introduction

A stack is a Last-In-First-Out (LIFO) data structure, where the last element added is the first one to be removed. A queue, on the other hand, is a First-In-First-Out (FIFO) data structure. The challenge here is to implement stack operations using a queue.

Approach

To implement a stack using a queue, we need to simulate the LIFO behavior using the FIFO nature of the queue. Here’s how we achieve this:

  1. Push Operation:

    • Simply add the element to the queue.
  2. Pop Operation:

    • Transfer all elements from the queue to an array.
    • Re-add all elements except the last one back to the queue.
    • Return the last element from the array.
  3. Top Operation:

    • Similar to pop, transfer all elements to an array.
    • Re-add all elements back to the queue.
    • Return the last element from the array without removing it from the queue.
  4. Empty Operation:

    • Check if the queue is empty.

Time and Space Complexity

  • Push Operation:

    • Time Complexity: (O(1)) - Adding an element to the queue is a constant time operation.
    • Space Complexity: (O(1)) - No additional space is used apart from the input element.
  • Pop Operation:

    • Time Complexity: (O(n)) - We need to transfer all elements to an array and back to the queue.
    • Space Complexity: (O(n)) - An array of size (n) is used to store the elements temporarily.
  • Top Operation:

    • Time Complexity: (O(n)) - Similar to pop, we transfer all elements to an array and back.
    • Space Complexity: (O(n)) - An array of size (n) is used to store the elements temporarily.
  • Empty Operation:

    • Time Complexity: (O(1)) - Checking if the queue is empty is a constant time operation.
    • Space Complexity: (O(1)) - No additional space is used.

The Code

Here’s the Java code for our MyStack class:

class MyStack {
    Queue<Integer> queue = new LinkedList<>();
    public MyStack() {
       
    }
   
    public void push(int x) {
        queue.add(x);
    }
   
    public int pop() {
        int[] arr = new int[queue.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = queue.poll();
        }
        for (int i = 0; i < arr.length - 1; i++) {
            queue.add(arr[i]);
        }
        return arr[arr.length - 1];
    }
   
    public int top() {
        int[] arr = new int[queue.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = queue.poll();
        }
        for (int i = 0; i < arr.length; i++) {
            queue.add(arr[i]);
        }
        return arr[arr.length - 1];
    }
   
    public boolean empty() {
        if (queue.size() <= 0) {
            return true;
        }
        return false;
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

Conclusion

Implementing a stack using a queue is a great exercise to understand the underlying principles of data structures. It shows how we can manipulate one data structure to mimic the behavior of another. This kind of problem-solving is essential for developing a deeper understanding of algorithms and data structures.

Comments

Popular posts from this blog

3Sum Closest: O(n²) Two-Pointer Magic 🚀 leetcode: 16

Kadane's Algorithm: Maximum Subarray Problem - LeetCode(53) Solution

LeetCode Problem: Implement pow(x, n) leetcode:- 50