• stack implement C++


    Stack Source Code in C++
     
    Special thanks to Eric Suh for contributing the following implementation of a stack. This implementation uses templates to faciliate generic programming.
    / * 
         -------------------------------------------------------------------
        |                                                                   |
        |    Stack Class                                                    |
        |    ===========================================================    |
        |    This Stack has been implemented with templates to allow it     |
        |    to accomodate virtually any data type, and the size of the     |
        |    Stack is determined dynamically at runtime.                    |
        |                                                                   |
        |    There is also a new function: peek(), which, given a whole     |
        |    number 'Depth', returns the Stack element which is 'Depth'     |
        |    levels from the top.                                           |
        |                                                                   |
         -------------------------------------------------------------------
    */
    #ifndef __StackClassH__
    #define __StackClassH__
     
    #include <assert.h>    // For error-checking purposes
     
    //-------------------------------------------------
    // Main structure of Stack Class:
    //-------------------------------------------------
     
    template <class Elem>
    class Stack
    {
      public:
        Stack(int MaxSize=500);
        Stack(const Stack<Elem> &OtherStack);
        ~Stack(void);
     
        inline void        Push(const Elem &Item); // Adds Item to the top
        inline Elem        Pop(void);              // Returns Item from the top
        inline const Elem &Peek(int Depth) const;  // Peek a depth downwards
     
      protected:
        Elem     *Data;           // The actual Data array
        int       CurrElemNum;    // The current number of elements
        const int MAX_NUM;        // Maximum number of elements
    };
     
    //-------------------------------------------------
    // Implementation of Stack Class:
    //-------------------------------------------------
     
    // Stack Constructor function
    template <class Elem>
    Stack<Elem>::Stack(int MaxSize) :
        MAX_NUM( MaxSize )    // Initialize the constant
    {
      Data = new Elem[MAX_NUM];
      CurrElemNum = 0;
    }
     
    // Stack Destructor function
    template <class Elem>
    Stack<Elem>::~Stack(void)
    {
      delete[] Data;
    }
     
    // Push() function
    template <class Elem>
    inline void Stack<Elem>::Push(const Elem &Item)
    {
      // Error Check: Make sure we aren't exceeding the maximum storage space
      assert(CurrElemNum < MAX_NUM);
      
      Data[CurrElemNum++] = Item;
    }
     
    // Pop() function
    template <class Elem>
    inline Elem Stack<Elem>::Pop(void)
    {
      // Error Check: Make sure we aren't popping from an empty Stack
      assert(CurrElemNum > 0);
     
      return Data[--CurrElemNum];
    }
     
    // Peek() function
    template <class Elem>
    inline const Elem &Stack<Elem>::Peek(int Depth) const
    {
      // Error Check: Make sure the depth doesn't exceed the number of elements
      assert(Depth < CurrElemNum);
     
      return Data[ CurrElemNum - (Depth + 1) ];
    }
     
    #endif
  • 相关阅读:
    node错误: primordials is not defined
    单片机TTL转RS232模块DB9数据线接口排针接口多接口方便连接
    单片机串口自适应电平5V TTL电平兼容转换3.3V电平串口转换NMOS管
    USB串口转RS485转换器工业级usb串口转RS485模块转换器串口通讯
    Kafka丢数据、重复消费、顺序消费的问题
    纪念第一次做的拉花拿铁
    《奢侈的理由》总结
    【算法框架套路】回溯算法(暴力穷举的艺术)
    svg中矩形旋转问题
    性能测试工具集锦
  • 原文地址:https://www.cnblogs.com/nickchan/p/3104410.html
Copyright © 2020-2023  润新知