> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/232.-implement-queue-using-stacks.md).

# 232. Implement Queue using Stacks

## 1.問題

* 用stack來設計queue

![](/files/-LJdMSqTp88q7FwGntEP)

## 2.想法

* 當push時, 將所有reverseStack中的元素推入forwardStack;
* 當pop時, 將所有forwardStack中的元素推入reverseStack
* empty要確定兩個stack都是empty

## 3.程式碼

```
class MyQueue {
public:
    /** Initialize your data structure here. */
    MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        if (forwardStack.empty()) {
            while (!reverseStack.empty()) {
                forwardStack.push(reverseStack.top());
                reverseStack.pop();
            }
        }
        forwardStack.push(x);
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        if (reverseStack.empty()) {
            while (!forwardStack.empty()) {
                reverseStack.push(forwardStack.top());
                forwardStack.pop();
            }
        }
        int top = reverseStack.top();
        reverseStack.pop();
        return top;
    }
    
    /** Get the front element. */
    int peek() {
        if (reverseStack.empty()) {
            while (!forwardStack.empty()) {
                reverseStack.push(forwardStack.top());
                forwardStack.pop();
            }
        }
        int top = reverseStack.top();
        return top;
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return forwardStack.empty() && reverseStack.empty();
    }
private:
    stack<int> forwardStack;
    stack<int> reverseStack;
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * bool param_4 = obj.empty();
 */
```

## 4.Performance

![](/files/-LJdMz9U3FLFqhxn72PG)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/232.-implement-queue-using-stacks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
