225. Implement Stack using Queues
来源:互联网 发布:c语言char怎么输出 编辑:程序博客网 时间:2024/06/05 11:31
MyStack() { } /** Push element x onto stack. */ void push(int x) { q2.push(x); while(q2.size()>1){ q1.push(q2.front()); q2.pop(); } } /** Removes the element on top of the stack and returns that element. */ int pop() { top(); int x=q2.front(); q2.pop(); return x; } /** Get the top element. */ int top() { if(q2.empty()){ for(int i=0;i<(int)q1.size()-1;i++){ q1.push(q1.front()); q1.pop(); } q2.push(q1.front()); q1.pop(); } return q2.front(); } /** Returns whether the stack is empty. */ bool empty() { return q1.empty()&&q2.empty(); }private: queue<int>q1,q2;};
class MyStack {public: /** Initialize your data structure here. */ MyStack() { } /** Push element x onto stack. */ void push(int x) { queue<int> tmp; while(!q.empty()){ tmp.push(q.front()); q.pop(); } q.push(x); while(!tmp.empty()){ q.push(tmp.front()); tmp.pop(); } } /** Removes the element on top of the stack and returns that element. */ int pop() { int x=q.front(); q.pop(); return x; } /** Get the top element. */ int top() { return q.front(); } /** Returns whether the stack is empty. */ bool empty() { return q.empty(); }private: queue<int>q;};
阅读全文
0 0
- 225.Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 225. Implement Stack using Queues
- 11.2
- [Keyword]关键字
- DHCP协议原理及应用
- HDU2570-迷瘴
- vb.net 教程 8-3 数据库操作9-2
- 225. Implement Stack using Queues
- JAVA学习笔记22——SpingMVC框架第二章
- codeforces 208A Dubstep
- 状态模式的简单使用
- 使用git搭建简单代码服务器
- github上传项目(接上一篇创建新项目)
- 进度条等在外部刷新功能
- 互联网行业
- 利用GDB调试多线程core文件