C # linked list operations

Source: Internet
Author: User

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>///Students///    </summary> public    class Student    {public        string Name {get; Set } public        int age {get; set;}        Public Student (string name, int.)        {this            . name = name;            This. Age = Age;        }        public override string ToString ()        {            return ' \ r \ n ' + this. Name + ": Age" + this. Age + "Year";        }    }
Linked 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 </p aram> public void Append (node<t> newNode) {//If the next node is null, point the new node to the next node if (th Is. Next = = null) {this.            Next = NewNode; }//If the next node is not NULL, it is attached 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; }    }
Linked List classes:
    <summary>///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, get node 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 (); } 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 ("The contents of the integer type are: {0}\r\n", intlist);            linkedlist<student> students = new linkedlist<student> ();            Students. ADD (New Student ("Zhang San"));            Students. ADD (New Student ("John Doe",));            Students. ADD (New Student ("Harry", +));            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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.