题目如下
解题思路
题目已经告诉我们思路了,用两个stack堆栈来实现添加和删除
直接上代码
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);
}
int res = stack2.top();// delete head
stack2.pop();
while (!stack2.empty()){ // 1 <- 2
int temp = stack2.top();
stack2.pop();
stack1.push(temp);
}
return res;
}
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/