面试题9:用两个栈实现队列 原题链接
class CQueue {
stack<int> sk1, sk2;
public:
CQueue() {
}
void appendTail(int value) {
sk1.push(value);
}
int deleteHead() {
if(sk2.empty()){
if(sk1.empty())
return -1;
while(!sk1.empty()){
sk2.push(sk1.top());
sk1.pop();
}
}
int val = sk2.top();
sk2.pop();
return val;
}
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/