顺序栈

template<class T>

class arrStack:public Stack <T>{

private:

int mSize;

int top;

T *st;

public:

arrStack(int size){

mSize=size;

top=-1;

st=new T[mSize];

}

arrStack(){

top=-1;

}

~arrStack(){

delete[]st;

}

void clear(){

top=-1;

}

bool push(const T item)//入栈

{

if(top==mSize-1){

cout<<"栈满了,不可以输入"<<endl;

return false;

}else

{

st[++top]=item;

return true;

}

}

 

    bool pop(T & item)//出栈

{

if(top==-1){

cout<<"栈是空的,不能出栈"<<endl;

}else{

item=st[top--];

return true;

}

 

   }

};

 

//如果栈满了然后非要想进栈可以如下定 义pus

template <class T>

bool arrStack<T>::push(const T item){

if(top==mSize-1)

{

T *newst=new T[mSize*2];

for(int i=0;i<mSize;i++)

{

newst[i]=st[i];

}

delete []st;

st=newst;

mSize*=2;

}

}

 

 

// 1. top从0或1开始  2.  mSize-1=maxtop    3.删除就是item=st[top--]  加入就是st[++top]=item

你可能感兴趣的:(delete,Class)