解题思路:
(1)类比循环链表,对于每一刚进入队列的元素x,使用队列的pop、push操作,使新进入的元素x移动到队列的首部
class MyStack {
public:
/** Initialize your data structure here. */
queue<int> q;
int temp;
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
q.push(x);
for(int i=0;i<q.size()-1;i++) {
temp = q.front();
q.pop();
q.push(temp);
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
temp = q.front();
q.pop();
return temp;
}
/** 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();
*/