用两个队列实现栈
来源:互联网 发布:哪里买古典音乐cd知乎 编辑:程序博客网 时间:2024/05/16 23:34
同样,一个主队列,一个从队列。
从队列作数据中转用。
队列直接使用STL中的queue。
源代码:
NewStack.h
#include <queue>class NewStack{public:void Pop();void Push(int data);int Top();bool Empty();private:std::queue<int> m_queue1; // 主队列std::queue<int> m_queue2; // 从队列}NewStack.cpp
#include "NewStack.h"void NewStack::Push(int data){m_queue1.push(data);}int NewStack::Top(){return m_queue1.back();}void NewStack::Pop(){// 将主队列中的前面元素依次插入到从队列中// 只留下队尾元素while (m_queue1.size() > 1){m_queue2.push(m_queue1.front());m_queue1.pop();}m_queue1.pop();// 将从队列中全部元素依次插入到主队列中while (!m_queue2.empty()){m_queue1.push(m_queue2.front());m_queue2.pop();}}bool NewStack::Empty(){return m_queue1.empty();}
0 0
- 用两个栈实现队列 & 用两个队列实现栈
- 用两个队列实现栈&用两个栈实现队列
- 用两个栈实现队列 & 两个队列实现一个栈
- 两个栈实现队列&&用两个队列实现一个栈
- 用两个栈实现队列 & 两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- test8.17
- 使用 native2ascii.exe 将java utf-8编码批量转换为 ANSI.
- 实现秒杀的几个想法(续)
- C++基类,派生类,同名覆盖原则
- shell 之进行数学运算 let expr bc 中括号
- 用两个队列实现栈
- iOS swift-1
- hdu 5461 Largest Point(水)
- HTML
- 数据库的连接代码
- CSF Framework
- 一起talk C栗子吧(第四十九回:C语言实例--最小生成树一)
- Codeforces 7C
- 关于mysql语句的一道笔试题