Problems

Approach # 01 Using 2 Stacks

  1. Step 1: Push S1 elements into S2.

  2. Step 2: Push the Given input element in the S1 stack.

  3. Step 3: Push S2 elements into S1.

Queue using 2 Stacks
class MyQueue {
    stack <int> s1, s2;
public:
    
    void push(int x) {
        while(!s1.empty()) {
            s2.push(s1.top());
            s1.pop();
        }

        s1.push(x);
        
        while(!s2.empty()){
            s1.push(s2.top());
            s2.pop();
        }
    }
    
    int pop() {
        if(empty())return -1;
        
        int d = s1.top();
        s1.pop();
        return d;

    }
    
    int peek() {
        if(empty()) return -1;

        return s1.top();
    }
    
    bool empty() {
        return s1.empty();
    }
};

Approach # 02 Using Single Stack

Q2: Reverse Queue using recursion

Q3: Check Palindrome

Last updated

Was this helpful?