Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created April 16, 2018 15:05
Show Gist options
  • Save s4553711/deaa7c930da6c67bbd63ca3af8827a15 to your computer and use it in GitHub Desktop.
Save s4553711/deaa7c930da6c67bbd63ca3af8827a15 to your computer and use it in GitHub Desktop.
class MyStack {
public:
queue<int> q;
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
q.push(x);
for(int i = 1; i < q.size(); i++) {
q.push(q.front());
q.pop();
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int top1 = q.front();
q.pop();
return top1;
}
/** 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();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment