用两个栈实现队列
来源:互联网 发布:淘宝卖家短信催好评 编辑:程序博客网 时间:2024/06/14 07:27
剑指offer上的题目,因为我刚刚看过栈和队列,所以想练几个题目,再加强对栈和队列的认识。
其实还算简单。就是第一个栈用来push,第二个栈用来delete。代码如下:
#include<iostream>#include<stack>using namespace std;class solution {public:void push(int x){si1.push(x);}int pop(){if (si2.empty()){while (!si1.empty()){si2.push(si1.top());si1.pop();}}int top = si2.top();si2.pop();return top;}private:stack<int> si1;stack<int> si2;};int main(){solution queue;queue.push(3);queue.push(8);queue.push(9);int i1 = queue.pop();cout << i1 << endl;queue.push(99);queue.push(22);int i2 = queue.pop();cout << i2 << endl;return 0;}
0 0
- 用两个栈实现队列 & 用两个队列实现栈
- 用两个队列实现栈&用两个栈实现队列
- 用两个栈实现队列 & 两个队列实现一个栈
- 两个栈实现队列&&用两个队列实现一个栈
- 用两个栈实现队列 & 两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 如何利用xss绕过360网盾做防360拦截空间!
- IE高级配置 SSL协议注册表路径
- 华为oj中级 笔画
- PWA(Progressive Web App)入门系列:(一)PWA简介
- Python垃圾回收机制
- 用两个栈实现队列
- java控制一套动效图播放、暂停和重置的方法
- python全栈
- 用python爬虫抓站的一些技巧总结
- 作为程序员的你,会选择奋斗在一线城市还是回归故乡发展?
- Oracle数据库的查询之多表查询(五)
- linux innode与block
- forEach 和 map
- 题目1035:找出直系亲属 树