# 手撕栈队列

### 【面试题07：用两个栈实现队列】

``````class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}

/** Push element x to the back of queue. */
void push(int x) {
stack1.push(x);
}

/** Removes the element from in front of queue and returns that element. */
int pop() {
int result = peek();
stack2.pop();
return result;
}

/** Get the front element. */
int peek() {
if(stack2.size() == 0){
while(stack1.size()>0){
stack2.push(stack1.top());
stack1.pop();
}
}
return stack2.top();
}

/** Returns whether the queue is empty. */
bool empty() {
return stack1.empty() && stack2.empty();

}
private:
stack<int>stack1;
stack<int>stack2;
};

class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {
}

/** Push element x onto stack. */
void push(int x) {
queue1.push(x);
}

/** Removes the element on top of the stack and returns that element. */
int pop() {
int result = top();
queue1.pop();
queue1 = queue2;
queue2.clear();
return  result;
}

/** Get the top element. */
int top() {
while(queue1.size()>1){
queue2.push(queue1.front());
queue1.pop();
}
return queue1.front();
}

/** Returns whether the stack is empty. */
bool empty() {
return queue1.empty() && queue2.empty();
}

private:
queue<int>queue1;
queue<int>queue2;
};

class Stack {
queue<int> q;
public:
void push(int x) {
q.push(x);
for (int i=1; i<q.size(); i++) {
q.push(q.front());
q.pop();
}
}

void pop() {
q.pop();
}

int top() {
return q.front();
}

bool empty() {
return q.empty();
}
};
``````

### 【面试题21：包含min函数的栈】Min Stack

``````class MinStack {
public:
/** initialize your data structure here. */
MinStack() {

}

void push(int x) {
Stack.push(x);
if(minStack.empty() ||x < minStack.top())
minStack.push(x);
else
minStack.push(minStack.top());
}

void pop() {
if(!Stack.empty()){
Stack.pop();
minStack.pop();
}
}

int top() {
return Stack.top();
}

int getMin() {
return minStack.top();
}
private:
stack<int>Stack;
stack<int>minStack;
};
``````

### 【面试题22：栈的压入、弹出序列】

``````class Solution {
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
if(pushV.empty() || popV.empty()) return false;
stack<int>auxStack;
int pPushV=0;
int pPopV=0;
while( pPopV<popV.size()){
while(pPushV <= pushV.size() &&
(auxStack.empty()|| auxStack.top() != popV[pPopV]))
auxStack.push(pushV[pPushV++]);
if(auxStack.top() != popV[pPopV]) return false;
auxStack.pop();
pPopV++;
}
return true;
}
``````

### 【面试题59：队列的最大值】

``````class Solution {
public:
vector<int> maxInWindows(const vector<int>& num, unsigned int size)
{
vector<int>maxInWin;
if(num.size()>=size && size>=1){
deque<int>index;
for(unsigned int i=0; i<size; i++){
while(!index.empty() && num[i]>=num[index.back()])
index.pop_back();
index.push_back(i);
}
for(unsigned int i=size; i<num.size(); i++){
maxInWin.push_back(num[index.front()]);
while(!index.empty() && num[i]>=num[index.back()])
index.pop_back();
if(!index.empty() && index.front() <= int(i-size))
{//判断队头的下标是否超出size大小，如果超过，要删除队头元素
index.pop_front();
}
index.push_back(i);//将当前下标压入队尾，因为可能在未来是最大值
}
maxInWin.push_back(num[index.front()]);
}
return maxInWin;
}
};
``````

### 推荐阅读更多精彩内容

• 栈（stack）是限定仅在表尾进行插入和删除操作的线性表。队列（queue）是一种先进先出（First In Fi...
innovatorCL阅读 190评论 0 0
• 栈 1. 栈（stack）又名堆栈，它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被...
程序员高级码农阅读 4,509评论 0 11
• ———————————————回答好下面的足够了---------------------------------...
恒爱DE问候阅读 980评论 0 4
• 最全的iOS面试题及答案 iOS面试小贴士 ———————————————回答好下面的足够了-----------...
大罗Rnthking阅读 563评论 0 2
• 与有些人的相识是在紫葡萄化为初秋露水的时节，那时候总觉得时光的触角很长，我们要走很久很久，可是现在才恍然发现不过是...
喻为初阅读 641评论 45 30