剑指Offer05 用栈模拟队列
2024-08-24 07:40:26
添加了模板类应用
/*************************************************************************
> File Name: 05_StackMakeQueue.cpp
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年08月29日 星期一 19时32分12秒
************************************************************************/ #include <stdio.h>
#include <bits/stdc++.h> using namespace std; template<class T>
class qStack
{
private:
stack<T> stack1;
stack<T> stack2;
int size; public:
qStack()
{
size = ;
}
void push(T &node)
{
stack1.push(node);
size = stack1.size();
}
T pop()
{
assert(this->size > );
T ret = ;
if (this->size == )
{
ret = stack1.top();
stack1.pop();
this->size = ;
return ret;
}
if (this->size > )
{
while (stack1.size())
{
stack2.push(stack1.top());
stack1.pop();
}
ret = stack2.top();
stack2.pop(); while (stack2.size())
{
stack1.push(stack2.top());
stack2.pop();
}
this->size --;
return ret;
}
}
}; int main()
{
qStack<int> quque;
for (int i = ; i < ; i++)
{
cout << i << " push" << endl;
quque.push(i);
}
for (int i = ; i < ; i++)
{
cout << quque.pop() << " pop" << endl;
}
for (int i = ; i < ; i++)
{
cout << i << " push" << endl;
quque.push(i);
}
for (int i = ; i < ; i++)
{
cout << quque.pop() << " pop" << endl;
}
cout << endl;
}
最新文章
- SQL SERVER特殊行转列案列一则
- Android笔记:数据储存
- CF 115B Lawnmower(贪心)
- 调用jar程序,读取与jar同级的配置文件。
- nusoap
- WWDC 2016 总结
- c# web 缓存管理
- CentOS hadoop配置错误Incorrect configuration: namenode address dfs.namenode.servicerpc-address ...
- PDO应用
- JQuery datepicker 用法
- Java——(四)Collection之Set集合TreeSet类
- 【ICIP2013】MULTI-SOURCE IMAGE AUTO-ANNOTATION
- PPT排版细节,写给大家看的设计书,完美总结
- LVS集群TUN模式实例(5)
- 内核级线程(KLT)和用户级线程(ULT)
- A real example of vioplot in R (sample data and code attached)
- 集体干死java 在启动.sh
- 前端学习 -- Html&;Css -- 相对定位 绝对定位 固定定位
- linux rpm 安装后 mysql 默认安装目录等信息
- C# 各种输入格式验证#各种输入格式验证