我这个栈有什么问题?
头文件stack1.h
#include<iostream>
using namespace std;
template <class Type> class Stack;
template <class Type> class StackNode {
friend class Stack <Type>;
private:
Type data;
StackNode <Type> *link;
StackNode (Type d=0; StackNode <Type> *l=NULL): data(d),link(l) {} //构造函数
};
template <class Type> class Stack {
public:
Stack() : top (NULL) {}
~Stack();
void Push ( const Type & item);
Type Pop ();
Type Gettop() ;
void MakeEmpty() {top=NULL;};
int IsEmpty () const {return top ==NULL;}
private:
StackNode <Type> * top;
};
template <class type> Stack<Type>::~Stack(){
StackNode<Type> * p;
while ( top!=NULL) {p=top; top=top->link; delete p;}
}
template <class Type> Stack<Type>::Push (const Type & item){
top = new StackNode <Type> (item,top);
}
template <class Type> Type Stack<Type>::Pop() {
assert(!IsEmpty());
StackNode <Type> *p=top;
Type retvalue =p->data;
top = top->link;
delete p;
return retvalue;
}
template <class Type> Type Stack<Type> :: Gettop() {
assert (!IsEmpty());
return top->data;
}
执行文件
002.cpp
#include<iostream>
#include"stack1.h"
using namespace std;
int main(){
Stack a;
a.Push(2);
a.Push(5);
cout<<a.Gettop()<<end;
return 0;
}