Ch3-5: implement myqueue with 2 stacks

来源:互联网 发布:拼图软件哪个好四宫格 编辑:程序博客网 时间:2024/05/21 08:59

since Stack is LIFO, Queue is FIFO;

in order to implement LIFO, need to have a stack1 to buff the data and back to stack2 for pop. it is just like cooling the hot water with 2 cups, it can reverse the order.

But one thing need to notice. we only reverse the order if we have no water in stack2 for drink. so only in pushing, we directly pour water in stack1. Other cases, we need to check if stack2 is empty so as to reverse hot water in stack1 to 2.

It save time.


Output:

Executing the program....$demo 0 91 1010 0


0 0
原创粉丝点击