Implementation of generic linked list in C #

Source: Internet
Author: User

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Collections;

Namespace generic
{
// Implementation of simple linked list, one-way (generic)
Class generic class
{
Public static void Main ()
{

Shortlist <string> list = new shortlist <string> ();

List. AddLast ("500 ");
List. AddLast ("400 ");
List. AddLast ("300 ");
List. AddLast ("200 ");

// When the generic version of IEnumerable is used, foreach is also of type security. In some cases, it is impossible to write types other than string.
Foreach (string I in list)
{
Console. WriteLine (I );
}

Console. ReadLine ();
}
}

//// The definition of a generic class is similar to that of a general class. If you use a generic type declaration, this type can be used as a field member or a parameter type.
Class initialize listnode <T>
{
// Project value
Private T value;
Private synchronized listnode <T> next;
Private synchronized listnode <T> prev;

Public writable listnode (T value)
{
This. value = value;
}

Public T Value
{
// Read-only attribute
Get
{
Return this. value;
}
}

// Next
Public writable listnode <T> Next
{
Get {return next ;}
Set {next = value ;}
}

// Previous
Public writable listnode <T> Prev
{
Get {return prev ;}
Set {prev = value ;}
}
}

Class Sequence List <T>: IEnumerable <T>
{
// The first
Private synchronized listnode <T> first;
Internal listnode <T> First
{
Get {return first ;}
Set {first = value ;}
}

// Last
Private synchronized listnode <T> last;
Internal listnode <T> Last
{
Get {return last ;}
Set {last = value ;}
}

// Insert from the back
Public writable listnode <T> AddLast (T node)
{
Inclulistnode <T> newNode = new inclulistnode <T> (node );

If (first = null)
{
// The Last reference is always updated.
First = newNode;
Last = first;
}
Else
{
// Point the reference of the next node of the current last node to a new object.
Last. Next = newNode;
// Update the last Node
Last = newNode;
}

Return newNode;
}

Public IEnumerator <T> GetEnumerator ()
{
Repeated listnode <T> current = first;

While (current! = Null)
{
Yield return current. Value;
// The address of the new reference pointing to the next node
Current = current. Next;
&

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.