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++)的更多相关文章

  1. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

  2. Hihicoder 题目1 : Trie树(字典树,经典题)

    题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...

  3. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  4. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  5. poj 2001:Shortest Prefixes(字典树,经典题,求最短唯一前缀)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12731   Accepted: 544 ...

  6. hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  7. hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  8. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  9. poj 1006:Biorhythms(水题,经典题,中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Des ...

随机推荐

  1. 【知识详解】Https详解

    Https详解 1.什么是Https Http + SSL = Https 一句话说:Https是身披SSL的Http,当使用了SSL后,Http先和SSL通信,再由SSL和TCP通信, 2.为什么需 ...

  2. THE MINTO PYRAMID PRINCIPLE

    金字塔原理:(重点突出,逻辑清晰.层次分明,简单易懂的思考方式.沟通方式.规范的动作.) 结构:结论先行,以上统下,归类分组,逻辑递进.先重要后次要,先总结后具体,先框架后细节,先结论后原因,先结果后 ...

  3. C++第五十篇 -- 获取串口的描述信息

    如何知道自己的电脑上有无串口呢? -- 手动 1. 查看电脑,看是否有串口器件(串口是一个九针的D型接口) 2. 在设备管理器上查看 乍一看,还以为是有两个串口,其实仔细看描述就知道,这是蓝牙虚拟串口 ...

  4. element UI+vue关于日期范围选择的操作,picker-options属性的使用

    一般 <el-date-picker />使用会出现起始日期和结束日期,结束日期不能早与起始日期,选择了其实日期后,结束日期大于起始日期的不可选,置灰,同理先选结束日期后再选起始日期,那么 ...

  5. HCNA Routing&Switching之二层交换技术VLAN基础

    前文我们主要聊了下交换机的工作原理和以太网接口的速率和双工相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15088183.html:今天我们主要来聊一 ...

  6. 3.python编程与计算机的关系,如何执行python文件

    上一节预告了这一章想讲如何不停地和世界打招呼,这涉及到编程中一个重要的概念:循环. 但经过了两周断更后细想了一下,不行,我们得对上一章进行补充,而且这个补充非常关键!也印证了上一章所说的: 上一节章很 ...

  7. DNS投毒学习分析总结

    [一]背景 今晚看一份日志,数据很奇怪.大佬说是DNS投毒,盲点就来了,学习一下~ [二]内容 https://zhuanlan.zhihu.com/p/92899876 看完内容发现属于之前写的DN ...

  8. vulnhub靶机-XXE Lab 1

    目录 信息收集 漏洞利用 信息收集 扫描目标主机,ip为192.168.88.154 nmap扫描结果 存在robots.txt文件.直接访问其中的admin.php显示404,加一层目录访问/xxe ...

  9. 详细讲讲netty的pipiline!

    前言 提到 Netty 首当其冲被提起的肯定是支持它承受高并发的线程模型,说到线程模型就不得不提到 NioEventLoopGroup 这个线程池,接下来进入正题. 线程模型 首先来看一段 Netty ...

  10. 【Python机器学习实战】决策树和集成学习(一)

    摘要:本部分对决策树几种算法的原理及算法过程进行简要介绍,然后编写程序实现决策树算法,再根据Python自带机器学习包实现决策树算法,最后从决策树引申至集成学习相关内容. 1.决策树 决策树作为一种常 ...