Stack类模板

来源:互联网 发布:sql语句编写 编辑:程序博客网 时间:2024/06/06 03:42
//-------------DStackT.h----------------  #include<iostream>    #ifndef DSTACK  #define DSTACK  template<typename StackElement>class Stack  {  public:      Stack(int numElements=128);      Stack(const Stack<StackElement> & original);      ~Stack();      const Stack & operator=(const Stack<StackElement> &rightHandSide);      bool empty() const;      void push(const StackElement & value);      void display(ostream & out) const;      StackElement top() const;      void pop();  private:      int myTop;      int myCapacity;      StackElement *myArray;        }; #include<new>   #include<cassert>template<typename StackElement>  Stack<StackElement>::Stack(int numElements)  {      assert(numElements>0);      myCapacity=numElements;        myArray=new (nothrow) StackElement[myCapacity];      if(myArray!=0)          myTop=-1;      else      {          cout<<"Inadequate memory to allocate stack \n";              exit(1);      }  }  template<typename StackElement>Stack<StackElement>::Stack(const Stack<StackElement> & original)      :myCapacity(original.myCapacity),myTop(original.myTop)  {      myArray=new (nothrow) StackElement[myCapacity];      if(myArray!=0)          for(int pos=0;pos<=myTop;pos++)              myArray[pos]=original.myArray[pos];      else      {          cerr<<"Inadequate memory to allocate stack ***\n";          exit(1);      }  }   template<typename StackElement>Stack<StackElement>::~Stack()  {      delete [] myArray;  }    template<typename StackElement>const Stack<StackElement> & Stack<StackElement>::operator=(const Stack<StackElement> & rightHandSide)  {      if(this!=&rightHandSide)      {          if(myCapacity!=rightHandSide.myCapacity)          {              delete [] myArray;                myCapacity=rightHandSide.myCapacity;              myArray=new (nothrow) StackElement[myCapacity];              if(myArray==0)              {                  cerr<<"*** Inadequate memory ***\n";                  exit(1);              }          }          myTop=rightHandSide.myTop;          for(int pos=0;pos<=myTop;pos++)              myArray[pos]=rightHandSide.myArray[pos];      }      return *this;  }    template<typename StackElement>bool Stack<StackElement>::empty() const  {      return (myTop==-1);  }    template<typename StackElement>void Stack<StackElement>::push(const StackElement & value)  {      if(myTop<myCapacity-1)      {          myTop++;          myArray[myTop]=value;      }      else      {          cerr<<"***Stack full -- can't add new value ***\n";          exit(1);      }  }    template<typename StackElement>void Stack<StackElement>::display(ostream & out) const  {      for(int i=myTop;i>=0;i--)          out<<myArray[i]<<endl;  }  template<typename StackElement>inline ostream & operator<<(ostream & out,const Stack<StackElement>& st){st.display(out);return out;}template<typename StackElement>StackElement Stack<StackElement>::top() const  {      if(!empty())      {          return (myArray[myTop]);      }      else      {          cerr<<"***Stack is empty -- returning garbage value ***"              <<endl;              StackElement garbage;          return garbage;      }  }    template<typename StackElement>void Stack<StackElement>::pop()  {      if(!empty())          myTop--;      else          cerr<<"*** Stack is empty -- can't remove a value ***\n";  }  #endif  

//-------------DStackT_main.cpp---------------------  #include<iostream>  #include<iomanip>using namespace std;    #include"DStackT.h"  template<typename T>void print(Stack<T> st)  { st.display(cout); }    int main()  {      int cap;      cout<<"Enter stack capacity: ";      cin>>cap;    Stack<int> intSt;Stack<char> charSt;for(int i=1;i<=4;i++)        intSt.push(100*i);  cout<<intSt<<endl;for(char ch='A';ch<='D';ch++)        charSt.push(ch); cout<<charSt<<endl;cout<<"Content of stacks intSt (via print):\n";      print(intSt); cout<<endl;      Stack<int>t;t=intSt;cout<<"Content of stacks t after t=stInt (via print):\n";      print(t); cout<<endl;    cout<<"Stack t empty? "<<boolalpha<<t.empty()<<endl;         cout<<"Top value in t: "<<t.top()<<endl;        while(!t.empty())      {          cout<<"Popping t: "<<t.top()<<endl;          t.pop();      }      cout<<"Stack t empty? "<<t.empty()<<endl;      cout<<"\nNow try to retrieve top value from t."<<endl;      cout<<"Top value in t: "<<t.top()<<endl;      cout<<"\nTrying to pop t: "<<endl;      t.pop();  }  

原创粉丝点击