用队列实现栈
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通队列的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
注意:
你只能使用队列的基本操作 —— 也就是 push to back、peek/pop from front、size 和 is empty 这些操作。
你所使用的语言也许不支持队列。 你可以使用 list (列表)或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
这道题的关键之处就是push的操作,如何可以让后入队的数据成为队头呢?
队列化栈有两种办法:
1,两个队列
队列是先进先出,而栈则是先进后出,可以设两个队列q1和q2,q2作为一个辅助队列,每次进栈元素先入队到q2中,这就是栈顶,然后在依次将q1中的元素依次入队到q2中,则q2的数据就是栈的存储方式,然后再交换q1和q2,当再有新的数据入栈时再重复这几步;代码如下:
class MyStack {
private:
queue<int> q1,q2;
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
q2.push(x);
while(!q1.empty())//将q1的数据依次入队到q2中
{
q2.push(q1.front());
q1.pop();
}
swap(q1,q2);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int front=q1.front();
q1.pop();
return front;
}
/** Get the top element. */
int top() {
return q1.front();
}
/** Returns whether the stack is empty. */
bool empty() {
return q1.empty();
}
};
/**
* 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();
*/
2,一个队列
想法是类似的,只要有新数据入队,只需要把该数据之前的所有数据移到该数据之后就可以了;
代码如下:
class MyStack {
queue<int> q;
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
q.push(x);
int i=q.size()-1;
//每次入队的数据一定在最后一个,只需要将最后一个数据前的所有数据依次排到该数据之后即可
while(i--)
{
q.push(q.front());
q.pop();
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int front=q.front();
q.pop();
return front;
}
/** Get the top element. */
int top() {
return q.front();
}
/** Returns whether the stack is empty. */
bool empty() {
return q.empty();
}
};
/**
* 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();
*/
栈化队列
实现一个MyQueue类,该类用两个栈来实现一个队列。
示例:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false
说明:
你只能使用标准的栈操作 – 也就是只有 push to top, peek/pop from top, size 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
两个栈实现
我们可以通过两个栈来实现队列先进先出,设栈s1和s2,先将数据入栈到s1中,s1的数据再依次出栈到s2中,这样s1的栈顶就成了s2的栈底,而s1中最先入栈的数据就成了s2的栈顶,这样就实现了先进先出;
总的来说就是s1只用来入栈,而s2只用来出栈,即s1执行的入队操作,s2是出队操作;
代码如下:
class MyQueue {
private:
stack<int> s1,s2;
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
s1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(s2.empty())//s2为空时将s1数据出栈依次入栈到s2
{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
int top=s2.top();
s2.pop();
return top;
}
/** Get the front element. */
int peek() {
if(s2.empty())
{
while(!s1.empty())
{
s2.push(s1.top());
s1.pop();
}
}
return s2.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return s1.empty()&&s2.empty();
}
};
/**
* 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();
*/
总结:这两道题虽然是栈和队列的互换,但是细细体会就会感受到不同数据结构之间的联系,对数据的处理方式还需不断理解体会;