堆栈相关的经典题(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 ...
随机推荐
- 【LeetCode】204.计数质数
问题描述: 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 这是一道简单题,但是却并没有那么直 ...
- sqli-labs 靶场笔记
sqli-labs 靶场笔记 冲冲冲,好好学习 2020年1月12号 先来点铺垫 分类: 按照请求方法分类: 1.Get型注入:注入参数在get的参数里. 2.POST型注入:注入参数在POST的参数 ...
- 【爬虫系列】1. 无事,Python验证码识别入门
最近在导入某站数据(正经需求),看到他们的登录需要验证码, 本来并不想折腾的,然而Cookie有效期只有一天. 已经收到了几次夜间报警推送之后,实在忍不住. 得嘞,还是得研究下模拟登录. 于是,秃头了 ...
- Vue2中父子组件通信的几种常用方法
源码地址 点击查看演示源码 Vue2父子传参:props 首先在父组件中引入子组件,然后以属性的方式将数据传递给子组件 父组件: <template> <div class=&quo ...
- mybatis动态sql总结
前言 平时在开发中,针对动态sql这块目前是薄弱点,自己根据官网在对应项目边测试边写博客,此篇只是为了加深动态sql的熟练度,有不到之处敬请批评指正! 1.if 使用动态 SQL 最常见情景是根据条件 ...
- 【剑指offer】59 - I. 滑动窗口的最大值
剑指 Offer 59 - I. 滑动窗口的最大值 知识点:队列:滑动窗口:单调 题目描述 给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值. 示例 输入: nums = [ ...
- 字节跳动上传了一份“面试官版Android面试小册”,不讲一句废话,全是精华
前言 金三银四马上就到了,很多粉丝朋友私信希望我出一篇面试专题或者分享面试相关的笔记来学习,这不今天就给大家安排上了?(都是干货,错过就是亏.) 下面的面试笔记都是精心整理好免费分享给大家的,希望新朋 ...
- 算法竞赛中的常用JAVA API :HashMap 和 TreeMap(转载)
算法竞赛中的常用JAVA API :HashMap 和 TreeMap 摘要 本文主要介绍Map接口下的HashMap和TreeMap. HashMap HashMap是基于哈希表的 Map 接口的实 ...
- Shell-15-脚本练习
批量生成随机字符串文件名 # 用for循环在 /test 目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字符加固定字符串 alnk #!/bin/bash ########### ...
- 【vulhub】Weblogic CVE-2017-10271漏洞复现&&流量分析
Weblogic CVE-2017-10271 漏洞复现&&流量分析 Weblogic CVE-2017-10271 XMLDecoder反序列化 1.Weblogic-XMLDeco ...