不知这程序错在那些地方~~ 望高手指教:) #include<iostream.h> #include<stdio.h> #include<conio.h>
#define MaxLen 100 #define NULL 0
template <class T> class Chain{ friend stack<T>; private: T data; Chain<T> *next; };
template <class T> class stack{ public: stack(){stack=NULL;Maxlen=0;} ~stack(); stack<T>& push(const T& data); bool pop(T& data); int len()const; private: int Maxlen; Chain<T> *stack; };
template <class T> stack<T>::~stack(){ stack<T> *q=stack; while(stack){ q=stack->next; delete stack; stack=q; } }
template <class T> stack<T>& stack<T>::push(const T& data){ if(Maxlen>=MaxLen){ cout<<"The stack is full!"<<endl; }else{ chain<T> *q=new chain<T>; q->data=data;q->next=NULL; stack->next=q; stack=q;
Maxlen++; } return *this; }
template <class T> bool stack<T>::pop(T& data){ if(Maxlen==0){ data=-1; return false; } stack<T> *y=stack; stack=y->next; data=y->data; delete y; Maxlen--;
return true; }
template <class T> int stack<T>::len(){ return Maxlen; }
void main(){ int x,i; stack<int> sta; cout<<"Input five Number:"<<endl; for(i=0;i<5;i++){ cin>>&x>>endl; sta.push(x); }
while(sta.pop(x)){ cout<<x<<endl; } getch(); }