Idea: Esta
pregunta es para usar la idea de una pila para hacerlo. Una pila se usa para ingresar el número, y luego, cuando desea generar un número, solo necesita ver si la otra pila tiene un número, si no, ponga la pila 1 El número se pone en la pila 2, y la pila 2 es equivalente al elemento colocado en la cola.
Código:
class CQueue {
public:
CQueue() {
}
void appendTail(int value) {
st1.push(value);
return ;
}
int deleteHead() {
if(st1.size()==0 && st2.size()==0 ) return -1;
if(st2.size()){
int t = st2.top();
st2.pop();
return t;
}else{
while(!st1.empty()){
st2.push(st1.top());
st1.pop();
}
int t = st2.top();
st2.pop();
return t;
}
return 0;
}
private:
stack<int> st1,st2;
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/