其他分享
首页 > 其他分享> > 两个栈实现队列

两个栈实现队列

作者:互联网

class CQueue {
public:
stack<int> stack1;
stack<int> stack2;
CQueue() {}

void appendTail(int value) {
stack1.push(value);
}

int deleteHead() {
if (stack1.empty()) return -1;

while (!stack1.empty()){ // 1 -> 2
int tmp = stack1.top();
stack1.pop();
stack2.push(tmp);
}
// delete head
int res = stack2.top();
stack2.pop();
while (!stack2.empty()){ // 1 <- 2
int temp = stack2.top();
stack2.pop();
stack1.push(temp);
}
return res;
}
};

标签:两个,队列,pop,实现,int,push,stack2,empty,stack1
来源: https://www.cnblogs.com/wkhust/p/16192383.html