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