C # linked list

Source: Internet
Author: User

/// <Summary>
/// The linked list is a chain storage structure corresponding to a linear table (A1, A2, A3, A4, A5, A6 ).
// H --> | A1 | --> | A2 | --> | Ai-1 | --> | Ai | --> | An |
/// H is a virtual node, which is equivalent to the Root node)
/// An = {data, next };
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
Public class Node <T>
{
/// <Summary>
/// Data
/// </Summary>
Public T Data {set; get ;}

/// <Summary>
/// Reference of the rear drive Node
/// </Summary>
Public Node <T> Next {set; get ;}

Public Node ()
{
Data = default (T );
Next = null;
}

Public Node (T data)
{
This. Data = data;
}

Public Node (T data, Node <T> node): this (data)
{
This. Next = node;
}
}

Public class MyList <T>
{
/// <Summary>
/// Header node, also known as a virtual Root Node
/// </Summary>
Public Node <T> Head {set; get ;}

Public MyList ()
{
Head = new Node <T> ();
}

/// <Summary>
/// The length attribute of the linked list
/// </Summary>
Public int Length
{
Get
{
Node <T> point = Head;
Int len = 0;
While (point. Next! = Null)
{
Len ++;
Point = point. Next;
}
Return len;
}
}

/// <Summary>
/// Clear a single-chain table
/// </Summary>
Public void Clear ()
{
Head. Next = null;
}

Public bool IsEmpty
{
Get
{
Return this. Length = 0? True: false;
}
}

Public void Add (T item)
{
Node <T> node = new Node <T> (item );

// None of the nodes
If (Head. Next = null)
{
Head. Next = node;
}
Else
{
Node <T> point = Head. Next;
While (point. Next! = Null)
{
Point = point. Next;
}
Point. Next = node;
}
}

/// <Summary>
/// Search for elements based on the index (starting from 0)
/// </Summary>
/// <Param name = "index"> </param>
/// <Returns> </returns>
Public T GetItem (int index)
{
If (index <0 | index> = this. Length)
{
Throw new Exception ("the index is error .");
}
If (! This. IsEmpty)
{
Node <T> point = Head. Next;
Int j = 0;
While (point! = Null & j <index)
{
If (j ++ = index)
{
Break;
}
Point = point. Next;
}
Return point. Data;
}
Else
{
Throw new Exception ("the list is empty .");
}
}

/// <Summary>
/// Search for subscript Based on value (starting from 0)
/// </Summary>
/// <Param name = "value"> </param>
/// <Returns> </returns>
Public int Locate (T value)
 

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.