数据结构基础(6) --顺序栈的设计与实现

栈是一种只允许在一端进行插入或删除操作的线性表.其特点为:先进后出(FILO)/后进先出(LIFO);

 

栈 VS. 队列

    栈和队列都是动态集合, 但在栈中, 可以去掉的是最近插入的那一个,:栈实现了一种后进先出(last-in, first-out)的策略;类似的, 在队列中, 可以去掉的元素总是在集合中存在时间最长的那一个:队列实现了先进先出(first-in, first-out)的策略[下一篇我们着重复习队列].

 

栈的示意图:


[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //顺序栈的实现与解析  
  2. template <typename Type>  
  3. class MyStack  
  4. {  
  5.     template <typename T>  
  6.     friend ostream &operator<<(std::ostream &os, const MyStack<T> &stack);  
  7. public:  
  8.     MyStack(int stackCapacity = 16);  
  9.     ~MyStack();  
  10.   
  11.     bool isEmpty() const;  
  12.     void push(const Type &item);  
  13.     void pop() throw (std::range_error);  
  14.     const Type &top() const throw(std::range_error);  
  15.   
  16. private:  
  17.     Type *m_stack;  
  18.     int m_top;  
  19.     int m_capacity;  
  20. };  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. template <typename Type>  
  2. MyStack<Type>::MyStack(int stackCapacity):m_capacity(stackCapacity)  
  3. {  
  4.     if (m_capacity < 1)  
  5.         throw std::range_error("new size must >= 1");  
  6.   
  7.     //申请内存并构造对象  
  8.     m_stack = new Type[m_capacity];  
  9.     if (m_stack == NULL)  
  10.         throw std::bad_alloc();  
  11.   
  12.     m_top = -1;  
  13. }  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. template <typename Type>  
  2. MyStack<Type>::~MyStack()  
  3. {  
  4.     //析构对象并释放内存  
  5.     delete []m_stack;  
  6.     m_stack = NULL;  
  7.     m_top = -1; //将top指针指向无效  
  8.     m_capacity = 0;  
  9. }  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //全局函数:数组放大/缩小  
  2. template <typename Type>  
  3. static void changeSize1D(Type *&array, int oldSize, int newSize)  
  4. throw (std::range_error, std::bad_alloc)  
  5. {  
  6.     if (newSize < 0)  
  7.         throw std::range_error("new size must >= 0");  
  8.   
  9.     Type *tmp = new Type[newSize];  
  10.     if (tmp == NULL)  
  11.         throw std::bad_alloc();  
  12.   
  13.     int minSize = std::min(oldSize, newSize);  
  14.     std::copy(array, array+minSize, tmp);  
  15.   
  16.     delete []array; //将原数组释放掉  
  17.     array = tmp;    //将原指针指向新申请的数组  
  18. }  
  19.   
  20. template <typename Type>  
  21. void MyStack<Type>::push(const Type &item)  
  22. {  
  23.     //数组最大容量为m_capacity, 因此数组的最大下标为m_capacity-1  
  24.     if (m_top  >= m_capacity-1)  
  25.     {  
  26.         changeSize1D(m_stack, m_capacity, m_capacity * 2);  //一次扩容2倍  
  27.         m_capacity *= 2;  
  28.     }  
  29.     //将元素插入栈顶  
  30.     m_stack[++ m_top] = item;  
  31. }  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //栈是否为空  
  2. template <typename Type>  
  3. inline bool MyStack<Type>::isEmpty() const  
  4. {  
  5.     return -1 == m_top;  
  6. }  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. template <typename Type>  
  2. inline const Type &MyStack<Type>::top() const  
  3. throw (std::range_error)  
  4. {  
  5.     if (isEmpty())  
  6.         throw std::range_error("stack is empty");  
  7.   
  8.     return m_stack[m_top];  //返回栈顶元素  
  9. }  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. template <typename Type>  
  2. inline void MyStack<Type>::pop()  
  3. throw (std::range_error)  
  4. {  
  5.     if (isEmpty())  
  6.         throw std::range_error("stack is empty");  
  7.   
  8.     //注意:如果该栈保存的对象类型的元素, 则需要显示调用其析构函数,  
  9.     //同时还需要将栈顶指针下移  
  10.     m_stack[m_top --].~Type();  
  11. }  
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //输出栈的所有内容,以便测试  
  2. template <typename Type>  
  3. ostream &operator<<(ostream &os, const MyStack<Type> &stack)  
  4. {  
  5.     os << stack.m_stack[0];  
  6.     for (int i = 1; i <= stack.m_top; ++i)  
  7.         os << ' ' << stack.m_stack[i];  
  8.   
  9.     return os;  
  10. }  

附-测试代码

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. int main()  
  2. {  
  3.     MyStack<int> iStack;  
  4.     iStack.push(10);  
  5.     iStack.push(22);  
  6.     iStack.push(15);  
  7.   
  8.     cout << iStack << endl;  
  9.     try  
  10.     {  
  11.         cout << "Top = " << iStack.top() << endl;  
  12.         iStack.pop();  
  13.         cout << "Top = " << iStack.top() << endl;  
  14.         iStack.pop();  
  15.         cout << "Top = " << iStack.top() << endl;  
  16.         iStack.pop();  
  17.         cout << "Top = " << iStack.top() << endl;  
  18.     }  
  19.     catch (const std::exception &e)  
  20.     {  
  21.         cout << e.what() << endl;  
  22.     }  
  23. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值