Algorithm --> 两个栈实现队列和两个队列实现栈
两个栈实现队列和两个队列实现栈
队列(queue)先进先出的线性表;栈(stack)先进后出的线性表。
两个栈实现队列
法一思路:
s1是入栈的,s2是出栈的。
- 入队列:直接压入s1即可;
- 出队列:如果s2不为空,把s2中的栈顶元素直接弹出;否则,把s1的所有元素全部弹出压入s2中,再弹出s2的栈顶元素。
代码:
#include <stack>
#include <iostream>
#include <cassert>
using namespace std; template <class T>
class Queue {
public:
bool empty( ) const { return m_stack1.empty() && m_stack2.empty(); }
size_t size( ) const { return m_stack1.size() + m_stack2.size(); }
void push( const T& x );
void pop( );
private:
stack<T> m_stack1;
stack<T> m_stack2;
}; template <class T>
void Queue<T>::push( const T& x )
{
m_stack1.push( x );
} template <class T>
void Queue<T>::pop( )
{
// if m_stack2 is empty, and there are some elements in m_stack1, push them in m_stack2
if( m_stack2.empty() ) {
while( !m_stack1.empty() ) {
T& data = m_stack1.top( );
m_stack1.pop( );
m_stack2.push( data );
}
}
// push the element into m_stack2
assert( !m_stack2.empty() );
m_stack2.pop( );
} int main( )
{
Queue<int> q;
for( int i = ; i <= ; ++i )
q.push( i );
q.pop( );
q.push( );
q.pop( );
q.pop( );
q.push( );
q.pop( );
q.pop( );
q.pop( );
q.pop( ); return ;
}
法二思路:
用两个栈来实现,令其中一个栈inStack专门处理入队工作,另一个栈outStack专门处理出队工作。
- 入队列:直接把元素加入到 inStack中,复杂度为O(1);
- 出队列:因为队列是先入先出的,而栈是先入后出,所以输出应该反序。比如inStack接收到的元素顺序为1, 2, 3,栈顶元素是3,但出队希望让1先出。解决方法是把inStack里的元素放到outStack中,这样outStack接收到的元素顺序就是3, 2, 1了,栈顶元素就是我们想要的1,时间复杂度为O(N)。
总结,入队直接inStack.push(元素),出队先判断outStack是否为空,若不为空,则直接outStack.pop(),若为空,则把inStack元素导入到outStack里,再执行outStack.pop()。
#include <stack>
#include <string>
#include <iostream>
#include <cassert> using namespace std; template <class T>
class YL_Queue
{
public:
void enqueue(const T &element); //入队
T dequeue(); //出队
T top(); //队首元素
bool empty() const; //判断队列是否为空
size_t size() const; //队列的尺寸大小
private:
stack<T> inStack;
stack<T> outStack;
}; template <class T>
void YL_Queue<T>::enqueue(const T &element)
{
inStack.push(element);
} template <class T>
T YL_Queue<T>::dequeue()
{
assert(!empty()); T temp;
if (!outStack.empty())
{
temp=outStack.top();
outStack.pop();
return temp;
}
else
{
while(!inStack.empty())
{
temp=inStack.top();
inStack.pop();
outStack.push(temp);
}
temp= outStack.top();
outStack.pop();
return temp;
}
} template <class T>
T YL_Queue<T>::top()
{
assert(!empty()); T temp;
if (!outStack.empty())
{
temp=outStack.top();
return temp;
}
else
{
while(!inStack.empty())
{
temp=inStack.top();
inStack.pop();
outStack.push(temp);
}
temp= outStack.top();
return temp;
}
} template <class T>
bool YL_Queue<T>::empty() const
{
return (size()==);
} template <class T>
size_t YL_Queue<T>::size() const
{
return inStack.size()+outStack.size();
}
void main()
{
YL_Queue<int> myqueue;
myqueue.enqueue();
myqueue.enqueue();
myqueue.enqueue();
myqueue.enqueue();
myqueue.enqueue();
cout<<"1队列的大小为: "<<myqueue.size()<<endl;
cout<<"1队首的元素为: "<<myqueue.top()<<endl;
myqueue.dequeue();
myqueue.dequeue();
cout<<"2队列的大小为: "<<myqueue.size()<<endl;
cout<<"2队首的元素为: "<<myqueue.top()<<endl;
myqueue.dequeue();
myqueue.dequeue();
myqueue.dequeue();
cout<<"3队列的大小为: "<<myqueue.size()<<endl;
}
两个队列实现栈
思路
s1是入队列的,s2是辅助s1入队列的。
- 入队列:如果s1不空,把s1的元素push到s2中,之后push新元素到s1,再把s2的元素都push到s1中;否则,直接push元素到s1
- 出队列:直接从s1中pop
代码:
#include <iostream>
#include <queue>
#include <cassert>
using namespace std; template <class T>
class Stack {
public:
bool empty( ) const { return m_queue1.empty() && m_queue2.empty(); }
size_t size( ) const { return m_queue1.size() + m_queue2.size(); }
T& top( );
const T& top( ) const;
void push( const T& x );
void pop( );
private:
queue<T> m_queue1;
queue<T> m_queue2;
}; template <class T>
T& Stack<T>::top( )
{
assert( !m_queue1.empty() );
return m_queue1.front( );
} template <class T>
const T& Stack<T>::top( ) const
{
assert( !m_queue1.empty() );
return m_queue1.front( );
} template <class T>
void Stack<T>::push( const T& x )
{
while( !m_queue1.empty() ) { //s1不空,把元素push到s2
const T& val = m_queue1.front( );
m_queue1.pop( );
m_queue2.push( val );
}
m_queue1.push( x ); //push新元素到s1
while( !m_queue2.empty() ) { //s2不空,把元素在push回s1
const T& val = m_queue2.front( );
m_queue2.pop( );
m_queue1.push( val );
}
} template <class T>
void Stack<T>::pop( )
{
assert( !m_queue1.empty() );
m_queue1.pop( );
} int main( )
{
Stack<int> s;
for( int i = ; i <= ; ++i )
s.push( i );
cout << "1.栈元素个数:" << s.size( ) << endl;
cout << "1.栈顶元素:" << s.top( ) << endl; s.pop( );
s.push( );
cout << "2.栈元素个数:" << s.size( ) << endl;
cout << "2.栈顶元素:"<< s.top( ) << endl; s.pop( );
s.pop( );
s.pop( );
s.push( );
cout << "3.栈元素个数:" << s.size( ) << endl;
cout << "3.栈顶元素:"<< s.top( ) << endl; s.pop( );
s.pop( );
s.pop( );
cout << "4.栈元素个数:" << s.size( ) << endl; return ;
}
Algorithm --> 两个栈实现队列和两个队列实现栈的更多相关文章
- 剑指offer 5.栈和队列 用两个栈实现队列
题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解题思路:1,整体思路是元素先依次进入栈1,再从栈1依次弹出到栈2,然后弹出栈2顶部的元素,整个过程 ...
- 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别
前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...
- [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...
- 算法与数据结构题目的 PHP 实现:栈和队列 由两个栈组成的队列
思路:同样使用 PHP 的数组模拟栈.栈的特点是先进后出,队列的特点是先进先出,可以用第一个栈(StackPush)作为压入栈,压入数据的时候只往这个栈中压入数据,第二个栈作(StackPop)为弹出 ...
- 包含MIN函数的栈+一个数组实现两个堆栈+两个数组实现MIN栈
1.题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数. 思路:利用一个辅助栈来存放最小值 栈 3,4,2,5,1 辅助栈 3,2,1 每入栈一次,就与辅 ...
- 使用java语言实现一个队列(两种实现比较)(数据结构)
一.什么是队列,换句话说,队列主要特征是什么? 四个字:先进先出 六个字:屁股进,脑袋出 脑补个场景:日常排队买饭,新来的排在后面,前面打完饭的走人,这就是队列: OK,思考一个问题,我为什么写了两种 ...
- Linux 进程间通信 消息队列 实现两个进程间通信
例子: 通过消息队列实现两个进程间通信,一个进程从终端输入数据,通过消息队列发送,另一个进程通过消息队列接收数据 文件1 创建进程1 终端输入通过消息队列发送数据 #include <stdio ...
- rabbitmq 实现延迟队列的两种方式
原文地址:https://blog.csdn.net/u014308482/article/details/53036770 ps: 文章里面延迟队列=延时队列 什么是延迟队列 延迟队列存储的对象肯定 ...
- Java数组实现循环队列的两种方法
用java实现循环队列的方法: 1.添加一个属性size用来记录眼下的元素个数. 目的是当head=rear的时候.通过size=0还是size=数组长度.来区分队列为空,或者队列已满. 2.数组中仅 ...
随机推荐
- dojo省份地市级联之地市封装类(二)
dojo省份地市级联之地市封装类 City.java: /** * 地市封装类 */ package com.you.model; import java.io.Serializable; /** * ...
- 部署Java Web项目报错(一)
今天,我在部署Java Web项目时,出现错误,并且在eclipse新建一个servers,却出现多个项目. 具体错误截图如下: 然后,我又将项目部署到JBoss服务器中,却还是运行不成功 22:12 ...
- strstr()函数实现
/* 函数要求:写一个函数模拟strstr()函数,设计中不得使用其他库函数. 函数原型:const char *strstr(const char *str1,const char *str2); ...
- Educational Codeforces Round37 E - Connected Components?
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> ...
- Codeforces Round #454 D. Seating of Students
分三类 1 1: 一个就好了 3 3:特殊讨论下 或 : 第一行奇序号的数放前面,偶序号的数放后面,第二行奇序号的数放前面,偶序号的数放后面,第二行依次类推 有点难写,真的菜 #include< ...
- studio设置File Templates
从项目的整体风格考虑,对所有类要进行必要的说明,就注释说明来说首先需要说明是作者,文件创建时间,业务功能说明,这几项是基本的内容,而添加这些说明内容以前可能手动的添加文件标题头,这种做法现在都非常过时 ...
- arttemplate与webpack的应用
模板渲染使用arttemplate,使用方法如下: 普通使用 普通使用把渲染模板写在<script>标签里面,引入arttemplate.js,从服务端接收数据(数组与对象的形式),然后调 ...
- MFC关于多线程中传递窗口类指针时ASSERT_VALID出错的另类解决 转
MFC关于多线程中传递窗口类指针时ASSERT_VALID出错的另类解决 在多线程设计中,许多人为了省事,会将对话框类或其它类的指针传给工作线程,而在工作线程中调用该类的成员函数或成员变量等等. ...
- springMVC web项目 对访问数据库的用户名密码进行加密解密
在使用springMVC开发web项目中,数据库的用户名,密码一般都是配置在.properties文件中 然后在通过.xml配置文件引入.properties的变量,例如 在config.proper ...
- requests库使用
介绍: 发送HTTP请求的第三方库,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) 安装:pip3 install requests 学习reques ...