Implement Stack using Queues Solutions in C++
Number 225
Difficulty Easy
Acceptance 45.3%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/implement-stack-using-queues/// Author : Hao Chen// Date : 2015-06-13class Stack {public:// Push element x onto stack.void push(int x) {nums.push(x);}// Removes the element on top of the stack.void pop() {if (!empty()) {int len = nums.size()-1;//Don't treat the "-->" is a new operator. ;-)while ( len-->0) {nums.push(nums.front());nums.pop();}nums.pop();}}// Get the top element.int top() {return nums.back();}// Return whether the stack is empty.bool empty() {return nums.empty();}private:queue<int> nums;};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/implement-stack-using-queues/description//// Author : liuyubobobo/// Time : 2018-09-29#include <iostream>#include <queue>using namespace std;/// Naive Implementation/// Time Complexity: init: O(1)/// push: O(1)/// pop: O(n)/// top: O(n)/// empty: O(1)/// Space Complexity: O(n)class MyStack {private:queue<int> q;public:/** Initialize your data structure here. */MyStack() {}/** Push element x onto stack. */void push(int x) {q.push(x);}/** Removes the element on top of the stack and returns that element. */int pop() {assert(!empty());queue<int> q2;while(q.size() > 1){q2.push(q.front());q.pop();}int ret = q.front();q.pop();while(!q2.empty()){q.push(q2.front());q2.pop();}return ret;}/** Get the top element. */int top() {assert(!empty());queue<int> q2;while(q.size() > 1){q2.push(q.front());q.pop();}int ret = q.front();q2.push(ret);q.pop();while(!q2.empty()){q.push(q2.front());q2.pop();}return ret;}/** Returns whether the stack is empty. */bool empty() {return q.empty();}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/implement-stack-using-queues/description//// Author : liuyubobobo/// Time : 2018-09-29#include <iostream>#include <queue>#include <cassert>using namespace std;/// Naive Implementation/// Using a variable to record top element:)////// Time Complexity: init: O(1)/// push: O(1)/// pop: O(n)/// top: O(1)/// empty: O(1)/// Space Complexity: O(n)class MyStack {private:queue<int> q;int topV;public:/** Initialize your data structure here. */MyStack() {}/** Push element x onto stack. */void push(int x) {q.push(x);topV = x;}/** Removes the element on top of the stack and returns that element. */int pop() {assert(!empty());queue<int> q2;while(q.size() > 1){q2.push(q.front());q.pop();}int ret = q.front();q.pop();while(!q2.empty()){q.push(q2.front());topV = q2.front();q2.pop();}return ret;}/** Get the top element. */int top() {assert(!empty());return topV;}/** Returns whether the stack is empty. */bool empty() {return q.empty();}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/implement-stack-using-queues/description//// Author : liuyubobobo/// Time : 2018-09-29#include <iostream>#include <queue>#include <cassert>using namespace std;/// Naive Implementation/// Using a variable to record top element:)////// Time Complexity: init: O(1)/// push: O(n)/// pop: O(1)/// top: O(1)/// empty: O(1)/// Space Complexity: O(n)class MyStack {private:queue<int> q;public:/** Initialize your data structure here. */MyStack() {}/** Push element x onto stack. */void push(int x) {queue<int> q2;while(!q.empty()){q2.push(q.front());q.pop();}q.push(x);while(!q2.empty()){q.push(q2.front());q2.pop();}}/** Removes the element on top of the stack and returns that element. */int pop() {assert(!empty());int ret = q.front();q.pop();return ret;}/** Get the top element. */int top() {assert(!empty());return q.front();}/** Returns whether the stack is empty. */bool empty() {return q.empty();}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/implement-stack-using-queues/description//// Author : liuyubobobo/// Time : 2018-09-29#include <iostream>#include <queue>#include <cassert>using namespace std;/// Using only one queue!////// Time Complexity: init: O(1)/// push: O(n)/// pop: O(1)/// top: O(1)/// empty: O(1)/// Space Complexity: O(n)class MyStack {private:queue<int> q;int sz = 0;public:/** Initialize your data structure here. */MyStack(): sz(0) {}/** Push element x onto stack. */void push(int x) {q.push(x);for(int i = 0; i < sz; i ++){int e = q.front();q.pop();q.push(e);}sz ++;}/** Removes the element on top of the stack and returns that element. */int pop() {assert(!empty());int ret = q.front();q.pop();sz --;return ret;}/** Get the top element. */int top() {assert(!empty());return q.front();}/** Returns whether the stack is empty. */bool empty() {return q.empty();}};int main() {return 0;}