> For the complete documentation index, see [llms.txt](https://dsa-cpp.gitbook.io/nafees/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dsa-cpp.gitbook.io/nafees/queue/problems.md).

# Problems

## `Q1:`   [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)

## Approach # 01 <mark style="color:red;">Using 2 Stacks</mark>

1. `Step 1:` Push <mark style="color:red;">S1</mark> elements into <mark style="color:red;">S2</mark>.
2. `Step 2:` Push the Given input element in the <mark style="color:red;">S1</mark> stack.
3. `Step 3:` Push <mark style="color:red;">S2</mark> elements into <mark style="color:red;">S1</mark>. &#x20;

{% code title="Queue using 2 Stacks" %}

```cpp
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();
    }
};
```

{% endcode %}

## Approach # 02 <mark style="color:red;">Using Single Stack</mark>

```cpp
class MyQueue {
    stack <int> s1;
public:
    
    void push(int x) {
        insertAtBottom(x);
    }
    
    void insertAtBottom(const int x) {
        // base condition
        if(s1.empty()) {
            s1.push(x);
            return ;
        }
        
        int temp = s1.top();
        s1.pop();
        
        // recursive call
        insertAtBottom(x);
        
        s1.push(temp);
    }
    
    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();
    }
};
```

## `Q2:`   Reverse Queue using recursion

```cpp
void reverseQueue(queue<int>& q1) {
    if(q1.empty()) return ;

    int temp = q1.front();
    q1.pop();
    reverseQueue(q1);

    q1.push(temp);
}

void print(queue<int> q) {
    while(!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }cout << endl;
} 
```

## `Q3:`  Check Palindrome

```cpp
bool checkPalindrome(string s) {
    queue<char> q1, q2;
    int i = 0;6
    for (; i < s.size()/2; i++)
    {
        q1.push(s[i]);
    }
    
    if(s.size()&1) i++;

    for(int j = s.size()-1; j >= i; j--) {
        q2.push(s[j]);
    }    
    return q1 == q2;
}
```
