public void Sort() { LinkGen <T> temp = list; LinkListGen <T> newList = new LinkListGen <T>(); newList.AppendItem(temp.Data); LinkGen <T> temp2 = newList.list; temp = temp.Next; while (temp != null) { if (temp.Data.CompareTo(temp2.Data) == 1) //if temp > temp2 { newList.AppendItem(temp.Data); temp2 = newList.list; } if (temp.Data.CompareTo(temp2.Data) < 1) //if temp <= temp2 { newList.AddItem(temp.Data); temp2 = newList.list; } temp = temp.Next; } list = newList.list; }
static void Main(string[] args) { LinkListGen <int> list = new LinkListGen <int>(); LinkListGen <int> list2 = new LinkListGen <int>(); list.AddItem(40); list.AddItem(60); list.AddItem(80); list.AddItem(90); list.AddItem(70); list.AddItem(20); list.AddItem(15); list.Sort(); System.Console.WriteLine("Copy " + list.DisplayItems()); list.Clear(); list2.AddItem(40); list2.AddItem(60); list2.AddItem(80); list2.AddItem(100); list.Copy(list2); System.Console.WriteLine("Copy " + list.DisplayItems()); list.Clear(); list2.Clear(); list.AddItem(20); list.AddItem(40); list.AddItem(60); list.AddItem(80); list.AddItem(100); list2.AddItem(10); list2.AddItem(30); list2.AddItem(50); list2.AddItem(70); list2.AddItem(90); list.Concat(list2); System.Console.WriteLine("Concat Two Lists: " + list.DisplayItems()); list.Clear(); list2.Clear(); list.AddItem(40); list.AddItem(11); list.AddItem(77); list.AppendItem(54); list.AppendItem(23); System.Console.WriteLine("Append Item " + list.DisplayItems()); //Remove Item list.RemoveItem(40); list.RemoveItem(11); list.RemoveItem(77); list.RemoveItem(54); list.RemoveItem(23); list.RemoveItem(30); System.Console.WriteLine("Remove Item " + list.DisplayItems()); //IsPresent System.Console.WriteLine("Current Number of Items in List " + list.NumberOfItems()); //Display Items In Order list.InsertInOrder(80); list.InsertInOrder(10); list.InsertInOrder(15); list.InsertInOrder(25); list.InsertInOrder(38); list.InsertInOrder(78); list.InsertInOrder(100); list.InsertInOrder(81); System.Console.WriteLine("Insert in order " + list.DisplayItems()); System.Console.ReadLine(); Console.ReadKey(); }
static void Main(string[] args) { LinkListGen <int> test1 = new LinkListGen <int>(); Console.WriteLine("Testing AddItem: 1, 2, 3, 4"); test1.AddItem(1); test1.AddItem(2); test1.AddItem(3); test1.AddItem(4); Console.WriteLine("\nTesting DisplayList:"); test1.DisplayList(); Console.WriteLine("\nTesting NumberOfItems: " + test1.NumberOfItems()); Console.WriteLine("\nTesting IsPresentItem:"); Console.WriteLine("3: " + test1.IsPresentItem(3)); Console.WriteLine("5: " + test1.IsPresentItem(5)); Console.WriteLine("\nTesting RemoveItem: 4, 2"); test1.RemoveItem(4); test1.RemoveItem(2); test1.DisplayList(); Console.WriteLine("\nTesting AppendItem: 2"); test1.AppendItem(2); test1.DisplayList(); Console.WriteLine("\nCreate test2 and AppendItem: 4, 5, 6"); LinkListGen <int> test2 = new LinkListGen <int>(); test2.AppendItem(4); test2.AppendItem(5); test2.AppendItem(6); test2.DisplayList(); Console.WriteLine("\nConcat test1 and test2:"); test1.Concat(test2); test1.DisplayList(); Console.WriteLine("\nCopy test2 to test1:"); Console.WriteLine("Current test1:"); test1.DisplayList(); Console.WriteLine("Current test2:"); test2.DisplayList(); test1.Copy(test2); Console.WriteLine("Updated test1:"); test1.DisplayList(); Console.WriteLine("test2 should remain unchanged:"); test2.DisplayList(); LinkListGen <int> test3 = new LinkListGen <int>(); test3.AppendItem(1); test3.AppendItem(2); test3.AppendItem(4); test3.AppendItem(5); Console.WriteLine("\nNew list to test InsertInOrder:"); test3.DisplayList(); Console.WriteLine("InsertInOrder: 3"); test3.InsertInOrder(3); test3.DisplayList(); LinkListGen <int> test4 = new LinkListGen <int>(); test4.AppendItem(5); test4.AppendItem(7); test4.AppendItem(4); test4.AppendItem(9); test4.AppendItem(2); Console.WriteLine("\ntest4 unsorted:"); test4.DisplayList(); Console.WriteLine("test4 sorted:"); test4.Sort(); test4.DisplayList(); Console.ReadKey(); }