利用两个栈来构造队列

#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值