泛型单链表

泛型单链表

单链表将每个数据分为节点,每个节点存储数据和指向下一个节点的指针。这样数据就不用在内存中使用连续的存储空间,有更大的灵活性。

这里将单链表分为节点类(Node)和链表类(singleLinkedList),由于链表类需要访问到Node类的数据,因此将Node类的数据声明为public,也可以将链表类声明为节点类的友元类。

抽向数据类型(ADT) :(linearList.h)

/*************************************************************************
> File Name       : linearList.h
> Author          : Harold
> Mail            : 2106562095@qq.com
> Github          : www.github.com/Haroldcc
> Created Time    : 2020年02月26日  11时02分58秒
************************************************************************//***** 线性表的抽象定义 *****/#ifndef LINEARLIST_H
#define LINEARLIST_H#include template <typename T>
class linearList
{
public:virtual ~linearList() {}virtual bool isEmpty() const = 0;                         //是否为空virtual int size() const = 0;                             // 元素个数virtual T &getElement(int index) const = 0;               // 根据索引获取元素virtual void setElement(int index, const T &element) = 0; // 根据索引更改元素值virtual int indexOf(const T &element) const = 0;          //指定元素第一次出现的索引virtual void removeByIndex(int index) = 0;                //根据索引删除元素并返回virtual void removeByElement(const T &element) = 0;       // 删除指定元素virtual void add(const T &element) = 0;                   // 在尾部插入元素virtual void add(int index, const T &element) = 0;        // 在指定索引处插入元素virtual void output(std::ostream &out) const = 0;         // 插入输出流out
};#endif

异常类(myException.h)

/*************************************************************************
> File Name       : myExceptions.h
> Author          : Harold
> Mail            : 2106562095@qq.com
> Github          : www.github.com/Haroldcc
> Created Time    : 2020年02月26日  14时32分01秒
************************************************************************//***** 自定义的异常 *****/
#ifndef MYEXCEPTIONS_H
#define MYEXCEPTUONS_H#include 
#include // 非法参数
class illegalParameterValue
{
private:std::string message;public:illegalParameterValue(std::string theMessage = "参数值非法!") : message(theMessage) {}void outputMessage() { std::cout << message << std::endl; }
};// 非法输入
class illegalInputData
{
private:std::string message;public:illegalInputData(std::string theMessage = "非法数据输入!"){message = theMessage;}void outputMessage() { std::cout << message << std::endl; }
};// 非法索引
class illegalIndex
{
private:std::string message;public:illegalIndex(std::string theMessage = "非法索引!") : message(theMessage) {}void outputMessage() { std::cout << message << std::endl; }
};
#endif

单链表头文件(singleLinkedList.h)

/*************************************************************************
> File Name       : singleLinkedList.h
> Author          : Harold
> Mail            : 2106562095@qq.com
> Github          : www.github.com/Haroldcc
> Created Time    : 2020年02月27日  10时12分02秒
************************************************************************//***** 单链表 *****/#ifndef SINGLELINKEDLIST_H
#define SINGLELINKEDLIST_H#include "linearList.h"   // ADT
#include "myExceptions.h" // 异常类
#include         // for ostringstream/*** 链表节点类 ***/
template <typename T>
class Node
{
public:T element;Node<T> *next;Node(){};Node(const T &element) { this->ele


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部