leecode 解题总结:225. Implement Stack using Queues

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
using namespace std;
/*
问题:
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.
Notes:
You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

分析:用两个队列来实现栈。
设置队列1,队列2。

插入时:队列2为空,直接将当前元素插入到队列2;否则,将元素从队列2拷贝到队列1,然后将元素插入队列2,
弹出时:队列2不空,弹出队列2的元素;如果队列2为空,将元素从队列1拷贝到队列2

当前插入的要放在另一个队列的头部,如果队列2不空,插入到队列2中;如果队列2非空,将队列2
的所有元素存储到队列1,插入元素,然后再将队列1中所有元素拷到队列2
弹出的时候就弹出队列2的元素即可

输入:
15(指令个数)
push 1
top
push 2
top
push 3
empty
top
pop
empty
top
pop
empty
top
pop
empty
输出:
1
2
3
false
3
false
2
false
1
true


关键:
1 当前插入的要放在另一个队列的头部,如果队列2不空,插入到队列2中;如果队列2非空,将队列2
的所有元素存储到队列1,插入元素,然后再将队列1中所有元素拷到队列2
弹出的时候就弹出队列2的元素即可

2 这个人的解法:https://leetcode.com/problems/implement-stack-using-queues/?tab=Solutions
用一个队列,插入元素到队尾,然后将对头元素全部弹出放在队尾,牛逼
*/

class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {
        
    }
    
    /** Push element x onto stack. */
    void push(int x) {
        //压入元素到pushQueue前,先判断popQueue如果不空,然后将元素转移到pushQueue,再将当前元素插入到
		if(pushQueue.empty())
		{
			pushQueue.push(x);
		}
		//空出当前队列位置,留给当前元素
		else
		{
			int size = pushQueue.size();
			pushQueue.push(x);
			for(int i = 0 ; i < size ; i++)
			{
				int value = pushQueue.front();
				pushQueue.pop();
				pushQueue.push(value);
			}
		}
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        //弹出元素时,如果pushQueue不空,直接弹出,否则,如果为空,将辅助队列中
		if(!pushQueue.empty())
		{
			int value = pushQueue.front();
			pushQueue.pop();
			return value;
		}
		else
		{
			return 0;
		}
    }
    
    /** Get the top element. */
    int top() {
		return pushQueue.front();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
		return pushQueue.empty();
    }

private:
	queue<int> pushQueue;//压入队列,最终压入的元素要转入到这个队列存储
};



void print(vector<int>& result)
{
	if(result.empty())
	{
		cout << "no result" << endl;
		return;
	}
	int size = result.size();
	for(int i = 0 ; i < size ; i++)
	{
		cout << result.at(i) << " " ;
	}
	cout << endl;
}

void process()
{
	 int value;
	 int num;
	 vector<int> result;
	 string command;
	 while(cin >> num )
	 {
		 MyStack myStack;
		 for(int i = 0 ; i < num ; i++)
		 {
			 cin >> command;
			 if("push" == command )
			 {
				cin >> value;
				myStack.push(value);
			 }
			 else if("pop" == command)
			 {
				 myStack.pop();
			 }
			 else if("top" == command)
			 {
				 if(!myStack.empty())
				 {
					cout << myStack.top() << endl;
				 }
				 else
				 {
					 cout << "empty" << endl;
				 }
			 }
			 else if("empty" == command)
			 {
				 if(myStack.empty())
				 {
					 cout << "true" << endl;
				 }
				 else
				 {
					 cout << "false" << endl;
				 }
			 }
		 }
	 }
}

int main(int argc , char* argv[])
{
	process();
	getchar();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值