Implement the following operations of a queue using stacks.
push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
跟前面用队列实现堆原理是差不多的。
<

本文介绍了如何使用栈来实现队列的基本操作,包括push、pop、peek和empty。遵循仅使用标准栈操作的原则,讨论了在不同编程语言中如何模拟栈,并强调所有操作都是在合法状态下进行的。
订阅专栏 解锁全文
645

被折叠的 条评论
为什么被折叠?



