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 classStudent { Public stringName {Get;Set; } Public intAge {Get;Set; } PublicStudent (stringNameintAge ) { This. Name =name; This. Age =Age ; } Public Override stringToString () {return "\ r \ n"+ This. Name +": Age"+ This. Age +"years"; } }
Linked list Node class:
/// <summary> ///Node Class/// </summary> /// <typeparam name= "T" >generic type</typeparam> Public classNode<t> { PublicT Data {Get;Set; } PublicNode<t> Next {Get;Set; } PublicNode (T data) { This. Data =data; This. Next =NULL; } /// <summary> ///Additional Nodes/// </summary> /// <param name= "NewNode" >The new node</param> Public voidAppend (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 stringToString () {stringOutput = This. Data.tostring (); if( This. Next! =NULL) {Output+=" "+ This. Next.tostring (); } returnoutput; } }
Linked List classes:
/// <summary> ///Linked List class/// </summary> /// <typeparam name= "T" >generic type</typeparam> Public classLinkedlist<t>{Node<T> Headnode =NULL;//head Node /// <summary> ///Append Node Method/// </summary> /// <param name= "Data" ></param> Public voidAdd (T data) {if(Headnode = =NULL) {Headnode=NewNode<t>(data); } Else{headnode.append (NewNode<t>(data)); } } /// <summary> ///Indexer, getting nodes by index/// </summary> /// <param name= "index" ></param> /// <returns></returns> PublicT This[intIndex] { Get { inttemp =0; Node<T> node =Headnode; while(Node! =NULL&& Temp <=index) { if(temp = =index) { returnnode. Data; } Else{node=node. Next; } temp++; } return default(T); } } Public Override stringToString () {if(Headnode! =NULL) { return This. headnode.tostring (); } return string. Empty; } }
Main function:
classProgram {Static voidMain (string[] args) {LinkedList<int> intlist =Newlinkedlist<int>(); Enumerable.range (0,5). tolist<int> (). ForEach (x =Intlist.add (x)); Console.WriteLine ("the contents of the integral type are: {0}\r\n", intlist); LinkedList<Student> students =NewLinkedlist<student>(); Students. ADD (NewStudent ("Zhang San", -)); Students. ADD (NewStudent ("John Doe", A)); Students. ADD (NewStudent ("Harry", +)); Console.WriteLine (students[1]. Name +"The age 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