const int StackSize = 10;
class Stack
{
public:
Stack(){top = -1;}//构造函数,初始化一个空栈
~Stack(){}//析构函数为空
void Push(char x);//入栈操作,将元素x
//入栈
char Pop();//出栈操作,将栈顶元素弹出
char GetTop();//取栈顶元素(不删除)
char Empty();//判断栈是否为空
char Emp();//清空栈
protected:
char data[StackSize];//存放栈元素的数组
int top;//栈顶指针,为栈顶元素在数组中的下标
};
上面这个是我们学的数据结构,下面的代码是网上的代码,而且大家都这么写,我看不太懂,想请求帮助
template
class Stack
{
public:
Stack(int MaxStackSize=10);
~Stack(){delete []stack;}
bool IsEmpty()const {return top==-1;}
bool IsFull()const {return top==MaxTop;}
T Top()const;
Stack& Add(const T& x);
Stack& Delete(T& x);
int Length(){return top+1;}
void Output(ostream& out) const;
Stack& Divide(Stack& x);
Stack& Merge(Stack& x);
friend istream& operator>>(istream& in,Stack& x);
private:
int top;
int MaxTop;
T *stack;
};为什么要这么写