Skip to content

p257 스택을 이용한 큐 구현 #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tomatomatoto opened this issue Feb 15, 2022 · 1 comment
Open

p257 스택을 이용한 큐 구현 #151

tomatomatoto opened this issue Feb 15, 2022 · 1 comment

Comments

@tomatomatoto
Copy link

tomatomatoto commented Feb 15, 2022

안녕하세요? 리트코드 232번 문제 '스택을 이용한 큐 구현' 관련해서 질문드립니다.

258쪽 코드 부분에서,
peek 함수 안에서 self.output가 비어있으면
while 문을 타서 self.input에 있는 값을 하나씩 꺼내서 self.output에 저장하는 걸로 이해했습니다. (큐의 FIFO 구현을 위해)

만일 input과 output에 1, 2 값이 채워져있는 상태에서
새로운 값 3이 들어오면 어떻게 되는 것인지 궁금합니다.
제 생각에는 push함수로 새로운 값인 3을 input에 넣는 것은 될테지만,
output 에는 3이 들어있지 않기 때문에 peek 이나 pop을 했을 때는 문제가 될 것 같다는 생각이 듭니다.

258쪽 코드 윗부분에, 'output의 값이 모두 팝되기 전까지는 다시 재입력이 일어나지 않기 때문에' 라는 언급이 있는데,
혹시 3과 같은 새로운 값이 들어오지 않는 것을 말씀하시는 걸까요?
리트코드 232번 문제 본문을 확인해보았는데, 관련된 말이 없는 것 같아 답답한 마음에 질문드립니다!

감사합니다

@likejazz
Copy link
Collaborator

likejazz commented Mar 3, 2022

peek() 함수는 output이 비게되면 input에서 다시 채워넣는 역할도 겸합니다. 새로운 값 3도 나중에는 output쪽으로 이동하게 됩니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants