【数据结构】1.线性表的数组描述和链式描述

这篇具有很好参考价值的文章主要介绍了【数据结构】1.线性表的数组描述和链式描述。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1. 线性表抽象类

#pragma once
template <class T>
class LinearList
{
public:
    // 线性表是否为空
    virtual bool empty() const = 0;
    // 线性表大小
    virtual int size() const = 0;
    // 根据ID获取线性表元素
    virtual T& get(int theIndex) const = 0;
    // 根据元素获取元素对应ID
    virtual int indexOf(const T& theElement) const = 0;
    // 删除ID处的元素
    virtual void erase(int theIndex) = 0;
    // 在ID处插入元素
    virtual void insert(int theIndex, const T& theElement) = 0;
    // 输出线性表
    virtual void output() = 0;
};

2. 线性表的数组描述

#include"linearList.h"
#include<iostream>
using namespace std;

template<class T>
class ArrayList :public LinearList<T>
{
protected:
    T* element;                                // 线性表元素指针
    int arrayLength;                        // 容量
    int listSize;                            // 元素个数
    bool checkIndex(int theIndex) const;    // 检查索引是否有效
    void changeLength();                    // 扩充数组长度

public:
    // 构造函数
    ArrayList(int initialCapacity = 10);
    // 拷贝构造函数
    ArrayList(const ArrayList<T>& theList);
    // 析构函数
    ~ArrayList() 
    {
        delete[] element;
    }

    // 线性表是否为空
    bool empty() const { return listSize == 0; }

    // 线性表大小
    int size() const { return listSize; }

    // 线性表容量
    int capacity() const { return arrayLength; }

    // 根据ID获取线性表元素
    T& get(int theIndex) const;

    // 根据元素获取元素对应ID
    int indexOf(const T& theElement) const;

    // 删除ID处的元素
    void erase(int theIndex);

    // 在ID处插入元素
    void insert(int theIndex, const T& theElement);

    // 输出线性表
    void output();

};


// 构造函数
template<class T>
ArrayList<T>::ArrayList(int initialCapacity)
{
    if (initialCapacity < 1) 
    {
        cout << "初始化容量必须大于0" << endl;
        return;
    }
    this->arrayLength = initialCapacity;
    this->element = new T[arrayLength];
    listSize = 0;
}

// 复制构造函数
template<class T>
ArrayList<T>::ArrayList(const ArrayList<T>& theList)
{
    this->arrayLength = theList.arrayLength;
    this->listSize = theList.listSize;
    this->element = new T[arrayLength];
    copy(theList.element, theList.element + listSize, element);
}

// 越界, false 表示越界, true 表示没有越界
template<class T>
bool ArrayList<T>::checkIndex(int theIndex) const
{
    bool ret = !(theIndex < 0 || theIndex > listSize);
    return ret;
}


// 获取元素
template<class T>
T& ArrayList<T>::get(int theIndex) const
{
    if (checkIndex(theIndex))
    {
        return element[theIndex];
    }
}

// 根据元素获取ID
template<class T>
int ArrayList<T>::indexOf(const T& theElement) const
{
    int theIndex = (int)find(element, element + listSize, theElement);
    return theIndex == listSize ? -1 : (theIndex-(int)element)/sizeof(T);
}

// 删除ID处元素
template<class T>
void ArrayList<T>::erase(int theIndex)
{
    if (checkIndex(theIndex))
    {
        copy(element + theIndex + 1, element + listSize, element + theIndex);
        element[--listSize].~T();
    }
}

// 扩充数组长度
template<class T>
void ArrayList<T>::changeLength()
{
    T* temp = new T[arrayLength * 2];
    copy(element, element + arrayLength, temp);
    delete[] element;
    element = temp;
    arrayLength = 2 * arrayLength;
}


// 在ID处插入元素
template<class T>
void ArrayList<T>::insert(int theIndex, const T& theElement)
{
    if (!checkIndex(theIndex))
    {
        cout << "无效索引" << endl;
        return;
    }
    if (listSize == arrayLength)
    {
        changeLength();
    }
    copy_backward(element + theIndex, element + listSize, element + listSize + 1);
    element[theIndex] = theElement;
    listSize++;
}

// 输出线性表
template<class T>
void ArrayList<T>::output()
{
    for (int i = 0; i < listSize; i++)
    {
        cout << element[i] << " ";
    }
    cout << endl;
}

3. 线性表的链式描述

3.1 结点结构体

#pragma once
#include<iostream>
using namespace std;
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;
    }
};

3.2 线性表实现

#include"linearList.h"
#include"chianNode.h"
#include<iostream>
using namespace std;
template<class T>
class Chain :public LinearList<T>
{
protected:
    ChainNode<T>* firstNode;
    int listSize;
    bool checkIndex(int theIndex) const;

public:
    Chain(int initialCapacity = 10);
    Chain(const Chain<T>&);
    ~Chain();

    bool empty() const { return listSize == 0; };
    // 线性表大小
    int size() const { return listSize; }
    // 根据ID获取线性表元素
    T& get(int theIndex) const;
    // 根据元素获取元素对应ID
    int indexOf(const T& theElement) const;
    // 删除ID处的元素
    void erase(int theIndex);
    // 在ID处插入元素
    void insert(int theIndex, const T& theElement);
    // 输出线性表
    void output();
};

// 普通的构造函数
template<class T>
Chain<T>::Chain(int initialCapacity)
{
    if (initialCapacity < 1)
    {
        cout << "初始容量设置必须大于0" << endl;
    }
    firstNode = NULL;
    listSize = 0;
}

// 拷贝构造函数
template<class T>
Chain<T>::Chain(const Chain<T>& theList)
{
    listSize = theList.listSize;
    if (listSize == 0)
    {
        firstNode = NULL;
        return;
    }
    ChainNode<T>* sourceNode = theList.firstNode;
    firstNode = new ChainNode<T>(sourceNode->element);
    sourceNode = sourceNode->next;
    ChainNode<T>* targetNode = firstNode;
    while (sourceNode != NULL)
    {
        targetNode->next = new ChainNode<T>(sourceNode->element);
        targetNode = targetNode->next;
        sourceNode = sourceNode->next;
    }
    targetNode->next = NULL;
}

// 析构函数
template<class T>
Chain<T>::~Chain()
{
    while (firstNode != NULL)
    {
        ChainNode<T>* nextNode = firstNode->next;
        delete firstNode;
        firstNode = nextNode;
    }
}

template<class T>
T& Chain<T>::get(int theIndex) const
{
    if (checkIndex(theIndex))
    {
        ChainNode<T>* currentNode = firstNode;
        for (int i = 0; i < theIndex; i++)
        {
            currentNode = currentNode->next;
        }
        return currentNode->element;
    }
}

template<class T>
int Chain<T>::indexOf(const T& theElement) const
{
    ChainNode<T>* currentNode = firstNode;
    int index = 0;
    while (currentNode->element != theElement && currentNode != NULL)
    {
        currentNode = currentNode->next;
        index++;
    }
    return currentNode == NULL ? -1 : index;
}

template<class T>
void Chain<T>::erase(int theIndex)
{
    if (checkIndex(theIndex))
    {
        ChainNode<T>* deleteNode;
        if (theIndex == 0)
        {
            deleteNode = firstNode;
            firstNode = firstNode->next;
        }
        else if (theIndex < listSize - 1)
        {
            ChainNode<T>* p = firstNode;
            for (int i = 0; i < theIndex - 1; i++)
            {
                p = p->next;
            }
            deleteNode = p->next;
            p->next = p->next->next;
        }
        else
        {
            ChainNode<T>* p = firstNode;
            for (int i = 0; i < theIndex; i++)
            {
                p = p->next;
            }
            deleteNode = p;
            p->next = NULL;
        }
        listSize--;
        delete deleteNode;
    }
}

template<class T>
void Chain<T>::insert(int theIndex, const T& theElement)
{
    if (checkIndex(theIndex))
    {
        if (theIndex == 0)
        {
            firstNode = new ChainNode<T>(theElement, firstNode);
        }
        else
        {
            ChainNode<T>* p = firstNode;
            for (int i = 0; i < theIndex - 1; i++)
            {
                p = p->next;
            }
            p->next = new ChainNode<T>(theElement, p->next);
        }
        listSize++;
    }
}


template<class T>
void Chain<T>::output()
{
    ChainNode<T>* currentNode = firstNode;
    while (currentNode != NULL)
    {
        cout << currentNode->element << " ";
        currentNode = currentNode->next;
    }
    cout << endl;
}

template<class T>
bool Chain<T>::checkIndex(int theIndex) const
{
    bool ret = !(theIndex < 0 || theIndex > listSize);
    return ret;
}

 文章来源地址https://www.toymoban.com/news/detail-711778.html

到了这里,关于【数据结构】1.线性表的数组描述和链式描述的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【玩转408数据结构】线性表——双链表、循环链表和静态链表(线性表的链式表示 下)

            在前面的学习中,我们已经了解到了链表(线性表的链式存储)的一些基本特点,并且深入的研究探讨了单链表的一些特性,我们知道,单链表在实现插入删除上,是要比顺序表方便的,但是,单链表中每个结点仅存在一个指针指向其后续结点,那么如果我们想要找

    2024年04月10日
    浏览(34)
  • 青岛大学_王卓老师【数据结构与算法】Week03_11_线性表的链式表示和实现11_学习笔记

    本文是个人学习笔记,素材来自青岛大学王卓老师的教学视频。 一方面用于学习记录与分享,另一方面是想让更多的人看到这么好的《数据结构与算法》的学习视频。 如有侵权,请留言作删文处理。 课程视频链接: 数据结构与算法基础–第3周11–2.5线性表的链式表示和实现

    2024年02月12日
    浏览(31)
  • 数据结构之线性表的类型运用Linear Lists: 数组,栈,队列,链表

    定义 一个最简单,最基本的数据结构。一个线性表由多个相同类型的元素穿在一次,并且每一个元素都一个前驱(前一个元素)和后继(后一个元素)。 线性表的类型 常见的类型:数组、栈、队列、链表 这些不同数据结构的特性可以解决不同种类的问题 题面 题目描述 有

    2024年02月12日
    浏览(27)
  • 数据结构---链式存储的线性表

    指针    定义:       指针也就是内存地址 ,指针变量是 用来存放内存地址 的 变量 ,在同一CPU构架下,不同类型的指针变量所占用的存储单元长度是相同的,而 存放数据的变量因数据的类型不同,所占用的存储空间长度也不同。 有了指针以后,可以对数据本身,也可以

    2024年02月15日
    浏览(27)
  • 【数据结构】单链表 && 双链表(链式和数组实现)

           🔥🔥 欢迎来到小林的博客!!       🛰️博客主页:✈️小林爱敲代码       🛰️博客专栏:✈️ 数据结构与算法       🛰️社区 :✈️ 进步学堂       🛰️欢迎关注:👍点赞🙌收藏✍️留言 今天给大家带来四个内容,一个

    2024年02月01日
    浏览(31)
  • 【数据结构初阶】六、线性表中的队列(C语言 -- 链式结构实现队列)

    ========================================================================= 相关代码gitee自取 : C语言学习日记: 加油努力 (gitee.com)  ========================================================================= 接上期 : 【数据结构初阶】五、线性表中的栈(C语言 -- 顺序表实现栈)_高高的胖子的博客-CSDN博客  

    2024年02月08日
    浏览(29)
  • 线性表的链式存储结构——链表

    目录 一、顺序表优缺点 二、链表的定义 ①:概念: ②:结点 ③:单链表的定义: ④:头结点: 三 、单链表的C语言结构定义: 四、单链表基本操作: 四.1、遍历单链表 四.2、用malloc函数创建新结点 四.3、尾插法 四.4、头插法 四.5、尾删法 四.6、头删法 优点:我们知道顺

    2024年02月08日
    浏览(31)
  • 【数据结构】线性表(顺序存储和链式存储)两种方法,细节满满,保你学会

    ⭐⭐⭐⭐⭐⭐ 🎊专栏【数据结构】 🍔喜欢的诗句:更喜岷山千里雪 三军过后尽开颜。 🎆音乐分享【勋章】 大一同学小吉,欢迎并且感谢大家指出我的问题🥰 ⭐⭐⭐⭐⭐⭐  目录 ⭐定义:  ⭐ 理解: ⭐存储方式 : ⭐顺序存储的优缺点: 优点: 缺点: ⭐链式存储的优

    2023年04月09日
    浏览(26)
  • 【数据结构初阶】八、非线性表里的二叉树(二叉树的实现 -- C语言链式结构)

    ========================================================================= 相关代码gitee自取 : C语言学习日记: 加油努力 (gitee.com)  ========================================================================= 接上期 : 【数据结构初阶】七、非线性表里的二叉树(堆的实现 -- C语言顺序结构)-CSDN博客  ==========

    2024年02月08日
    浏览(35)
  • 【数据结构】——线性表的相关习题

    1、线性表的顺序存储结构是一种()存储结构。 A、顺序存取 B、随机存取 C、索引存取 D、散列存取 解析: (B) 顺序存储结构 的可以实现 随机存取 ,可以在O(1)内通过首地址和元素序号找到元素,每个元素占用最少的存储空间,其存储密度高,但只能使用相邻的一块存储

    2024年02月14日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包