clueless coding // TODO: be smarter

LeetCode Implement Stack using Queues: CPP Solution



TL;DR Code Solution


/** Initialize your data structure here. */
queue<int> que;
MyStack() {

}

/** Push element x onto stack. */
void push(int x) {
    que.push(x);
    for (int i = 0; i < que.size() - 1; i++) {
        que.push(que.front());
        que.pop();
    }
}

/** Removes the element on top of the stack and returns that element. */
int pop() {
    int x = que.front();
    que.pop();
    return x;
}

/** Get the top element. */
int top() {
    int x = que.front();
    return x;
}

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