利用两个栈实现队列的--->基本操作
2017-05-09 00:20
267 查看
面试题:使用两个栈实现一个队列
栈和队列都是我们经常使用的数据结构, 栈 -> 后进先出, 队列 -> 先进先出 何其相似, 既然如此, 我们可不可以 试着 用 两个栈来试试实现一个队列呢
基本思想:我们使用两个栈, s1:实现入队,s2:实现出队 首先,将数据存放在栈s1中,然后将数据push进s2中,再将s2中的数据pop
出队列: (1)如果栈B不为空,直接弹出栈B的数据 (2)如果栈B为空,则依次弹出栈A的数据,放入栈B中,再弹出栈B的数据
#include <iostream>
#include <stack>
#include <windows.h>
using namespace std;
template <typename T>
class MyQueue
{
public:
MyQueue( )
{}
~MyQueue( )
{}
void Push( const T& data )
{
s1.push( data );
++size;
}
void Pop( )
{
if ( true == Empty( ) )
throw new exception( "队列为空!\n" );
//我们不采用 将 s1 的元素全部压入 s2 后 再 对s2 进行 一次 pop 的做法。 如下的做法可以 节省一次 入栈的开销
if ( true == s2.empty( ) )
{
while( 1 != s1.size( ) )
{
s2.push( s1.top( ) );
s1.pop( );
}
s1.pop( );
}
else
{
s2.pop( );
}
--size;
}
bool Empty( )
{
if ( true == s1.empty( ) )
if ( true == s2.empty( ) )
return true;
return false;
}
size_t Size( )
{
return size;
}
T& Front( )
{
if ( true == Empty( ) )
throw new exception( "队列为空!\n" );
if ( true == s2.empty( ) )
{
while ( 0 != s1.size( ) )
{
s2.push( s1.top( ) );
s1.pop( );
}
}
return s2.top( );
}
T& Back( )
{
if ( true == Empty( ) )
throw new exception( "队列为空!\n" );
if ( true == s1.empty( ) )
{
while ( 0 != s2.size( ) )
{
s1.push( s2.top( ) );
s2.pop( );
}
}
return s1.top( );
}
private:
stack<T> s1; //注意,不要写成 stack s1; -> stack<T> s1. s1里面存的数据是 T 类型.
stack<T> s2;
size_t size;
};
void Test( )
{
MyQueue<int> q;
cout << q.Empty( ) << endl;
q.Push( 1 );
cout << q.Front( ) << endl;
cout << q.Back( ) << endl;
cout << q.Empty( ) << endl;
q.Push( 2 );
q.Push( 3 );
q.Push( 4 );
q.Push( 5 );
cout << q.Front( ) << endl;
cout << q.Back( ) << endl;
q.Pop( );
cout << q.Front( ) << endl;
cout << q.Back( ) << endl;
q.Pop( );
q.Pop( );
q.Pop( );
cout << q.Front( ) << endl;
cout << q.Back( ) << endl;
q.Pop( );
cout << q.Empty( ) << endl;
}
int main( )
{
Test( );
system( "pause" );
return 0;
}
栈和队列都是我们经常使用的数据结构, 栈 -> 后进先出, 队列 -> 先进先出 何其相似, 既然如此, 我们可不可以 试着 用 两个栈来试试实现一个队列呢
基本思想:我们使用两个栈, s1:实现入队,s2:实现出队 首先,将数据存放在栈s1中,然后将数据push进s2中,再将s2中的数据pop
出队列: (1)如果栈B不为空,直接弹出栈B的数据 (2)如果栈B为空,则依次弹出栈A的数据,放入栈B中,再弹出栈B的数据
#include <iostream>
#include <stack>
#include <windows.h>
using namespace std;
template <typename T>
class MyQueue
{
public:
MyQueue( )
{}
~MyQueue( )
{}
void Push( const T& data )
{
s1.push( data );
++size;
}
void Pop( )
{
if ( true == Empty( ) )
throw new exception( "队列为空!\n" );
//我们不采用 将 s1 的元素全部压入 s2 后 再 对s2 进行 一次 pop 的做法。 如下的做法可以 节省一次 入栈的开销
if ( true == s2.empty( ) )
{
while( 1 != s1.size( ) )
{
s2.push( s1.top( ) );
s1.pop( );
}
s1.pop( );
}
else
{
s2.pop( );
}
--size;
}
bool Empty( )
{
if ( true == s1.empty( ) )
if ( true == s2.empty( ) )
return true;
return false;
}
size_t Size( )
{
return size;
}
T& Front( )
{
if ( true == Empty( ) )
throw new exception( "队列为空!\n" );
if ( true == s2.empty( ) )
{
while ( 0 != s1.size( ) )
{
s2.push( s1.top( ) );
s1.pop( );
}
}
return s2.top( );
}
T& Back( )
{
if ( true == Empty( ) )
throw new exception( "队列为空!\n" );
if ( true == s1.empty( ) )
{
while ( 0 != s2.size( ) )
{
s1.push( s2.top( ) );
s2.pop( );
}
}
return s1.top( );
}
private:
stack<T> s1; //注意,不要写成 stack s1; -> stack<T> s1. s1里面存的数据是 T 类型.
stack<T> s2;
size_t size;
};
void Test( )
{
MyQueue<int> q;
cout << q.Empty( ) << endl;
q.Push( 1 );
cout << q.Front( ) << endl;
cout << q.Back( ) << endl;
cout << q.Empty( ) << endl;
q.Push( 2 );
q.Push( 3 );
q.Push( 4 );
q.Push( 5 );
cout << q.Front( ) << endl;
cout << q.Back( ) << endl;
q.Pop( );
cout << q.Front( ) << endl;
cout << q.Back( ) << endl;
q.Pop( );
q.Pop( );
q.Pop( );
cout << q.Front( ) << endl;
cout << q.Back( ) << endl;
q.Pop( );
cout << q.Empty( ) << endl;
}
int main( )
{
Test( );
system( "pause" );
return 0;
}
相关文章推荐
- 《算法》第一章——利用两个栈实现一个队列的push和pop操作
- C++:利用两个栈实现队列操作
- C++:利用两个队列实现栈的操作
- 225. Implement Stack using Queues 用两个队列实现栈的基本操作。
- 二、 编写一个类,用两个栈实现队列,支持队列的基本操作(add,poll,peek)
- 数据结构 编写一个类,用两个栈实现队列,支持队列的基本操作(add 、poll、peek)
- 【数据结构与算法C】利用两个栈S1S2模拟一个队列,用栈的基本操作实线EnQueue,DeQueue,QueueEmpty
- 剑指offer-->面试题7 用两个栈实现队列
- Qt可显示基本的图像类型,利用QImage、QPxmap类可以实现图像的显示,并且利用类中的方法可以实现图像的基本操作(缩放、旋转)。
- IO流的基本操作规律以及利用IO流实现音频文件的切割与合并
- 使用两个Stack类(JDK容器类库中的Stack类)实现一个队列类MyQueue,提供队列的入队列和出队列操作:enQueue和deQueue
- 试用多态实现线性表(队列,串,堆栈),要求具备线性表的基本操作:插入,删除,测长等。
- 两个队列模拟栈的操作(stl实现)
- <table>标签 利用DOM 的方法和属性实现对表格的动态操作
- 用char*实现的一个完整的类,包含类的基本操作:一般构造、拷贝构造、赋值转换、重载 > >,< <
- 利用joda(强大的Java日期操作组件)实现获取两个日期间隔
- 利用hashtable模拟实现权限验证(同map思想-->只能有一个用户名,可重复密码)以及增删查改操作
- 可视化webpart基础开发——利用事件接收器实现同步操作两个文档库(添加、删除、修改文档)
- 可视化webpart基础开发——利用事件接收器实现同步操作两个文档库(添加、删除、修改文档)
- 如何用两个栈来实现一个队列,并分析有关队列操作的运行时间。