CodingInterviews icon indicating copy to clipboard operation
CodingInterviews copied to clipboard

两个栈实现队列的问题

Open easternHong opened this issue 7 years ago • 1 comments

void push(int node) { stackIn.push(node); } //不会导致队列顺序出错吗?

easternHong avatar Nov 24 '17 08:11 easternHong

stackIn模拟入队,stackOut模拟出队。因此出队的时候,先判断stackOut是否为空,如果为空,则将stackIn的所有数据压栈到stackOut,此时stackOut栈顶元素就是模拟队列的头元素;如果不为空,就直接将stackOut栈顶元素出栈。

yantanglife avatar Jul 03 '19 01:07 yantanglife