C # linked list operation,

Source: Internet
Author: User

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.

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.