Generic (III)-generic interface

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace generic interface
{
Public class genericlist <t>: system. Collections. Generic. ienumerable <t>
{
// Field
Protected node head;
Protected node current = NULL;

// Nested class
Protected class Node
{
// Field
Public node next;
Private t data;

// Constructor
Public node (T)
{
Next = NULL;
Data = T;
}

// Attributes
Public node next
{
Get
{
Return next;
}
Set
{
Next = value;
}
}
Public t data
{
Get
{
Return data;
}
Set
{
Data = value;
}
}
}

// Constructor
Public genericlist ()
{
Head = NULL;
}

// method 1
Public void addhead (t)
{< br> node n = new genericlist . node (t);
N. next = head;
head = N;
}

// method 2
public system. collections. generic. ienumerator getenumerator ()
{< br> node current = head;
while (current! = NULL)
{< br> yield Return Current. Data;
current = current. Next;
}< BR >}

// method 3
// ienumerable inherits from ienumerable, therefore this class
// must implement both the generic and non-generic versions of
// getenumerator. in most cases, the non-generic method can
// simply call the generic method.
system. collections. ienumerator system. collections. ienumerable. getenumerator ()
{< br> return getenumerator ();
}< BR >}

public class sortedlist : genericlist where T: system. icomparable
{< br> Public void bubblesort ()
{< br> If (null = head | null = head. next)
{< br> return;
}< br> bool swapped;

Do
{
Node previous = NULL;
Node current = head;
Swapped = false;

While (current. Next! = NULL)
{
If (current. Data. compareto (current. Next. Data)> 0)
{
Node TEM = current. Next;
Current. Next = current. Next. Next;
TEM. Next = current;

If (previous = NULL)
{
Head = TEM;
}
Else
{
Previous. Next = TEM;
}
Previous = TEM;
Swapped = true;
}
Else
{
Previous = current;
Current = current. Next;
}
}
} While (swapped );
}
}

// A simple class that implements icomparable <t> using itself as
// Type argument. This is a common design pattern in objects that
// Are stored in generic lists.
Public class person: system. icomparable <person>
{
String name;
Int age;

Public Person (string S, int I)
{
Name = s;
Age = I;
}

Public int compareto (person P)
{
Return age-P. Age;
}

Public override string tostring ()
{
Return name + ":" + age;
}

Public bool equals (person P)
{
Return (this. Age = P. Age );
}
}

Class Program
{
Static void main (string [] ARGs)
{
Sortedlist <person> List = new sortedlist <person> ();

String [] names = new string []
{
"Franscoise ",
"Bill ",
"Li ",
"Sandra ",
"Gunnar ",
"Alok ",
"Hiroyuki ",
"Maria ",
"Alessandro ",
"Raul"
};

Int [] ages = new int [] {45, 19, 28, 23, 18, 9,108, 72, 30, 35 };

For (INT x = 0; x <10; X ++)
{
List. addhead (new person (Names [X], ages [x]);
}

foreach (person P in list)
{< br> system. console. writeline (P. tostring ();
}< br> system. console. writeline ("Done With unsorted List");

list. bubblesort ();
foreach (person P in list)
{< br> system. console. writeline (P. tostring ();
}< br> system. console. writeline ("done with sorted List");
console. readkey ();
}< BR >}

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.