YLLEN

要去看埃菲尔铁塔的顶

欢迎关注本人微博:t.cn/RGSLVUk

一个简单的栈类

template<class T,int SIZE=100>

class CMyStack

{

public:

CMyStack(void);  

~CMyStack(void);

void push(T d); //入栈

T pop();         //出栈

bool IsEmpty();  //测试是否为空

bool IsFull();    //测试是否栈满

T GetTop();     //获取栈顶元素

void Empty();   //清空栈

private:

T data[SIZE];

int top;  //指向当前栈顶

};


template<class T,int SIZE>

CMyStack<T,SIZE>::CMyStack(void):top(-1) 

{

}

template<class T,int SIZE>

CMyStack<T,SIZE>::~CMyStack(void)

{

}

template<class T,int SIZE>

void CMyStack<T,SIZE>::Empty()

{

memset(data,0,sizeof(data));

top=-1;

}

template<class T,int SIZE>

bool CMyStack<T,SIZE>::IsEmpty()

{

return top==-1? true: false;

}

template<class T,int SIZE>

bool CMyStack<T,SIZE>::IsFull()

{

return top>=SIZE-1?true:false;

}

template<class T,int SIZE>

T  CMyStack<T,SIZE>::GetTop()

{

return data[top];

}

template<class T,int SIZE>

void CMyStack<T,SIZE>::push(T d)

{

if(IsFull())  //压栈失败 返回

exit(-1);

data[++top]=d;

}

template<class T,int SIZE>

T CMyStack<T,SIZE>::pop()

{

if(IsEmpty())

exit(-1);

return data[top--]; 

}



评论

© YLLEN | Powered by LOFTER