用两个栈实现队列
来源:互联网 发布:.net core 2.0 数据库 编辑:程序博客网 时间:2024/06/09 22:46
//// main.cpp// Test//// Created by Bazinga on 2017/3/15.// Copyright © 2017年 Bazinga. All rights reserved.//#include <iostream>#include <stack>#include <exception>#include <queue>using namespace std;template<typename T> class CQueue{public: CQueue(void); ~CQueue(void); void appendTail(const T& node); T deleteHead();private: stack<T> stack1; stack<T> stack2;};template <typename T> CQueue<T>::CQueue(void){}template <typename T> CQueue<T>::~CQueue(void){}template<typename T> void CQueue<T>::appendTail(const T & node){ stack1.push(node);}template<typename T> T CQueue<T>::deleteHead(){ if (stack2.size()<=0) { while(stack1.size()>0){ int data = stack1.top(); stack2.push(data); stack1.pop(); } } if(stack2.size() == 0){ throw string("queue is empty"); } T head = stack2.top(); stack2.pop(); return head;}int main(){ CQueue<int > list ; list.appendTail(1); list.appendTail(2); int value = list.deleteHead(); cout<<value<<endl;}
0 0
- 用两个栈实现队列 & 用两个队列实现栈
- 用两个队列实现栈&用两个栈实现队列
- 用两个栈实现队列 & 两个队列实现一个栈
- 两个栈实现队列&&用两个队列实现一个栈
- 用两个栈实现队列 & 两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个队列实现栈
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 用两个栈实现队列
- 浮水法
- SSH学习之Hibernate的配置文件
- 山寨美图秀秀的美肤功能----实现过程
- uboot下init_sequence_f函数之reserve_malloc
- 精通23种设计模式——观察者模式
- 用两个栈实现队列
- Random类
- GreenDao的使用
- IBM MB(IIB)访问数据库的消息流开发示例
- 计算机运行快捷键
- struts2中使用validate校验数据
- Java 并发开发:Lock 框架详解
- C语言二进制文件读取
- 如何在PDF文件中插入其他PDF页面