数据结构行编辑程序
#include<iostream>using namespace std;
template <class T>
class Stack
{
private:
int top;
int MaxTop;
T * stack;
public:
Stack(int Max=10);
~Stack(){delete []stack;}
bool Empty()const{return top==-1;}
bool Full();
T Top();
void ClearStack();
Stack<T>&Push(const T & x);
Stack<T>&Pop(T & x );
};
template<class T>
Stack<T>::Stack(int Max)
{
MaxTop=Max-1;
stack=new T[Max];
top=-1;
}
template<class T>
void Stack<T>::ClearStack()
{
top=-1;
}
template<class T>
bool Stack<T>::Full()
{
if(top==MaxTop)
return true;
else
return false;
}
template<class T>
T Stack<T>::Top()
{
if(top==-1)
cout<<"栈空!"<<endl;
else
return stack[top];
}
template<class T>
Stack<T>&Stack<T>::Push(const T & x)
{
if(Full())
cout<<"栈满!"<<endl;
top++;
stack[top]=x;
return *this;
}
template<class T>
Stack<T>&Stack<T>::Pop(T &x)
{
if(top==-1)
cout<<"栈空!"<<endl;
x=stack[top];
top--;
return *this;
}
int main()
{
Stack<char> s;
char ch,c;
ch=getchar();
while(ch!=EOF)
{
while(ch!=EOF && ch!='\n')
{
switch(ch)
{
case '#':s.Pop(c);
break;
case '@':s.ClearStack();
break;
default :s.Push(ch);
break;
}
ch=getchar();
}
s.ClearStack();
if(ch!=EOF)
ch=getchar();
}
return 0;
}
上述程序为什么不能实现操作?