232. 用栈实现队列
class MyQueue {
public:
MyQueue() {
}
void push(int x) {
stin.push(x);
}
int pop() {
if (stout.empty()) {
while (!stin.empty()) {
stout.push(stin.top());
stin.pop();
}
}
int result = stout.top();
stout.pop();
return result;
}
int peek() {
int result = this->pop();
stout.push(result);
return result;
}
bool empty() {
if (stout.empty() && stin.empty()) return true;
return false;
}
stack<int> stin;
stack<int> stout;
};
225. 用队列实现栈
class MyStack {
public:
MyStack() {
}
void push(int x) {
que.push(x);
}
int pop() {
int size = que.size();
size--;
while (size--) {
que.push(que.front());
que.pop();
}
int result = que.front();
que.pop();
return result;
}
int top() {
return que.back();
}
bool empty() {
if (que.empty()) return true;
return false;
}
queue<int> que;
};