wordpress软件下载站主题,做一个网站怎么做数据库,wordpress $pagenow,重启 iis 中的网站目录
1.先设计一个泛型节点类Node
2.再设计一个泛型链表类LinkedList
3.创建一个LinkedList类的实例
4.完整的实例 再发一篇使用泛型的方法设计单向链表。 以下是一个使用泛型节点类和LinkedListT类的示例#xff0c;其中包含Insert方法用于插入新节点#xff0c…目录
1.先设计一个泛型节点类Node
2.再设计一个泛型链表类LinkedList
3.创建一个LinkedList类的实例
4.完整的实例 再发一篇使用泛型的方法设计单向链表。 以下是一个使用泛型节点类和LinkedListT类的示例其中包含Insert方法用于插入新节点并在插入后更新当前节点。同时GetCurrentValue方法用于获取当前节点的值并将其转换为int类型。
1.先设计一个泛型节点类NodeT /// summary/// 定义泛型节点类/// /summarypublic class NodeT(T data){public T Data { get; set; } data;public NodeT? Next { get; set; } null;}
2.再设计一个泛型链表类LinkedListT 定义一个包含Insert和GetCurrentValue方法的LinkedListT类 /// summary/// 定义泛型链表类LinkedListT/// /summarypublic class LinkedListT where T : struct{private NodeT? head;private NodeT? current;public void Insert(T value){var newNode new NodeT(value);if (head null){head newNode;current newNode;}else{NodeT temp head;while (temp.Next ! null){temp temp.Next;}temp.Next newNode;current newNode;}}// 定义GetCurrentValue()方法获取当前节点public int GetCurrentValue(){if (head null){throw new InvalidOperationException(The linked list is empty.);}return LinkedListT.ConvertToInt(current.Data);}// 把T转换为int类型private static int ConvertToInt(T value){return checked((int)(object)value);}} 使用类似的方法在LinkedListT类中添加其他方法。
3.创建一个LinkedListint类的实例 创建一个LinkedListint类的实例插入一些节点并显示当前节点的值
var linkedList new LinkedListint();linkedList.Insert(5);
linkedList.Insert(10);
linkedList.Insert(15);Console.WriteLine(linkedList.GetCurrentValue()); // 输出15 这个示例假设类型T可以转换为int。在实际应用中请确保T的类型符合您的需求。
4.完整的实例
namespace _131_3
{internal class Program{private static void Main(string[] args){ArgumentNullException.ThrowIfNull(args);var linkedList new LinkedListint();linkedList.Insert(5);Console.WriteLine(linkedList.GetCurrentValue()); // 输出5linkedList.Insert(10);Console.WriteLine(linkedList.GetCurrentValue()); // 输出10linkedList.Insert(15);Console.WriteLine(linkedList.GetCurrentValue()); // 输出15}/// summary/// 定义泛型节点类/// /summarypublic class NodeT(T data){private T data data;public T Data { get data; set data value; }public NodeT? Next { get; set; } null;}/// summary/// 定义泛型链表类LinkedListT/// /summarypublic class LinkedListT where T : struct{private NodeT? head;private NodeT? current;public void Insert(T value){var newNode new NodeT(value);if (head null){head newNode;current newNode;}else{NodeT temp head;while (temp.Next ! null){temp temp.Next;}temp.Next newNode;current newNode;}}// 定义GetCurrentValue()方法获取当前节点public int GetCurrentValue(){if (head null){throw new InvalidOperationException(The linked list is empty.);}return LinkedListT.ConvertToInt(current!.Data);}// 把T转换为int类型private static int ConvertToInt(T value){return checked((int)(object)value);}}}
}
//运行结果
/*
5
10
15*/