侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

算法系列15天速成 第八天 线性表【下】

技术  /  管理员 发布于 7年前   175

一:线性表的简单回顾

       上一篇跟大家聊过“线性表"顺序存储,通过实验,大家也知道,如果我每次向顺序表的头部插入元素,都会引起痉挛,效率比较低下,第二点我们用顺序存储时,容易受到长度的限制,反之就会造成空间资源的浪费。

二:链表

      对于顺序表存在的若干问题,链表都给出了相应的解决方案。

1. 概念:其实链表的“每个节点”都包含一个”数据域“和”指针域“。

            ”数据域“中包含当前的数据。

            ”指针域“中包含下一个节点的指针。

            ”头指针”也就是head,指向头结点数据。

            “末节点“作为单向链表,因为是最后一个节点,通常设置指针域为null。

代码段如下:

复制代码 代码如下:

#region 链表节点的数据结构
/// <summary>
/// 链表节点的数据结构
/// </summary>
    public class Node<T>
    {
 7/// <summary>
/// 节点的数据域
/// </summary>
        public T data;

/// <summary>
/// 节点的指针域
/// </summary>
        public Node<T> next;
    }
    #endregion

2.常用操作:

    链表的常用操作一般有:

           ①添加节点到链接尾,②添加节点到链表头,③插入节点。

           ④删除节点,⑤按关键字查找节点,⑥取链表长度。

<1> 添加节点到链接尾:

          前面已经说过,链表是采用指针来指向下一个元素,所以说要想找到链表最后一个节点,必须从头指针开始一步一步向后找,少不了一个for循环,所以时间复杂度为O(N)。

代码段如下:

复制代码 代码如下:

#region 将节点添加到链表的末尾
        /// <summary>
/// 将节点添加到链表的末尾
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="head"></param>
/// <param name="data"></param>
/// <returns></returns>
        public Node<T> ChainListAddEnd<T>(Node<T> head, T data)
        {
            Node<T> node = new Node<T>();

            node.data = data;
            node.next = null;

            ///说明是一个空链表
            if (head == null)
            {
                head = node;
                return head;
            }

            //获取当前链表的最后一个节点
            ChainListGetLast(head).next = node;

            return head;
        }
#endregion
#region 得到当前链表的最后一个节点
        /// <summary>
/// 得到当前链表的最后一个节点
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="head"></param>
/// <returns></returns>
        public Node<T> ChainListGetLast<T>(Node<T> head)
        {
            if (head.next == null)
                return head;
            return ChainListGetLast(head.next);
        }
        #endregion

<2> 添加节点到链表头:

          大家现在都知道,链表是采用指针指向的,要想将元素插入链表头,其实还是很简单的,

      思想就是:① 将head的next指针给新增节点的next。②将整个新增节点给head的next。

      所以可以看出,此种添加的时间复杂度为O(1)。

效果图:

代码段如下:

复制代码 代码如下:

1#region 将节点添加到链表的开头
/// <summary>
/// 将节点添加到链表的开头
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="chainList"></param>
/// <param name="data"></param>
/// <returns></returns>
        public Node<T> ChainListAddFirst<T>(Node<T> head, T data)
        {
            Node<T> node = new Node<T>();

            node.data = data;
            node.next = head;

            head = node;

            return head;

        }
        #endregion

<3> 插入节点:

           其实这个思想跟插入到”首节点“是一个模式,不过多了一步就是要找到当前节点的操作。然后找到

      这个节点的花费是O(N)。上图上代码,大家一看就明白。

效果图:

代码段:

复制代码 代码如下:

#region 将节点插入到指定位置
/// <summary>
/// 将节点插入到指定位置
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="head"></param>
/// <param name="currentNode"></param>
/// <param name="data"></param>
/// <returns></returns>
        public Node<T> ChainListInsert<T, W>(Node<T> head, string key, Func<T, W> where, T data) where W : IComparable
        {
            if (head == null)
                return null;

            if (where(head.data).CompareTo(key) == 0)
            {
                Node<T> node = new Node<T>();

                node.data = data;

                node.next = head.next;

                head.next = node;
            }

            ChainListInsert(head.next, key, where, data);

            return head;
        }
        #endregion

<4> 删除节点:

        这个也比较简单,不解释,图跟代码更具有说服力,口头表达反而让人一头雾水。
        当然时间复杂度就为O(N),N是来自于查找到要删除的节点。

效果图:

代码段:

复制代码 代码如下:

#region 将指定关键字的节点删除
        /// <summary>
/// 将指定关键字的节点删除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="W"></typeparam>
/// <param name="head"></param>
/// <param name="key"></param>
/// <param name="where"></param>
/// <param name="data"></param>
/// <returns></returns>
        public Node<T> ChainListDelete<T, W>(Node<T> head, string key, Func<T, W> where) where W : IComparable
        {
            if (head == null)
                return null;

            //这是针对只有一个节点的解决方案
            if (where(head.data).CompareTo(key) == 0)
            {
                if (head.next != null)
                    head = head.next;
                else
                    return head = null;
            }
            else
            {
                //判断一下此节点是否是要删除的节点的前一节点
                while (head.next != null && where(head.next.data).CompareTo(key) == 0)
                {
                    //将删除节点的next域指向前一节点
                    head.next = head.next.next;
                }
            }

            ChainListDelete(head.next, key, where);

            return head;
        }
        #endregion

<5> 按关键字查找节点:

         这个思想已经包含到“插入节点”和“删除节点”的具体运用中的,其时间复杂度为O(N)。

代码段:

复制代码 代码如下:

#region 通过关键字查找指定的节点
        /// <summary>
/// 通过关键字查找指定的节点
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="W"></typeparam>
/// <param name="head"></param>
/// <param name="key"></param>
/// <param name="where"></param>
/// <returns></returns>
        public Node<T> ChainListFindByKey<T, W>(Node<T> head, string key, Func<T, W> where) where W : IComparable
        {
            if (head == null)
                return null;

            if (where(head.data).CompareTo(key) == 0)
                return head;

            return ChainListFindByKey<T, W>(head.next, key, where);
        }
        #endregion

<6> 取链表长度:

          在单链表的操作中,取链表长度还是比较纠结的,因为他不像顺序表那样是在内存中连续存储的,

      因此我们就纠结的遍历一下链表的总长度。时间复杂度为O(N)。

代码段:

复制代码 代码如下:

#region 获取链表的长度
        /// <summary>
///// 获取链表的长度
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="head"></param>
/// <returns></returns>
        public int ChanListLength<T>(Node<T> head)
        {
            int count = 0;

            while (head != null)
            {
                ++count;
                head = head.next;
            }

            return count;
        }
        #endregion

好了,最后上一下总的运行代码:

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChainList
{
    class Program
    {
        static void Main(string[] args)
        {
            ChainList chainList = new ChainList();

            Node<Student> node = null;

            Console.WriteLine("将三条数据添加到链表的尾部:\n");

            //将数据添加到链表的尾部
            node = chainList.ChainListAddEnd(node, new Student() { ID = 2, Name = "hxc520", Age = 23 });
            node = chainList.ChainListAddEnd(node, new Student() { ID = 3, Name = "博客园", Age = 33 });
            node = chainList.ChainListAddEnd(node, new Student() { ID = 5, Name = "一线码农", Age = 23 });

            Dispaly(node);

            Console.WriteLine("将ID=1的数据插入到链表开头:\n");

            //将ID=1的数据插入到链表开头
            node = chainList.ChainListAddFirst(node, new Student() { ID = 1, Name = "i can fly", Age = 23 });

            Dispaly(node);

            Console.WriteLine("查找Name=“一线码农”的节点\n");

            //查找Name=“一线码农”的节点
            var result = chainList.ChainListFindByKey(node, "一线码农", i => i.Name);

            DisplaySingle(node);

            Console.WriteLine("将”ID=4“的实体插入到“博客园”这个节点的之后\n");

            //将”ID=4“的实体插入到"博客园"这个节点的之后
            node = chainList.ChainListInsert(node, "博客园", i => i.Name, new Student() { ID = 4, Name = "51cto", Age = 30 });

            Dispaly(node);

            Console.WriteLine("删除Name=‘51cto‘的节点数据\n");

            //删除Name=‘51cto‘的节点数据
            node = chainList.ChainListDelete(node, "51cto", i => i.Name);

            Dispaly(node);

            Console.WriteLine("获取链表的个数:" + chainList.ChanListLength(node));
        }

        //输出数据
        public static void Dispaly(Node<Student> head)
        {
            Console.WriteLine("******************* 链表数据如下 *******************");
            var tempNode = head;

            while (tempNode != null)
            {
                Console.WriteLine("ID:" + tempNode.data.ID + ", Name:" + tempNode.data.Name + ",Age:" + tempNode.data.Age);
                tempNode = tempNode.next;
            }

            Console.WriteLine("******************* 链表数据展示完毕 *******************\n");
        }

        //展示当前节点数据
        public static void DisplaySingle(Node<Student> head)
        {
            if (head != null)
                Console.WriteLine("ID:" + head.data.ID + ", Name:" + head.data.Name + ",Age:" + head.data.Age);
            else
                Console.WriteLine("未查找到数据!");
        }
    }

    #region 学生数据实体
    /// <summary>
/// 学生数据实体
/// </summary>
    public class Student
    {
        public int ID { get; set; }

        public string Name { get; set; }

        public int Age { get; set; }
    }
    #endregion

    #region 链表节点的数据结构
    /// <summary>
/// 链表节点的数据结构
/// </summary>
    public class Node<T>
    {
        /// <summary>
/// 节点的数据域
/// </summary>
        public T data;

        /// <summary>
/// 节点的指针域
/// </summary>
        public Node<T> next;
    }
    #endregion

    #region 链表的相关操作
    /// <summary>
/// 链表的相关操作
/// </summary>
    public class ChainList
    {
        #region 将节点添加到链表的末尾
        /// <summary>
/// 将节点添加到链表的末尾
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="head"></param>
/// <param name="data"></param>
/// <returns></returns>
        public Node<T> ChainListAddEnd<T>(Node<T> head, T data)
        {
            Node<T> node = new Node<T>();

            node.data = data;
            node.next = null;

            ///说明是一个空链表
            if (head == null)
            {
                head = node;
                return head;
            }

            //获取当前链表的最后一个节点
            ChainListGetLast(head).next = node;

            return head;
        }
        #endregion

        #region 将节点添加到链表的开头
        /// <summary>
/// 将节点添加到链表的开头
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="chainList"></param>
/// <param name="data"></param>
/// <returns></returns>
        public Node<T> ChainListAddFirst<T>(Node<T> head, T data)
        {
            Node<T> node = new Node<T>();

            node.data = data;
            node.next = head;

            head = node;

            return head;

        }
        #endregion

        #region 将节点插入到指定位置
        /// <summary>
/// 将节点插入到指定位置
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="head"></param>
/// <param name="currentNode"></param>
/// <param name="data"></param>
/// <returns></returns>
        public Node<T> ChainListInsert<T, W>(Node<T> head, string key, Func<T, W> where, T data) where W : IComparable
        {
            if (head == null)
                return null;

            if (where(head.data).CompareTo(key) == 0)
            {
                Node<T> node = new Node<T>();

                node.data = data;

                node.next = head.next;

                head.next = node;
            }

            ChainListInsert(head.next, key, where, data);

            return head;
        }
        #endregion

        #region 将指定关键字的节点删除
        /// <summary>
/// 将指定关键字的节点删除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="W"></typeparam>
/// <param name="head"></param>
/// <param name="key"></param>
/// <param name="where"></param>
/// <param name="data"></param>
/// <returns></returns>
        public Node<T> ChainListDelete<T, W>(Node<T> head, string key, Func<T, W> where) where W : IComparable
        {
            if (head == null)
                return null;

            //这是针对只有一个节点的解决方案
            if (where(head.data).CompareTo(key) == 0)
            {
                if (head.next != null)
                    head = head.next;
                else
                    return head = null;
            }
            else
            {
                //判断一下此节点是否是要删除的节点的前一节点
                if (head.next != null && where(head.next.data).CompareTo(key) == 0)
                {
                    //将删除节点的next域指向前一节点
                    head.next = head.next.next;
                }
            }

            ChainListDelete(head.next, key, where);

            return head;
        }
        #endregion

        #region 通过关键字查找指定的节点
        /// <summary>
/// 通过关键字查找指定的节点
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="W"></typeparam>
/// <param name="head"></param>
/// <param name="key"></param>
/// <param name="where"></param>
/// <returns></returns>
        public Node<T> ChainListFindByKey<T, W>(Node<T> head, string key, Func<T, W> where) where W : IComparable
        {
            if (head == null)
                return null;

            if (where(head.data).CompareTo(key) == 0)
                return head;

            return ChainListFindByKey<T, W>(head.next, key, where);
        }
        #endregion

        #region 获取链表的长度
        /// <summary>
///// 获取链表的长度
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="head"></param>
/// <returns></returns>
        public int ChanListLength<T>(Node<T> head)
        {
            int count = 0;

            while (head != null)
            {
                ++count;
                head = head.next;
            }

            return count;
        }
        #endregion

        #region 得到当前链表的最后一个节点
        /// <summary>
/// 得到当前链表的最后一个节点
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="head"></param>
/// <returns></returns>
        public Node<T> ChainListGetLast<T>(Node<T> head)
        {
            if (head.next == null)
                return head;
            return ChainListGetLast(head.next);
        }
        #endregion

    }
    #endregion
}

运行结果:

当然,单链表操作中有很多是O(N)的操作,这给我们带来了尴尬的局面,所以就有了很多的优化方案,比如:双向链表,循环链表。静态链表等等,这些希望大家在懂得单链表的情况下待深一步的研究。


  • 上一条:
    算法系列15天速成 第十天 栈
    下一条:
    算法系列15天速成 第九天 队列
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(0个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客