C # linked list operation,
For Linked List operations, in C #, Microsoft has provided an external list <T> data structure. Using this class, a series of methods can be provided to implement linked list operations.
Here I provide a piece of code, which is the Code provided when someone asks a question in the Forum. It implements the operation of a custom linked list (readers can further improve it on this basis ). This Code involves some C # skills, so it will be posted as 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 + "years old ";}}Linked List nodes:
/// <Summary> /// Node class /// </summary> /// <typeparam name = "T"> generic </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 node /// </summary> /// <param name = "newNode"> new node </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 = n EwNode;} // if the next node is not null, it is directly appended 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 ;}}Linked List:
/// <Summary> /// linked list class /// </summary> /// <typeparam name = "T"> generic </typeparam> public class category list <T> {Node <T> headNode = null; // header node // <summary> /// method of appending a node /// </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, obtain a node by indexing /// </summary> /// <param name = "in Dex "> </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 () ;}return string. Empty ;}}Main function:
Class Program {static void Main (string [] args) {Program list <int> intList = new Program list <int> (); Enumerable. range (0, 5 ). toList <int> (). forEach (x => intList. add (x); Console. writeLine ("integer content: {0} \ r \ n", intList); shortlist <Student> students = new shortlist <Student> (); students. add (new Student ("James", 20); students. add (new Student ("Li Si", 22); students. add (new Student ("Wang Wu", 21); Console. writeLine (students [1]. the age of Name + "is:" + students [1]. age); Console. readLine ();}}At last, I will refer to the official linked list category of Microsoft in the appendix. Https://msdn.microsoft.com/zh-cn/library/he2s3bh7 (v = vs.110). aspx.