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

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

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

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></typeparam>
/// <param></param>
/// <param></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></typeparam>
/// <param></param>
/// <param></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></typeparam>
/// <param></param>
/// <param></param>
/// <param></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;

(编辑:焦作站长网)

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

热点阅读