[google面试题]Describe a queue data structure that is implemented using one or more stacks.
Good answer:You can use two stacks:an "incoming" stack and an "outgoing" stack.The enqueue and dequeue operations would look like this(in Java):Stack in;Stack out;void enqueue(int value) { while (!out.isEmpty()) in.push(out.pop()); in.push(value);}int dequeue() { while (!in.isEmpty()) out.push(in.pop()); return out.pop();}
页:
[1]