数据结构基础(13) --链式栈的设计与实现

    采用链式存储的栈成为链式栈(或简称链栈), 链栈的优点是便于多个栈共享存储空间和提高其效率, 且不存在栈满上溢的情况(因为链栈是靠指针链接到一起,只要内存够大, 则链栈理论上可以存储的元素是没有上限的);

    与顺序栈相比, 由于顺序栈是采用的数组实现, 因此一旦数组填满, 则必须重新申请内存, 并将所有元素”搬家”, 而链栈则省略了这一”耗时耗力”的工作, 但却需要付出附加一个指针的代价;

    链栈通常采用单链表实现, 并规定所有的操作都必须实在单链表的表头进行, 而且w我们的链栈没有头结点, m_top直接指向栈顶元素;

链式栈的图示如下:


链栈节点构造:

  1. template <typename Type>  
  2. class ChainNode  
  3. {  
  4.     template <typename T>  
  5.     friend ostream &operator<<(ostream &os, const LinkStack<T> &stack);  
  6.     friend class LinkStack<Type>;  
  7. private:  
  8.     ChainNode(const Type &_data, ChainNode *_next = NULL)  
  9.         :data(_data), next(_next) {}  
  10.   
  11.     Type data;  
  12.     ChainNode *next;  
  13. };  

链栈设计:

  1. template <typename Type>  
  2. class LinkStack  
  3. {  
  4.     template <typename T>  
  5.     friend ostream &operator<<(ostream &os, const LinkStack<T> &stack);  
  6. public:  
  7.     LinkStack(): m_top(NULL) {}  
  8.     ~LinkStack()  
  9.     {  
  10.         makeEmpty();  
  11.     }  
  12.     bool isEmpty() const  
  13.     {  
  14.         return m_top == NULL;  
  15.     }  
  16.   
  17.     void pop() throw(std::out_of_range);  
  18.     const Type &top() const throw(std::out_of_range);  
  19.     void push(const Type &data);  
  20.     void makeEmpty();  
  21.   
  22. private:  
  23.     ChainNode<Type> *m_top;  
  24. };  

栈的三大操作:

  1. template <typename Type>  
  2. const Type &LinkStack<Type>::top() const  
  3. throw (std::out_of_range)  
  4. {  
  5.     if (isEmpty())  
  6.         throw std::out_of_range("stack is empty, can`t get data");  
  7.   
  8.     return m_top->data;  
  9. }  
  1. template <typename Type>  
  2. void LinkStack<Type>::pop()  
  3. throw (std::out_of_range)  
  4. {  
  5.     if (isEmpty())  
  6.         throw std::out_of_range("stack is empty, can`t delete");  
  7.   
  8.     ChainNode<Type> *deleteNode = m_top;  
  9.     m_top = m_top->next;  
  10.     delete deleteNode;  
  11. }  
  1. template <typename Type>  
  2. void LinkStack<Type>::push(const Type &data)  
  3. {  
  4.     //此处是整个链栈的关键点  
  5.     // 该操作会生成一个节点,  
  6.     // 并自动将m_top上移一格,  
  7.     // 而且将m_top原先指向的节点, 作为新生成的节点的下一节点  
  8.     //注意此处  
  9.     //如果第一次运行push, 则原m_top为NULL  
  10.     // 新m_top指向第一个元素  
  11.     m_top = new ChainNode<Type>(data, m_top);  
  12. }  

清空整个栈:

  1. template <typename Type>  
  2. void LinkStack<Type>::makeEmpty()  
  3. {  
  4.     while (!isEmpty())  
  5.     {  
  6.         pop();  
  7.     }  
  8. }  

输出栈内所有内容:

  1. template <typename Type>  
  2. ostream &operator<<(ostream &os, const LinkStack<Type> &stack)  
  3. {  
  4.     ChainNode<Type> *currentNode = stack.m_top;  
  5.     while (currentNode != NULL)  
  6.     {  
  7.         cout << currentNode->data << ' ';  
  8.         currentNode = currentNode->next;  
  9.     }  
  10.   
  11.     return os;  
  12. }  

测试代码:

  1. int main()  
  2. {  
  3.     LinkStack<int> test;  
  4.     for (int i = 0; i < 10; ++i)  
  5.     {  
  6.         test.push(rand()%100);  
  7.     }  
  8.   
  9.     cout << test << endl;  
  10.     cout << "top = " << test.top() << endl;  
  11.   
  12.     test.pop();  
  13.     cout << "top = " << test.top() << endl;  
  14.   
  15.     test.push(1);  
  16.     cout << "top = " << test.top() << endl;  
  17.   
  18.     while (!test.isEmpty())  
  19.     {  
  20.         test.pop();  
  21.     }  
  22.     cout << test << endl;  
  23.   
  24.     test.push(11);  
  25.     test.push(22);  
  26.     test.push(33);  
  27.     cout << test << endl;  
  28.     test.makeEmpty();  
  29.   
  30.     try  
  31.     {  
  32.         cout << "top = " << test.top() << endl;  
  33.     }  
  34.     catch (const std::exception &e)  
  35.     {  
  36.         cerr << e.what() << endl;  
  37.     }  
  38.   
  39.     return 0;  
  40. }  


原文地址:http://blog.csdn.net/zjf280441589/article/details/42463881

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值