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