堆栈使用两个队列

Jag*_*gan 0 data-structures

可能重复:
使用两个队列实现堆栈

我想要在时间复杂度方面使用两个队列实现堆栈的最有效方式

Dav*_*Liu 5

我不明白为什么你需要两个队列.我已经在这里和dupe线程中看到了答案,你只需要一个队列即可.

从欺骗线程来看,它有两个版本,一个优化推送,另一个优化pop.

推送优化:

push:  
    enqueue in queue  
pop:  
    n = queue size.
    dequeue an object, and enqueue it immediately after. Do this n - 1 times.
    dequeue object, and return this.
Run Code Online (Sandbox Code Playgroud)

流行优化:

push:
    enqueue in queue
    n = queue size.
    dequeue an object, and enqueue it immediately after. Do this n - 1 times.
pop:
    dequeue from queue and return.
Run Code Online (Sandbox Code Playgroud)

然后,我不明白为什么你曾经想要这样做.责备你的教授让你浪费你的时间与无意义的编程问题.