Implement Queue using Stacks Solutions in C++
Number 232
Difficulty Easy
Acceptance 49.7%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/implement-queue-using-stacks/// Author : Hao Chen// Date : 2015-07-16class Queue {public:// Push element x to the back of queue.void push(int x) {s1.push(x);}// Removes the element from in front of queue.void pop(void) {transfer(s1, s2);s2.pop();transfer(s2, s1);}// Get the front element.int peek(void) {transfer(s1, s2);int ret = s2.top();transfer(s2, s1);return ret;}// Return whether the queue is empty.bool empty(void) {return s1.empty();}private:stack<int> s1, s2;void transfer(stack<int>& s1, stack<int>& s2) {while(!s1.empty()){s2.push(s1.top());s1.pop();}}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/implement-queue-using-stacks/description//// Author : liuyubobobo/// Time : 2018-09-14#include <iostream>#include <stack>using namespace std;/// Two Stacks/// The Queue front is the top of Stack////// Time Complexity: push: O(n)/// pop: O(1)/// peek: O(1)/// empty: O(1)////// Space Complexity: O(n)class MyQueue {private:stack<int> s;public:/** Initialize your data structure here. */MyQueue() {}/** Push element x to the back of queue. */void push(int x) {stack<int> s2;while(!s.empty()){s2.push(s.top());s.pop();}s.push(x);while(!s2.empty()){s.push(s2.top());s2.pop();}}/** Removes the element from in front of queue and returns that element. */int pop() {int ret = s.top();s.pop();return ret;}/** Get the front element. */int peek() {return s.top();}/** Returns whether the queue is empty. */bool empty() {return s.empty();}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/implement-queue-using-stacks/description//// Author : liuyubobobo/// Time : 2018-05-14#include <iostream>#include <stack>using namespace std;/// Two Stacks/// The Queue tail is the top of Stack////// Time Complexity: push: O(1)/// pop: O(n)/// peek: O(n)/// empty: O(1)////// Space Complexity: O(n)class MyQueue {private:stack<int> s;public:/** Initialize your data structure here. */MyQueue() {}/** Push element x to the back of queue. */void push(int x) {s.push(x);}/** Removes the element from in front of queue and returns that element. */int pop() {stack<int> s2;while(!s.empty()){s2.push(s.top());s.pop();}int ret = s2.top();s2.pop();while(!s2.empty()){s.push(s2.top());s2.pop();}return ret;}/** Get the front element. */int peek() {stack<int> s2;while(!s.empty()){s2.push(s.top());s.pop();}int ret = s2.top();while(!s2.empty()){s.push(s2.top());s2.pop();}return ret;}/** Returns whether the queue is empty. */bool empty() {return s.empty();}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/implement-queue-using-stacks/description//// Author : liuyubobobo/// Time : 2018-09-14#include <iostream>#include <stack>using namespace std;/// Two Stacks/// All the elements store in the two stacks all together////// Time Complexity: push: O(1)/// pop: O(1) in average/// peek: O(1)/// empty: O(1)////// Space Complexity: O(n)class MyQueue {private:stack<int> s1, s2;int front;public:/** Initialize your data structure here. */MyQueue() {}/** Push element x to the back of queue. */void push(int x) {if(s1.empty())front = x;s1.push(x);}/** Removes the element from in front of queue and returns that element. */int pop() {if(s2.empty()){while(!s1.empty()){s2.push(s1.top());s1.pop();}}int ret = s2.top();s2.pop();return ret;}/** Get the front element. */int peek() {if(!s2.empty())return s2.top();return front;}/** Returns whether the queue is empty. */bool empty() {return s1.empty() && s2.empty();}};int main() {return 0;}