共享栈

一.源代码

#include<iostream>
using namespace std;


const int stacksize=100;
template<class datatype>
class bothstack
{
public:
bothstack(){top1=-1;top2=stacksize;}
~bothstack(){}
void push(int i,datatype x);
datatype pop(int i);
datatype gettop(int i);
int empty(int i);
private:
datatype data[stacksize];
int top1,top2;
};


template<class datatype>
void bothstack<datatype>::push(int i,datatype x)
{
if(top1==top2-1)throw"up";
if(i==1)data[++top1]=x;
if(i==2)data[--top2]=x;
}


template<class datatype>
datatype bothstack<datatype>::pop(int i)
{
if(i==1){
if(top1==-1)throw"down";
return data[top1--];
}
if(i==2){
if(top2==stacksize)throw"down";
return data[top2++];
}
}


template<class datatype>
datatype bothstack<datatype>::gettop(int i)
{
if(i==1){
if(top1!=-1)
return data[top1];
}
if(i==2){
if(top2!=stacksize)
return data[top2];
}
}


template<class datatype>
int bothstack<datatype>::empty(int i)
{
if(i==1){
if(top1==-1)return 1;
else return 0;
}
if(i==2){
if(top2==stacksize)return 1;
else return 0;
}
}

二.总结

         主函数的编写总是出现各种错误,请赐教。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值