利用两个栈来构造队列
来源:互联网 发布:退火算法实例 编辑:程序博客网 时间:2024/05/16 04:49
#include<iostream>
using namespace std;
/************************************************************/
//define stack class
//st_p ready to push data
/***********************************************************/
#define stack_size 10
typedef char ELMTYPE;
class stack
{
private:
ELMTYPE *st_p;
ELMTYPE *st_bot;
ELMTYPE *st_top;
public:
stack()
{
st_p=new ELMTYPE(stack_size);
if(st_p==NULL){cout<<"Out of memory!";exit(1);}
st_top=st_p;
st_bot=st_p+stack_size-1;
}
/************************************************************/
//push
/***********************************************************/
void push(ELMTYPE num)
{
if(st_p>st_bot){cout<<"The stack is full!";exit(1);}
*st_p=num;
st_p++; //st_p++;just same ++st_p ;
}
/************************************************************/
//pop funcition
/***********************************************************/
ELMTYPE pop( )
{
if(st_p<st_top){cout<<"The stack is empty!";exit(1);}
st_p--; //st_p--;just same --st_p ;
return *st_p;
}
};
/************************************************************/
//class queue
/***********************************************************/
class queue
{
private:
int count;
stack s1;
stack s2;
public:
queue(){count=0;}
void m_push(ELMTYPE x)
{
s1.push(x);
count++;
}
ELMTYPE m_pop()
{
int i;
ELMTYPE temp;
for(i=0;i<count;i++){s2.push(s1.pop());}
temp=s2.pop();
count--;
for(i=0;i<count;i++){s1.push(s2.pop());}
return temp;
}
};
/************************************************************/
//mian funcition
/***********************************************************/
int main(void)
{
queue q1;
q1.m_push('a');
q1.m_push('b');
q1.m_push('c');
q1.m_push('d');
q1.m_push('f');
q1.m_push('g');
cout<<q1.m_pop();
cout<<q1.m_pop();
cout<<q1.m_pop();
cout<<q1.m_pop();
cout<<q1.m_pop();
cout<<q1.m_pop();
while(1);
}
- 利用两个栈来构造队列
- 两个栈构造队列
- 两个栈构造一个队列 || 两个队列构造一个栈
- 04 两个栈构造队列 & 两个队列构造栈
- 两个栈来实现队列
- 两个栈来模拟队列
- 两个栈来模拟队列
- 利用两个栈模拟队列
- 用两个栈来模拟队列
- 用两个队列来实现栈
- 用两个栈来实现一个队列
- 用两个栈来实现一个队列()
- 用两个栈来实现一个队列
- Java两个栈来实现一个队列
- 用两个栈来实现队列
- 用两个栈来实现一个队列
- 用两个栈来实现一个队列
- 用两个栈来实现队列
- 迭代器模式
- 随机数生成仪
- [Answers] Prolog
- 很久之前自己写的flash太空游戏
- 用Prolog和Turbo Prolog语言开发专家系统
- 利用两个栈来构造队列
- ssh蜜罐
- mysql服务删除
- 开源OCR引擎Tesseract 3.0发布
- 关于099.PKU信科院2002-2003本科期末卷平面构图点问题
- Android学习之menu
- The Most Important Algorithms
- Windows Service的建立和安装调试以及定时器的用法
- 数据库并发控制的三个问题