/// <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)