堆栈相关的经典题(c++)
1.定义队列
typedef struct node{
int data;
struct node * next;
}Node;
typedef struct linkQueue
{
Node * first;
Node * rear;
}Queue;
2.两个栈实现一个队列
类定义:
#include <iostream>
#include <stack> using namespace std;
template<typename T> class CQueue
{
public:
CQueue();
~CQueue();
void appendTail(const T& node);
T deleteHead();
private:
stack<T> stack1;
stack<T> stack2;
};
具体实现:
template <typename T> CQueue<T>::CQueue()
{
cout<<"CQueue"<<endl;
}
template <typename T> CQueue<T>::~CQueue()
{
cout<<"~CQueue"<<endl;
}
template <typename T> void CQueue<T>::appendTail(const T& node)
{
stack1.push(node);
cout<<"appendTail"<<endl; }
template <typename T> T CQueue<T>:: deleteHead()
{
if(stack2.size()<=0)
{
while(stack1.size()>0)
{
T& data = stack1.top();
stack1.pop();
stack2.push(data);
}
}
if(stack2.size()==0)
{
cout<<"deleteHead:error"<<endl;
return NULL;
}else
{
T & head = stack2.top();
stack2.pop();
return head;
}
cout<<"deleteHead"<<endl;
}
调用:
int main()
{
CQueue<int> cq;
cq.appendTail(2);
cq.appendTail(3);
cq.appendTail(1);
int i = cq.deleteHead();
cout << "val: "<<i<< endl;
return 0;
}
运行结果:
val:2
3.栈的转置
将当前栈的最底元素移到栈顶,其他元素顺次下移一位,然后继续不包含该栈顶元素的子栈。终止条件:递归下去,直到栈为空。代码如下
/**采用递归方式实现栈的转置**/
void Move_bottom2top(stack<int>& s)
{
if(s.empty()) return;
int top1 = s.top();
s.pop();
if(!s.empty())
{
Move_bottom2top(s);
int top2 = s.top();
s.pop();
s.push(top1);
s.push(top2);
return;
}
s.push(top1);
}
/**栈转置**/
void Reverse(stack<int> & s)
{
if(s.empty())return;
Move_bottom2top(s);
int top = s.top();
s.pop();
Reverse(s);
s.push(top);
}
4.栈的排序
和栈的转置思想类似
/**采用递归方式实现栈的排序**/
void Move_min2top(stack<int> &s)
{
if(s.empty())return;
int top1 = s.top();
s.pop();
if(!s.empty())
{
Move_min2top(s);
int top2 = s.top();
if(top1>top2)
{
s.pop();
s.push(top1);
s.push(top2);
return;
}
}
s.push(top1);
}
/**栈排序**/
void Sort(stack<int> & s)
{
if(s.empty())return;
if(!s.empty())
{
Move_min2top(s);
int top = s.top();
s.pop();
Sort(s);
s.push(top);
}
}
5.两个队列实现栈
定义栈类
/**两个队列实现一个栈**/
template <class T> class CStack
{
public:
CStack();
~CStack();
T pop();
void push(T e);
private:
queue<T> queue1;
queue<T> queue2;
};
实现
Pop
template<class T> T CStack<T>::pop()
{
cout<<"Pop:"<<endl;
if(!queue1.empty())
{
while(queue1.size()!=1)
{
int front = queue1.front();
queue1.pop();
queue2.push(front);
}
T val = queue1.front();
queue1.pop();
return val;
}
if(!queue2.empty())
{
while(queue2.size()!=1)
{
int front = queue2.front();
queue2.pop();
queue1.push(front);
}
T val = queue2.front();
queue2.pop();
return val;
}
cout<<"Pop:error stack is null"<<endl;
return NULL;
}
Push
template<class T>void CStack<T>::push(T e)
{
cout<<"push:"<<endl;
if(!queue1.empty())
{
queue1.push(e);
return;
}
if(!queue2.empty())
{
queue2.push(e);
return;
}
queue1.push(e);
}
堆栈相关的经典题(c++)的更多相关文章
- poj 1611:The Suspects(并查集,经典题)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21472 Accepted: 10393 De ...
- Hihicoder 题目1 : Trie树(字典树,经典题)
题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...
- poj 3264:Balanced Lineup(线段树,经典题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 32820 Accepted: 15447 ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)
Shortest Prefixes Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12731 Accepted: 544 ...
- hdu 1247:Hat’s Words(字典树,经典题)
Hat’s Words Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110991 Accepted: 34541 Des ...
随机推荐
- [考试总结]noip模拟17
爆零了! 菜爆了 弱展了 垃爆了 没有什么可以掩饰你的菜了 这次考试为我带来了第一个 \(\color{red}{ \huge{0}}\) 分,十分欣慰.... 最近的暴力都打不对,你还想什么正解?? ...
- 3G/4G串口服务器
Z3G/4G串口服务器 ZLAN8303-7是上海卓岚继ZLAN8100之后推出的3G/4G联网解决方案.支持7模的4G串口服务器.其产品支持Modbus功能.自定义注册包心跳包功能. ZLAN830 ...
- 开发者如何快速搭建自己的电商App?
面向电商购物场景,HMS Core提供了创新的电商解决方案,帮助应用快速获客.提升转化率,实现业务增长.为了帮助开发者了解如何在电商购物类应用中集成HMS Core的各项能力,HMS Core开发了电 ...
- Redis 实战篇:巧用数据类型实现亿级数据统计
在移动应用的业务场景中,我们需要保存这样的信息:一个 key 关联了一个数据集合,同时还要对集合中的数据进行统计排序. 常见的场景如下: 给一个 userId ,判断用户登陆状态: 两亿用户最近 7 ...
- C++ //多态案例三 ---电脑组装
1 //多态案例三 ---电脑组装 2 3 #include <iostream> 4 #include <string> 5 using namespace std; 6 7 ...
- MyBatis学习04(注解开发)
7.使用注解开发 7.1 面向接口编程 根本原因 : 解耦 , 可拓展 , 提高复用 , 分层开发中 , 上层不用管具体的实现 , 大家都遵守共同的标准 , 使得开发变得容易 , 规范性更好 在一个面 ...
- MySQL-15-主从复制
企业高可用性标准 1 全年无故障率(非计划内故障停机) 99.9% ----> 0.001*365*24*60=525.6 min 99.99% ----> 0.0001*365*24*6 ...
- SpringBoot开发八-会话管理
需求介绍-会话管理 利用Cookie和Seesion使得HTTP变成有会话的连接,写几个实例演示一下 代码实现 先写个例子,表示客户端第一次访问服务器,服务器端创建一个Cookie发送给客户端. 不管 ...
- SpringBoot开发二
需求介绍-Spring入门 主要是理解IOC,理解容器和Bean 代码 在Test里面利用getBean方法帮助我们看一下容器的创建: 那我首先要写一个Bean对象,假设是写一个访问数据库类. Alp ...
- pikachu Files Inclusion
文件包含分为远程文件包含和远程文件包含 比如程序员为了提高效率让代码看起来简洁,会使用包含函数的功能,写多个文件 之后需要了进行调用,比如.c写了很多个函数分别在不同的文件里,用的时候直接 引用文件即 ...