邢城 发表于 2013-7-30 17:25:11

[google面试题]Describe
 a
 queue
 data 
structure 
that
 is
 implemented
 using
 one
 or
 more
 stacks.



孙宏雷 发表于 2013-7-30 17:45:47

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]
查看完整版本: [google面试题]Describe
 a
 queue
 data 
structure 
that
 is
 implemented
 using
 one
 or
 more
 stacks.