准备两个栈,一个push栈,一个pop栈。appendTail操作始终是向push栈中push元素,deleteHead操作始终是从pop栈pop元素,要求pop栈是从push倒过来,pop只要有元素,push就不倒。
class CQueue {
private:
stack<int> Spush,Spop;
public:
CQueue() {
}
void appendTail(int value) {
Spush.push(value);
}
int deleteHead() {
int res=-1;
if(!Spop.empty()) {
res=Spop.top();
Spop.pop();
}
else if(!Spush.empty()) {
while(!Spush.empty()) {
Spop.push(Spush.top());
Spush.pop();
}
res=Spop.top();
Spop.pop();
}
return res;
}
};