前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >链栈 原

链栈 原

作者头像
青木
发布2018-08-15 15:17:46
1920
发布2018-08-15 15:17:46
举报

链栈就是栈结构的链表形式。其他的操作和数组表示栈一摸一样。 下面是具体的代码实现:

代码语言:javascript
复制
	//测试函数
	//linkedStack.h
#include<iostream>
#include"linkedstack.h"
#include"myexceptions.h"

using namespace std;
int main(int argc, char *argv[])
{
    linkedStack<int> s;

    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);

    cout<<"Stack shoule be 1234,bottom to top"<<endl;

    if(s.empty())
        cout<<"The stack is empty"<<endl;
    else
        cout<<"The stack is not empty"<<endl;

    cout<<"The stack size is "<<s.size()<<endl;

    while(!s.empty())
    {
        cout <<"Stack top is"<<s.top()<<endl;
        s.pop();
        cout<<"Popped top element"<<endl;
    }
    try
    {
        s.pop();
    }
    catch(stackEmpty message)
    {
        cout<<"last pop failed"<<endl;
        message.outputMessage();
    }

    return 0;
}
代码语言:javascript
复制
/*
 * 链栈类
 * linkedStack.h
*/
#ifndef LINKEDSTACK_H
#define LINKEDSTACK_H

#include"stack.h"
#include"myexceptions.h"
#include"chainnode.h"
#include<sstream>

using namespace std;

template<class T>
class linkedStack : public stack<T>
{
public:
    linkedStack(int initialCapacity = 10)
    {
        stackTop = NULL;
        stackSize = 0;
    }
    ~linkedStack();
    bool empty() const
    {
        return stackSize == 0;
    }

    int size() const
    {
        return stackSize;
    }

    T& top()
    {
        if(stackSize == 0)
            throw stackEmpty();
        return stackTop->element;
    }

    void pop();
    void push(const T &theElement)
    {
        stackTop = new chainNode<T>(theElement,stackTop);
                stackSize++;
    }

private:
    chainNode<T>* stackTop;//栈顶指针
    int stackSize;//栈中所含元素个数
};

template<class T>
linkedStack<T>::~linkedStack()
{
    while(stackTop != NULL)
    {
        chainNode<T>* nextNode = stackTop->next;
        delete stackTop;
        stackTop = nextNode;
    }
}

template<class T>
void linkedStack<T>::pop()
{
    if(stackSize == 0)
    {
        throw stackEmpty();
    }

    chainNode<T>* nextNode = stackTop->next;
    delete stackTop;
    stackTop = nextNode;
    stackSize--;
}
#endif
代码语言:javascript
复制
/*
 * 栈类的定义
 * stack.h
*/
#ifndef STACK_H
#define STACK_H

using namespace std;

template<class T>
class stack
{
public:
   virtual ~stack() {}
    //判断栈空
    virtual bool empty() const = 0;

    //计算栈内元素个数
    virtual int size() const  = 0;

    //返回栈顶元素索引
    virtual T& top() = 0;

    //出栈
    virtual void pop() = 0;

    //入栈
    virtual void push(const T& theElement) = 0;
};

#endif // STACK_H
代码语言:javascript
复制
/*
 * 链表节点类
 * chainNode.h
*/
#ifndef CHAINNODE_H
#define CHAINNODE_H
template<class T>
struct chainNode
{
  T element;
  chainNode<T> *next;

  chainNode(){}
  chainNode(const T& element)
  {
      this->element = element;
  }
  chainNode(const T &element,chainNode<T>* next)
  {
      this->element = element;
      this->next = next;
  }
};

#endif // CHAINNODE_H
代码语言:javascript
复制
/*
 * 异常类
 * myExceptions.h
*/
#ifndef MYEXCEPTIONS_H
#define MYEXCEPTIONS_H
#include<string>

using namespace std;
//判断栈空
class stackEmpty
{
public:
    stackEmpty(string theMessage =
            "Invalid operation on empty stack")
    {
        message = theMessage;
    }
    void outputMessage()
    {
        cout <<message<<endl;
    }
private:
    string message;
};

//

#endif // MYEXCEPTIONS_H
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档