LeetCode Implement Stack Using Queues

Description

Implement the following operations of a stack using queues.

  • push(x) – Push element x onto stack.
  • pop() – Removes the element on top of the stack.
  • top() – Get the top element.
  • empty() – Return whether the stack is empty.

The original problem is here.

The original code is here.

My Solution

I solve this problem in C++, as below:

/*Implement Stack using Queues 
*Author: shuaijiang
*Email: zhaoshuaijiang8@gmail.com
*/
#include<iostream>
#include<queue>
#include<stdlib.h>

class Stack {
public:
    queue<int> myQueue;
    // Push element x onto stack.
    void push(int x) {
        myQueue.push(x);
    }

    // Removes the element on top of the stack.
    void pop() {
        queue<int> queue2;
        int num;
        while(myQueue.size() > 1){
            num = myQueue.front();
            myQueue.pop();
            queue2.push(num);
        }
        myQueue = queue2;
    }

    // Get the top element.
    int top() {
        return myQueue.back();
    }

    // Return whether the stack is empty.
    bool empty() {
        return myQueue.empty();
    }
};

Note

For the pop, we need pop all the data to a new queue except the final number.


LeetCode Implement Stack Using Queues
http://zhaoshuaijiang.com/2015/07/10/leetcode_implement_stack_using_queues/
作者
shuaijiang
发布于
2015年7月10日
许可协议