Ylbtech-unitity-cs:generics |
Unsorted list:raul:35alessandro:30maria:72hiroyuki:108alok:9gunnar:18sandra:23li:28bill:19franscoise:45sorted List:alok:9gunnar:18bill:19sandra:23li:28alessandro:30raul:35franscoise:45maria:72hiroyuki:108done Please press any key to continue ...
View Code
1.B, source code back to top |
1.b.1,
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Text;namespacegenerics_csharp{//the type parameter T in angle brackets. Public classMylist<t>: ienumerable<t> { protectedNode Head; protectedNode current =NULL; //nested types are also generics on T protected classNode { PublicNode Next; //T as the private member data type. PrivateT data; //The T used in a non-generic constructor. PublicNode (t) {next=NULL; Data=T; } PublicNode Next {Get{returnNext;} Set{next =value;} } //T as the return type of the property. PublicT Data {Get{returndata;} Set{data =value;} } } PublicMyList () {head=NULL; } //T as a method parameter type. Public voidAddHead (T t) {Node n=NewNode (t); N.next=Head; Head=N; } //implement GetEnumerator to return IENUMERATOR<T>, enabling the list's//foreach iteration. Please note that in C # 2.0,//you do not need to implement current and MoveNext. //The compiler will create a class that implements Ienumerator<t>. PublicIenumerator<t>GetEnumerator () {Node current=Head; while(Current! =NULL) { yield returnCurrent . Data; Current=Current . Next; } } //This method must be implemented, because//ienumerable<t> Inheritance IEnumerableIEnumerator Ienumerable.getenumerator () {returnGetEnumerator (); } } Public classSortedlist<t>: mylist<t>whereT:icomparable<t> { //a simple sorting algorithm that is not optimized,//the algorithm sorts the list elements from low to High: Public voidBubblesort () {if(NULL= = Head | |NULL==head. Next)return; BOOLswapped; Do{Node Previous=NULL; Node Current=Head; Swapped=false; while(Current.next! =NULL) { //because this method needs to be called, SortedList//class is bound on ienumerable<t> if(Current.Data.CompareTo (Current.next.Data) >0) {Node tmp=Current.next; Current.next=Current.next.next; Tmp.next=Current ; if(Previous = =NULL) {Head=tmp; } Else{Previous.next=tmp; } Previous=tmp; Swapped=true; } Else{Previous=Current ; Current=Current.next; } }//End While} while(swapped); } } //a simple class that implements icomparable<t> itself as a type parameter,//is in the object.//Common Design patterns, these objects//stored in the generic list. Public classPerson:icomparable<person> { stringname; intAge ; PublicPerson (stringSinti) {name=s; Age=i; } //This causes the list element//sort by age value. Public intCompareTo (person p) {returnAge-P.age; } Public Override stringToString () {returnName +":"+Age ; } //Equals must be implemented. Public BOOLEquals (person p) {return( This. Age = =p.age); } } classGenerics {Static voidMain (string[] args) { //declares and instantiates a new paradigm SortedList class. //Person is a type parameter. sortedlist<person> list =NewSortedlist<person>(); //Create a name and age value to initialize the person object. string[] names =New string[] {"franscoise","Bill","Li","Sandra","Gunnar","Alok","Hiroyuki","Maria","Alessandro","Raul" }; int[] ages =New int[] { $, +, -, at, -,9,108, the, -, * }; //populate the list. for(intx =0; x < names. Length; X + +) {list. AddHead (NewPerson (Names[x], ages[x])); } Console.WriteLine ("unsorted List:"); //prints a list that is not sorted. foreach(Person Pinchlist) {Console.WriteLine (p.tostring ()); } //sort the list. list. Bubblesort (); Console.WriteLine (String.Format ("{0}sorted List:", Environment.NewLine)); //print out the sorted list. foreach(Person Pinchlist) {Console.WriteLine (p.tostring ()); } Console.WriteLine (" Done"); } }}
1.b.2,
|
Ylbtech Source: http://ylbtech.cnblogs.com/ This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. |
Ylbtech-unitity-cs:generics