# Implement Queue using Stacks

문제 : <https://leetcode.com/problems/implement-queue-using-stacks/>

<figure><img src="/files/6yTxQVGUsCDjceE9c1dS" alt=""><figcaption></figcaption></figure>

문제에서 스택 두 개 쓰라길래 어떻게 하라는지 모르겠어서 무시하고 일단 해봤는데 통과는 함\
그것도 더 빠르게. 일종의 치팅이니까 일단 답을 보기로 함. gpt에게 물어봄.

```python
    def dequeue(self):
        """
        큐에서 데이터를 제거 (FIFO)
        """
        if not self.stack_out:
            # stack_out이 비어있으면 stack_in에서 데이터를 옮긴다
            while self.stack_in:
                self.stack_out.append(self.stack_in.pop())
        if self.stack_out:
            return self.stack_out.pop()
        else:
            return None  # 큐가 비어있을 때
```

LIFO인 스택에서 pop을 하면서 다시 LIFO인 다른 스택에 넣으면\
원래 들어왔던 순서대로는 FIFO가 되니까\
deque를 요청했을 때 다른 스택에 pop하면서 넣은 뒤에 하나 빼기\
만약 다른 스택에 이미 뭐가 있었다면 일단 하나 빼기

천잰듯


---

# Agent Instructions: 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://lazyartisan.gitbook.io/note/main-page/algorithm/undefined-1/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.
