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;
(编辑:焦作站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|