C # A Simple simulation implementation of generic collection classes

Source: Internet
Author: User

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 );
}
}
}

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.