如何利用Stack实现Queue?

好吧,我承认这其实是个很简单的解法,在复习阿mole的414的时候,一个问题是说怎么用Perl的Shift&Unshift以及Push&Pop去实现一个FIFO的队列。

Maintain两个Stack,叫做A和B,一开始全空。

Enqueue:

Push element into Stack A

Dequeue:

If Stack B is empty, Pop element from Stack A and Push to Stack B until Stack A is empty.

Pop element from Stack B.

不给力啊,少年,这个都想不到!

 

Leave a Reply

Your email address will not be published.