The generic collection classes in C # are very convenient and convenient to use. In this article, I will use a linked list to simulate the list <t> class behavior in C #. I will not talk much about it. Let's look at my Implementation Code. The Code has already written comments, therefore, the Code is not described as follows:
Using system. collections;
Class mylist <t>
{
Private mylistnode firstnode; // The first node.
Private int count; // node count
Public mylist ()
{
This. firstnode = NULL;
This. Count = 0;
}
// Obtain the list length.
Public int getlength ()
{
Return this. count;
}
// Add a node
Public void addelement (t data)
{
Mylistnode first = This. firstnode;
If (first = NULL)
{
This. firstnode = new mylistnode (data );
This. Count ++;
Return;
}
While (first. Next! = NULL)
{
First = first. Next;
}
First. Next = new mylistnode (data );
This. Count ++;
}
// Delete a node
Public bool remove (t data)
{
Mylistnode first = This. firstnode;
If (first. Data. Equals (data ))
{
This. firstnode = first. Next;
This. Count --;
Return true;
}
While (first. Next! = NULL)
{
If (first. Next. Data. Equals (data ))
{
First. Next = first. Next. Next;
This. Count --;
Return true;
}
}
Return false;
}
// Obtain the set element on the specified index
Public t getatindex (INT index)
{
Int innercount = 1;
Mylistnode first = This. firstnode;
If (index> count)
{
Throw new exception ("Index out of boundary ");
}
Else
{
While (innercount <index)
{
First = first. Next;
Innercount ++;
}
Return first. Data;
}
}
// Insert a new element into the specified index
Public void insertatindex (INT index, t data)
{
Int innercount = 1;
Mylistnode first = This. firstnode;
If (index> count)
{
Throw new exception ("Index out of boundary ");
}
If (Index = 1)
{
This. firstnode = new mylistnode (data );
This. firstnode. Next = first;
}
Else
{
While (innercount <index-1)
{
First = first. Next;
Innercount ++;
}
Mylistnode newnode = new mylistnode (data );
Newnode. Next = first. Next;
First. Next = newnode;
}
This. Count ++;
}
// Delete the set element on the specified index
Public void removeatindex (INT index)
{
Int innercount = 1;
Mylistnode first = This. firstnode;
If (index> count)
{
Throw new exception ("Index out of boundary ");
}
If (Index = 1)
{
This. firstnode = first. Next;
}
Else
{
While (innercount <index-1)
{
First = first. Next;
Innercount ++;
}
First. Next = first. Next. Next;
}
This. Count --;
}
// Delete all elements in the Set
Public void removeall ()
{
This. firstnode = NULL;
This. Count = 0;
}
// You can use foreach to traverse the collection class.
Public ienumerator getenumerator ()
{
Mylistnode first = This. firstnode;
While (first! = NULL)
{
Yield return first. Data;
First = first. Next;
}
}
// Internal node class
Private class mylistnode
{
Public t data {Get; set;} // element value on the node
Public mylistnode next {Get; set;} // next node of the node
Public mylistnode (T nodedata)
{
This. Data = nodedata;
This. Next = NULL;
}
}
}
The usage of this simulation class is as follows:
Class Program
{
Static void main (string [] ARGs)
{
Mylist <string> ML = new mylist <string> ();
ML. addelement ("Xu ");
ML. addelement ("Jin ");
ML. addelement ("Lin ");
ML. addelement ("love ");
ML. addelement ("Jasmine ");
ML. insertatindex (4, "fiercely ");
ML. removeatindex (2 );
ML. Remove ("Lin ");
Foreach (string s in ml)
{
Console. writeline (s );
}
}
}