⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 堆栈.txt

📁 利用栈进行相应的操作包括栈的初始化
💻 TXT
字号:
/*=====================================================   
  *******************简单的自定义数组堆栈******************   
  =====================================================*/   
    
  #ifndef   _ARRAYSTACK_H_   
  #define   _ARRAYSTACK_H_   
    
  #include   <exception>   
    
  template<typename   T>   
  class   Stacks   
  {   
  public:   
  Stacks(int   maxArraySize   =   10);   
  ~Stacks(){   delete   []   pStack;   }   
  //元素出栈   
  T   &   Pop(void)   ;   
  //元素入栈   
  void   push(const   T   &   X);   
  //取栈顶元素   
  T   &   Top(void)   const;   
    
  bool   isEmpty(void)   const;   
  bool   isFull(void)   const;   
    
  //-------------------------------------------------------   
  //自定义的异常处理成员类   
  public:   
  class   NoMem:public   std::exception   
  {   
  public:   
  virtual   const   char*   what()   const   throw()   
  {   
  return   "there   is   no   memory!";   
  }   
  };   
    
  class   OutOfBounds:public   std::exception   
  {   
  public:   
  virtual   const   char*   what()   const   throw()   
  {   
  return   "out   of   bound!";   
  }   
  };   
  private:   
  int   currentSize,   maxSize;   
  T   *   pStack;     //存储栈元素的数组   
  };   
  //----------------------------------------------------   
    
  //构造函数定义   
    template<typename   T>   
  Stacks<T>::Stacks(int   maxArraySize)   
  {   
  maxSize   =   maxArraySize;   
  pStack   =   new   T[maxArraySize];   
  currentSize   =   0;   
  }   
  //----------------------------------------------------   
    
  //出栈函数   
      template<typename   T>   
    T   &   Stacks<T>::Pop(void)   
    {   
    if(currentSize   <=   0)     //堆栈已空   
      throw   OutOfBounds();   
    currentSize     =   currentSize   -1;   
    return   pStack[currentSize];     
    }   
  //-----------------------------------------------------   
    
    //取栈顶元素   
    template<typename   T>   
    T   &   Stacks<T>::Top(void)   const   
    {   
    if(currentSize   <=   0)   
    throw   OutOfBounds();   
    return   pStack[currentSize   -   1];   
    }   
  //-----------------------------------------------------   
    
  //将元素压入堆栈   
    template<typename   T>   
    void   Stacks<T>::push(const   T   &   x)   
    {   
          if(currentSize   >=   maxSize)   
  throw   NoMem();   //   空间不足,或数组越界   
  pStack[currentSize]   =   x;   
  currentSize   =   currentSize   +   1;   
    }   
    
    //堆栈是否为空   
    template<typename   T>   
    bool   Stacks<T>::isEmpty(void)   const   
    {   
  return   currentSize   ==   0;   
    }   
    
  //堆栈是否已满   
    template<typename   T>   
  bool   Stacks<T>::isFull(void)   const   
    {   
  return   currentSize   ==   maxSize;   
    }   
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -