一.源代码
#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;
}
}
二.总结
主函数的编写总是出现各种错误,请赐教。