practice2.h文件
#ifndef PRACTICE2_H_INCLUDED#define PRACTICE2_H_INCLUDEDtemplateclass LinkedStack;template class ChainNode//栈里面的每个节点{ friend class LinkedStack ;private: T data; ChainNode *link; ChainNode(const T& theData,ChainNode *n=0):data(theData),link(n) {}};template class LinkedStack{public: LinkedStack():top(0) {} ~LinkedStack() {MakeEmpty();} bool IsEmpty() const; T& Top() const; void Push(const T& e); void Pop(); void MakeEmpty();private: ChainNode *top;};template bool LinkedStack ::IsEmpty() const{ return top==0;}template void LinkedStack ::Push(const T &e){ top=new ChainNode (e,top);}template T& LinkedStack ::Top() const{ if(this->IsEmpty()) { throw "Stack is Empty"; } return top->data;}template void LinkedStack ::Pop(){ if(this->IsEmpty()) throw"is Empty"; ChainNode *delNode=top; top=top->link; delete delNode;//如果不删除的话会产生内存泄漏 和push的new对应}template void LinkedStack ::MakeEmpty(){ while(!IsEmpty()) Pop();}#endif // PRACTICE2_H_INCLUDED
practice1.cpp
#include#include "practice2.h"using namespace std;int main(){ LinkedStack s; s.Push(10); cout< <