225. Implement Stack using Queues
Implement the following operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
- You must use only standard operations of a queue -- which means only
push to back,peek/pop from front,size, andis emptyoperations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {}
/** Push element x onto stack. */
void push(int x) //重点就是push进去的时候花点时间,把前面所有的重新push一次
{
int size = que.size();
que.push(x);
for (int i = 0; i < size; i++)
{
que.push(que.front());
que.pop();
}
}
/** Removes the element on top of the stack and returns that element. */
int pop()
{
int key = que.front();
que.pop();
return key;
}
/** Get the top element. */
int top()
{
return que.front();
}
/** Returns whether the stack is empty. */
bool empty() {
return que.empty();
}
private:
queue<int> que;
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* bool param_4 = obj.empty();
*/232. Implement Queue using Stacks
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.
- You must use only standard operations of a stack -- which means only
push to top,peek/pop from top,size, andis emptyoperations 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).
使用两个stack
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x)
{
while (!sk.empty())
{
tmp.push(sk.top());
sk.pop();
}
sk.push(x);
while (!tmp.empty())
{
sk.push(tmp.top());
tmp.pop();
}
}
/** Removes the element from in front of queue and returns that element. */
int pop()
{
int ret = sk.top();
sk.pop();
return ret;
}
/** Get the front element. */
int peek()
{
return sk.top();
}
/** Returns whether the queue is empty. */
bool empty()
{
return sk.empty();
}
private:
stack<int> sk;
stack<int> tmp;
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
*/
本文介绍如何仅用队列实现栈的基本操作,反之亦然。通过巧妙地利用队列和栈的标准操作,实现了所需的功能,并确保了所有操作的有效性和正确性。
5万+

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



