行业知识
C#单向链表实现:用泛型类在当前位置插入新数据的方法Insert()
一、涉及到的知识点
1.ListNode类
�� ListNode是一个泛型类,用于表示链表中的一个节点。Value和Next属性是ListNode最基本的属性,用于表示节点的值和指向下一个节点的引用。但是,完全可以根据实际需求添加其他属性,例如一个指向前一个节点的引用的Previous的属性,它是一个可空的ListNode类型,表示前一个节点的引用。这个属性可以用于实现双向链表,其中每个节点都有一个指向前一个节点和下一个节点的引用。
()总之,ListNode类的属性数量和类型取决于工程需求。在实际开发中,可以根据实际需求自定义ListNode类。
ListNode中,Value属性存储节点的值,而Next属性是指向链表中下一个节点的引用。Next属性的类型为ListNode,这意味着它指向相同类型的节点。这种设计使得使用相同类型的节点来构建一个链表,而不需要为每个节点创建一个特定的类型。
()public class ListNode(T value){ public T Value { get; set; } = value; public ListNode? Next { get; set; } = null; public ListNode? Previous { get; set; } = null;} 2.LinkedList类
LinkedList类是一个泛型类,用于实现链表数据结构。链表是一种线性数据结构,其中每个元素(节点)包含一个值和指向下一个元素(节点)的引用。LinkedList类在C#中通常用于存储相同类型的元素集合。LinkedList类的主要特点包括:
- 泛型类型参数T:允许存储任何类型的数据,只要它们实现了System.IEquatable接口。
- 节点类:LinkedList使用内部类ListNode表示链表中的节点。ListNode包含一个值(Value属性)和对下一个节点的引用(Next属性)。
- 头部和尾部节点:LinkedList维护两个节点引用:_head表示链表的头部节点,_tail表示链表的尾部节点。当向链表中添加或删除节点时,这些引用会相应地更新。
- 插入和删除节点:LinkedList提供了一些方法来插入和删除节点,如AddFirst、AddLast、Insert、Remove等。这些方法会更新头部和尾部节点的引用,以保持链表的正确性。
- 遍历链表:LinkedList提供了一些方法来遍历链表中的节点,如GetEnumerator。这使得我们可以使用foreach循环来访问链表中的所有节点。
C#标准库中已经提供了System.Collections.Generic.LinkedList类。在实际开发中,可以直接使用这个类,而无需自己实现。在使用LinkedList时,只需要设计实现自己的工程需要的方法,这些方法是自定义的。
public class LinkedList{ private static ListNode? _head; private static ListNode? _current; public static ListNode? Current { get => _current; set => _current = value; } public static ListNode? Head { get => _head; set => _head = value; } public LinkedList() => _head = null; /// /// 泛型类在链表尾部插入新数据 /// 追加新数据 /// public void Append(T value) { var newNode = new ListNode(value); if (_head == null) { _head = newNode; } else { var current = _head; while (current.Next != null) { current = current.Next; } current.Next = newNode; _current = newNode; } } /// /// 在当前位置插入数据, /// 不对数据排序,也不比较数据 /// public static void Insert(T value) { // 创建一个新的节点 var newNode = new ListNode(value); // 如果链表为空,将新节点设置为头节点 if (_head == null) { _head = newNode; _current = newNode; return; } // 找到当前节点 var current = _current; if (current == null) { _current = _head; while (_current.Next != null) { _current = _current.Next; } current = _current; } // 在当前位置插入新节点 newNode.Next = current.Next; newNode.Previous = current; current.Next = newNode; _current = newNode; } /// /// 输出链表数据 /// public static void PrintList() { var current = _head; while (current != null) { Console.Write(current.Value + " "); current = current.Next; } Console.WriteLine(); } /// /// 当前节点指针移动到链表头 /// 当前节点=头节点 /// public static void MoveFirst() { if (_head != null) { _current = _head; } } /// /// 当前节点指针移动到下一个节点 /// 当前节点=下一个节点 /// public static void MoveNext() { if (_current != null && _current.Next != null) { _current = _current.Next; } }}在LinkedList类中,_head属性是一个ListNode类型的变量,用于存储链表的头部节点。当向链表中插入新节点时,我们会创建一个新的ListNode实例,并将其设置为_head或将其附加到现有链表的末尾。链表的遍历过程也会使用ListNode类,逐个访问链表中的节点。
二、Main方法的实例
class Program { public static void Main(string[] args) { ArgumentNullException.ThrowIfNull(args); var list = new LinkedList(); list.Append(5); list.Append(2); list.Append(8); list.Append(1); LinkedList.PrintList(); // 输出:1 8 2 5 list.Append(11); LinkedList.PrintList(); // 输出:1 8 2 5 11 LinkedList.MoveFirst(); LinkedList.Insert(12); LinkedList.PrintList(); LinkedList.MoveNext(); LinkedList.Insert(13); LinkedList.PrintList(); } } //运行结果:/*5 2 8 15 2 8 1 115 12 2 8 1 115 12 2 13 8 1 11 */
上一篇:远程服务器卡怎么办?