博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
题目七:用两个栈实现队列
阅读量:4695 次
发布时间:2019-06-09

本文共 3585 字,大约阅读时间需要 11 分钟。

// 10.题目七:用两个栈实现队列
// 题目:用两个栈实现一个队列,队列的声明如下:

template 
class CQueue{public: CQueue(){} ~CQueue(){} void AppendTail(const TYPE& node); TYPE DeleteHead();private: stack
m_stPushStack; stack
m_stPopStack;};// 方法一:// 插入时 --> m_stPopStack不为空,将元素压入m_stPushStack;// 删除时 --> m_stPushStack不为空,将元素压入 m_stPopStack;// 方法二:// 插入时 --> 直接往 m_stPushStack 插入新元素// 删除时 --> 如果m_stPopStack为空,插入m_stPushStack中元素,否则直接弹出元素template
TYPE CQueue
::DeleteHead(){#if 0 while (!m_stPushStack.empty()) { m_stPopStack.push(m_stPushStack.top()); m_stPushStack.pop(); }#else if (m_stPopStack.empty()) { while (!m_stPushStack.empty()) { m_stPopStack.push(m_stPushStack.top()); m_stPushStack.pop(); } }#endif TYPE tmp = m_stPopStack.top(); m_stPopStack.pop(); return tmp;}template
void CQueue
::AppendTail(const TYPE& node){#if 0 while (!m_stPopStack.empty()) { m_stPushStack.push(m_stPopStack.top()); m_stPopStack.pop(); } m_stPushStack.push(node);#else m_stPushStack.push(node);#endif }void QueueWithTwoStackTestFunc(){ cout << "\n\n --------------- QueueWithTwoStackTestFunc Start -------------->" << endl; CQueue
stQueue; stQueue.AppendTail(1); stQueue.AppendTail(2); stQueue.AppendTail(3); cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl; stQueue.AppendTail(4); stQueue.AppendTail(5); stQueue.AppendTail(6); cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl; cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl; cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl; cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl; cout << "Queue Pop Node: " << stQueue.DeleteHead() << endl; cout << "\n\n --------------- QueueWithTwoStackTestFunc End -------------->" << endl;}

/

// 11.用两个队列实现一个栈

template 
class CStack{public: CStack(){} ~CStack(){}public: void AppendTail(const TYPE& value); TYPE DeleteHead();private: queue
m_stQueue1; queue
m_stQueue2;};template
TYPE CStack
::DeleteHead(){ TYPE head; if (m_stQueue1.empty()) { while (m_stQueue2.size() > 1) { m_stQueue1.push(m_stQueue2.front()); m_stQueue2.pop(); } head = m_stQueue2.front(); m_stQueue2.pop(); } else { while (m_stQueue1.size() > 1) { m_stQueue2.push(m_stQueue1.front()); m_stQueue1.pop(); } head = m_stQueue1.front(); m_stQueue1.pop(); } return head;}template
void CStack
::AppendTail(const TYPE& value){ m_stQueue1.push(value);}void StackWithTwoQueueTestFunc(){ cout << "\n\n --------------- StackWithTwoQueueTestFunc Start -------------->" << endl; CStack
stStack; stStack.AppendTail(1); stStack.AppendTail(2); stStack.AppendTail(3); cout << "Stack Pop Node: " << stStack.DeleteHead() << endl; stStack.AppendTail(4); stStack.AppendTail(5); stStack.AppendTail(6); cout << "Stack Pop Node: " << stStack.DeleteHead() << endl; cout << "Stack Pop Node: " << stStack.DeleteHead() << endl; cout << "Stack Pop Node: " << stStack.DeleteHead() << endl; cout << "Stack Pop Node: " << stStack.DeleteHead() << endl; cout << "Stack Pop Node: " << stStack.DeleteHead() << endl; cout << "\n\n --------------- StackWithTwoQueueTestFunc End -------------->" << endl;}

转载于:https://www.cnblogs.com/yzdai/p/11258619.html

你可能感兴趣的文章
入侵检测-转载
查看>>
最大熵模型(Maximum Etropy)—— 熵,条件熵,联合熵,相对熵,互信息及其关系,最大熵模型。。...
查看>>
Git相关操作一
查看>>
阿里盒马领域驱动设计实践
查看>>
VS2005中的一个小BUG:关于Dropdownlist无法Datadinding的解决方法。
查看>>
frame-框架
查看>>
IOS开发学习笔记034-UIScrollView-xib实现分页
查看>>
(转)AS3多人游戏开发—同步人物移动1
查看>>
Djang学习笔记-1
查看>>
JS 中 constructor属性的用法
查看>>
python * 和 ** 的用法
查看>>
解决 src/MD2.c:31:20: fatal error: Python.h: No such file or directory安装包错误
查看>>
DrawerLayout 全屏显示(可以覆盖到statusbar上面)
查看>>
Flask的简单认识
查看>>
自旋锁(spinlock)(转)
查看>>
Springboot项目上传文件大小限制问题
查看>>
网络攻防实验三
查看>>
转 android 动态加载 插件模型开发
查看>>
STL中sort、priority_queue、map、set的自定义比较函数
查看>>
c primer plus 习题答案(6)
查看>>