加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 安全 > 正文

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

发布时间:2020-03-14 19:10:23 所属栏目:安全 来源:站长网
导读:上一篇跟大家聊过“线性表顺序存储,通过实验,大家也知道,如果我每次向顺序表的头部插入元素,都会引起痉挛,效率比较低下,第二点我们用顺序存储时,容易受到

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></typeparam>
/// <typeparam></typeparam>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></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></typeparam>
/// <typeparam></typeparam>
/// <param></param>
/// <param></param>
/// <param></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></typeparam>
/// <param></param>
/// <returns></returns>
        public int ChanListLength<T>(Node<T> head)
        {
            int count = 0;

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读