堆栈相关的经典题(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 ...
随机推荐
- python程序开机自启动
windows下设置 因为服务器是windows环境 担心黑窗口不小心被关闭 因此想要让python程序在后台运行 只需要一下几步 1. 在启动python启动文件加入以下代码 import win ...
- CentOS7安装桌面环境以及中文语言支持
CentOS7 操作系统 http://public-yum.oracle.com/oracle-linux-isos.html ================================= 1 ...
- 第一篇 -- 下载并安装IDEA
此篇讲的是安装IDEA企业版,社区版是免费的,就不多说了. 参考链接:https://www.exception.site/essay/how-to-free-use-intellij-idea-20 ...
- jvm源码解读--03 常量池的解析ConstantPool
先看bt栈 (gdb) bt #0 ConstantPool::allocate (loader_data=0x7fe21802e868, length=87, __the_thread__=0x7f ...
- header.mapper 用法
const header= [xxxx,xxxx] 基本用法是 const obj = header.map( item=>{ obj= item+'123'; return obj }) c ...
- ES6新特征
1.块级作用域 { } 就是块级作用域,还包括if.else.for.while...下都属于块级作用域. let 声明的变量不存在变量的提升,不允许let反复声明同一个变量:块级作用域下let ...
- fastboot刷机小脚本
在Windows系统下,一般刷机命令是在cmd路径下执行如下命令: 1.adb reboot bootloader2.fastboot flash boot +boot路径3.fastboot fla ...
- SpringCloud升级之路2020.0.x版-8.理解 NamedContextFactory
本系列为之前系列的整理重启版,随着项目的发展以及项目中的使用,之前系列里面很多东西发生了变化,并且还有一些东西之前系列并没有提到,所以重启这个系列重新整理下,欢迎各位留言交流,谢谢!~ spring- ...
- (2)用 if语句 区间判断
1 /*此例子只作为演示*/ 2 3 #include <stdio.h> 4 int main() 5 { 6 printf("请问贵公司给出的薪资是:\n"); 7 ...
- CVE-2021-3156 复现
测试环境 OS: Ubuntu 18.04.5 LTS GCC: gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) Make: GNU Make 4.1 ...