queue 模拟 stack
2016-08-18 11:52
337 查看
class Stack { public: // Push element x onto stack. queue<int>A; queue<int>B; void push(int x) { A.push(x); } // Removes the element on top of the stack. void pop() { while(B.size()){ B.pop(); } int ans; while(A.size()){ int t=A.front(); A.pop(); if(A.size()){ B.push(t); } } while(B.size()){ A.push(B.front()); B.pop(); } } // Get the top element. int top() { while(B.size()){ B.pop(); } int ans; while(A.size()){ int t=A.front(); A.pop(); if(!A.size()) ans=t; B.push(t); } while(B.size()){ A.push(B.front()); B.pop(); } return ans; } // Return whether the stack is empty. bool empty() { return A.size()==0; } };
相关文章推荐
- ZOJ 3210 A Stack or A Queue?【模拟】
- stack 模拟 queue
- 用2个stack模拟一个queue
- 【数据结构】Stack和Queue的模拟实现
- 1/20集训一 STL E.(模拟stack queue priority_queue) I Can Guess the Data Structure!
- STL库中的stack和queue及其模拟实现
- 模板模拟实现stack和queue
- STL之stack与queue的实际应用
- Implement Queue by using Stack
- Part 82 to 85 Talking about Generic queue, stack collection class
- STL Stack/Queue/Set常用函数
- STL学习笔记6 -- 栈stack 、队列queue 和优先级priority_queue 三者比较
- ACM程序设计选修课——1044: (ds:队列)打印队列(queue模拟)
- stack,deque,queue对比
- List Queue Map Stack的遍历
- STL中stack和queue的用法
- STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)
- C++ STL--stack/queue 的使用方法
- POJ 2259 Team Queue(队列模拟)
- 【转载】Java集合的Stack、Queue、Map的遍历