With regard to linked list operations, in C # Microsoft has provided a linkedlist<t> data structure that can be implemented through a series of methods provided by this class.
Here I provide a piece of code, this is in the forum when someone asked the code, it implemented a custom list of operations (the reader can be further improved on this basis). Because this code involves some C # techniques, it is a bit of a reference for beginners to learn C #.
Entity class:
/// <summary> /// Student Class // / </summary> public class student { public string Name { get; set; } public int Age { get; set; } public student (string name, int age) { This. Name = name; this. age = age; } public override string tostring () { return "\ r \ n" + this. name + ": Age" + this. age + "old"; } }
list node class:
/// <summary> /// Node Class // / </summary> /// <typeparam name= "T" > Generics </typeparam> public class Node<T> { public T Data { get; set; } public Node<T> Next { get; set; } public node (T data) { this. Data = data; this. next = null; } /// <summary> /// Additional Nodes /// </summary> /// <param name= "NewNode" > New Nodes </param> public void append (Node<t> newnode) { / /If the next node is null, point the new node to the next node if (this . Next == null) { this. next = newnode; } //if the next node is not NULL, attach directly to the next node else { this. Next.append (NewNode); } } public override string tostring () { string output = this. Data.tostring (); if (this. Next != null) { output += " " + this. Next.tostring (); } return output; } }
list class:
/// <summary> /// Chain List class // / </summary> /// <typeparam name= "T" > Generics </typeparam> public class LinkedList<T> { node<t> headnode = null;//head Node /// <summary> /// Append Node method /// </summary> /// <param name= "Data" ></param> public void add (T data) { if (headnode == null) { headNode = new Node<T> (data); } else { headnode.append (new Node<T> (data)); } } /// <summary> /// Indexer, retrieving nodes by index /// </ Summary> /// <param name= "Index" ></param > /// <returns></returns> public T this[int index] { get { int temp = 0; Node<T> node = headNode; while (node != null && temp <= index) { if (Temp == index) { return Node. data; } else { node = node. next; } temp++; } return default ( T); } } public override string ToString () { if (headnode != null) { return this.headnode.tostring (); } &Nbsp; return string. empty; } }
main function:
class Program { static void main (String[] args) { linkedlist<int> intlist = new LinkedList<int> (); enumerable.range (0, 5). Tolist<int> (). ForEach (X => intlist.add (x)); console.writeline ("Int's content is: {0}\r\n", intlist); LinkedList<Student> students = new LinkedList<Student> (); students. ADD (New student ("Zhang San", 20)); StuDents. ADD (New student ("John Doe", 22)); Students. ADD (New student ("Harry", 21)); Console.WriteLine (Students[1]. The age of name + "is:" + students[1]. age); console.readline (); } }
Finally, an appendix to Microsoft's official list category. Https://msdn.microsoft.com/zh-cn/library/he2s3bh7 (v=vs.110). aspx.
C # linked list operations