📄 stack.h
字号:
#define MAXSIZE 20
template<class Type>
class stack{//定义一个栈
protected:
Type * p;//用数组的形式存放栈的元素
int imaxsize;//栈所能存放的最大元素个数
int itop;//栈中当前所存放的元素个数
public:
stack(int a=MAXSIZE);//缺省状况下的最大值为MAXSIZE
~stack();
bool push(Type &);//进栈
bool pop(Type &);//出栈
bool checkele(int a,Type & ele);//查看栈中与栈顶元素相距为a个的元素.元素返回在ele中
bool MakeEmpty();//置空栈
int IsEmpty();//判断是否为空
int IsFull();//判断是否为满
};
template<class Type>
stack<Type>::stack(int a)
{
imaxsize=a;itop=-1;
if(!(p=new Type[a]))
cerr<<"内存不足,不能运行此程序";
}
template<class Type>
stack<Type>::~stack(){
delete []p;
}
template<class Type>
bool stack<Type>::push(Type&ele)
{
if(itop<imaxsize)
p[++itop]=ele;
else
return false;
return true;
}
template<class Type>
bool stack<Type>::pop(Type &ele)
{
if(itop+1)//如果为空加一为零
ele=p[itop--];
else
return false ;
return true ;
}
template<class Type>
bool stack<Type>::checkele(int a,Type&ele)
{
if(itop-a>=0)
ele=p[itop-a];
else
return false ;
return true ;
}
template<class Type>
bool stack<Type>::MakeEmpty()
{
if(itop+1)
return false ;
itop=-1;
return true;
}
template<class Type>
int stack<Type>::IsEmpty()//判断是否为空
{
return itop+1;
}
template<class Type>
int stack<Type>::IsFull()
{
return (itop+1)/maxsize;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -