Implement Stack using Queues && Implement Queue using Stacks
2015-07-23 01:42
671 查看
Implement Stack using Queues
Implement Queue using Stacks
class Stack { private: queue<int> q; public: // Push element x onto stack. void push(int x) { queue<int> t; while (!q.empty()) { t.push(q.front()); q.pop(); } q.push(x); while(!t.empty()) { q.push(t.front()); t.pop(); } } // Removes the element on top of the stack. void pop() { q.pop(); } // Get the top element. int top() { while (!q.empty()) return q.front(); } // Return whether the stack is empty. bool empty() { return q.empty(); } };
Implement Queue using Stacks
class Queue { private: stack<int> s; public: // Push element x to the back of queue. void push(int x) { stack<int> t; while (!s.empty()) { t.push(s.top()); s.pop(); } s.push(x); while(!t.empty()) { s.push(t.top()); t.pop(); } } // Removes the element from in front of queue. void pop(void) { s.pop(); } // Get the front element. int peek(void) { if (!s.empty()) return s.top(); } // Return whether the queue is empty. bool empty(void) { return s.empty(); } };
相关文章推荐
- CF 559 B Equivalent Strings(字符串匹配)
- Codeforces Round #313 (Div. 1) B. Equivalent Strings DFS暴力
- uva 11235 - Frequent values
- DuiLib介绍及其消息处理剖析
- UIAlertView自带password输入框
- 百度Ueditor uParse功能Bug,生成全局CSS: li,影响全局样式, 造成网页其它部分显示混乱解决方案
- easyui-combobox 多选框编辑
- 关于Android Stuido 打jar, aar 和使用aar
- UIViewController的生命周期及iOS程序执行顺序
- 2015 Multi-University Training Contest 1 OO’s Sequence hdu5288
- UIImageView 个人学习总结
- 交互设计的流程
- UI5 如何修改默认的style
- Ubuntu 14.04开启root账号,禁用Guest账号
- Unique Binary Search Trees
- UI5的语言问题
- MySQL - Incorrect string value: for column 'name' at row 1
- Android显示系统之View与SurfaceView更新屏幕的区别.对于View.则是在UI主线程中更新画面.SurfaceView更新屏幕,是在非UI线程(主线程)中实现SurfaceHolde
- EasyUI DataGrid可编辑单元格
- EasyUI DataGrid可编辑单元格