- //顺序栈的实现与解析
- template <typename Type>
- class MyStack
- {
- template <typename T>
- friend ostream &operator<<(std::ostream &os, const MyStack<T> &stack);
- public:
- MyStack(int stackCapacity = 16);
- ~MyStack();
- bool isEmpty() const;
- void push(const Type &item); //入栈
- void pop() throw (std::range_error); //出栈
- const Type &top() const throw(std::range_error); //获取栈顶元素
- private:
- Type *m_stack;
- int m_top;
- int m_capacity;
- };
栈是一种先进后出的顺序表,初始的top应该是-1,每放进一个元素,top应该加1,如果容量不够了,应该在入栈里扩容。