|
|
堆栈数据结构 stack.h |
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
/////////////////////////// // // // 堆栈数据结构 stack.h // // // //////////////////////////
#include<iostream.h>
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):link(L),data(D){} };
template<class Type> class Stack { public: Stack():top(NULL),NumItem(0){} void Push(Type item); Type Pop(); Type GetTop(); void MakeEmpty(); bool ISEmpty(); int GetNum(); private: int NumItem; StackNode<Type> *top; };
template<class Type> void Stack<Type>::Push(Type item) { top=new StackNode<Type>(item,top); NumItem++; }
template<class Type> Type Stack<Type>::Pop() { StackNode<Type> *p; Type temp; temp=top->data; p=top; top=top->link; delete p; NumItem--; return temp;
}
template<class Type> Type Stack<Type>::GetTop() { return top->data; }
template<class Type> bool Stack<Type>::ISEmpty() { return top==NULL; }
template<class Type> void Stack<Type>::MakeEmpty() { delete top; }
template<class Type> int Stack<Type>::GetNum() { return NumItem; }

|
|
相关文章:相关软件: |
|