剑指Offer系列---(9)用两个栈实现队列和用两个队列实现栈
来源:互联网 发布:python验证码识别 编辑:程序博客网 时间:2024/05/15 03:43
1.题目描述:
用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能。
template<typename T> class CQueue
{
public:
CQueue(void);
~CQueue(void);
void appendTail(const T& node);
T deleteHead();
private:
stack<T>stack1;
stack<T>stack2;
};
2.分析
1)删除操作:
a.先判断stack2是否为空,若其不为空,则从中弹出元素;若为空,则将stack1中元素依次弹出并存入stack2中。
b.重复步骤a。
2)插入操作:
直接插入stack1即可。
3.源代码:
// Copyright (c) 2015年 skewrain. All rights reserved.#include <iostream>#include <stack>#include <exception>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) {//stack2为空时,将stack1中所有元素存入stack2中。 T &data = stack1.top(); stack1.pop(); stack2.push(data); } if (stack2.size()==0) { throw new exception(); } T &head = stack2.top(); stack2.pop(); return head; }void Test(char actual,char expected){ if(actual == expected) cout<<"Test passed."<<endl; else cout<<"Test failed."<<endl;}int main(int argc,char *argv[]){ CQueue<char> queue; queue.appendTail('a'); queue.appendTail('b'); queue.appendTail('c'); char head = queue.deleteHead(); Test(head,'a'); head = queue.deleteHead(); Test(head,'b'); queue.appendTail('d'); head = queue.deleteHead(); Test(head, 'c'); queue.appendTail('e'); head = queue.deleteHead(); Test(head, 'd'); head = queue.deleteHead(); Test(head, 'e'); return 0;}
拓展:用两个队列实现栈
分析:入栈动作时,如果内部两个队列都为空的话,将数据压入其中一个队列(代码中为m_queue1)。如果其中一个队列已经有数据了,则将数据压入已经有数据的那个队列。出栈动作时,先将有数据的那个队列,除了最后一个入队的数据之外的所有数据输出到另外一个空的队列,然后最后那个数据也出队。
源代码:
#include<cstdlib>#include<cstdio>#include<cstring>#include<queue>using std::queue;template<typename T>class CStack{public: CStack(void){} ~CStack(void){} void push(const T&node); T pop(); private: queue<T> m_queue1; queue<T> m_queue2;};template<typename T>void CStack<T>::push(const T &node){ if((m_queue1.empty() && m_queue2.empty()) || m_queue1.size()) { m_queue1.push(node); } else { m_queue2.push(node); }}template<typename T>T CStack<T>::pop(){ if(m_queue1.empty()&&m_queue2.empty()) { printf("Stack is empty\n"); return T(); } T node; if(m_queue1.size()) { while (m_queue1.size() > 1) { node = m_queue1.front(); m_queue1.pop(); m_queue2.push(node); } node = m_queue1.front(); m_queue1.pop(); } else { while (m_queue2.size()>1) { node = m_queue2.front(); m_queue2.pop(); m_queue1.push(node); } node = m_queue2.front(); m_queue2.pop(); } return node;}int main(int argc,char *argv[]){ CStack<char> testStack; testStack.push('a'); testStack.push('b'); testStack.push('c'); char ch = testStack.pop(); printf("%c\n",ch); ch = testStack.pop(); printf("%c\n",ch); testStack.push('d'); ch = testStack.pop(); printf("%c\n",ch); return 0;}
0 0
- 剑指Offer系列---(9)用两个栈实现队列和用两个队列实现栈
- 剑指offer系列-用两个栈实现队列
- 剑指offer系列之五:用两个栈实现队列
- 剑指offer系列-用两个栈实现队列
- 剑指offer系列-用两个队列实现栈
- 剑指offer系列----用两个栈实现一个队列
- [剑指offer]用两个栈实现队列
- 【剑指offer】用两个栈实现队列
- 剑指offer--用两个栈实现队列
- 剑指Offer-用两个栈实现队列
- 剑指offer:用两个栈实现队列
- 《剑指offer》用两个栈实现队列
- 用两个栈实现队列(剑指offer)
- 剑指 offer:用两个栈实现队列
- 【剑指Offer】用两个栈实现队列
- 剑指offer-用两个栈实现队列
- 《剑指offer》-用两个栈实现队列
- 剑指offer 用两个栈实现队列
- android 实现应用卸载反馈
- POJ 2506 Tiling(大数版递推)
- CRSCTL 与 SRVCTL区别
- 程序员如何在复杂代码中查找到程序的bug?
- Jdk5.0泛型
- 剑指Offer系列---(9)用两个栈实现队列和用两个队列实现栈
- Photoshop入门学习
- hdu 4886 hash+暴力
- myeclipse使用jquery-easyui
- Theano的安装及GPU的配置
- 获取AWS所拥有的公网IP列表
- 深入理解String
- 堆空间的开辟与释放
- access数据库的两种驱动方式