当前位置: 移动技术网 > IT编程>开发语言>其他编程 > 算法系列15天速成 第八天 线性表【下】

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

2017年12月08日  | 移动技术网IT编程  | 我要评论
一:线性表的简单回顾        上一篇跟大家聊过“线性表"顺序存储,通过实验,大家也知道,如果我每次向顺序表的头部插入元素,都

一:线性表的简单回顾

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

二:链表

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

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)的操作,这给我们带来了尴尬的局面,所以就有了很多的优化方案,比如:双向链表,循环链表。静态链表等等,这些希望大家在懂得单链表的情况下待深一步的研究。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网