public static void Merge(GenericList <int> d1, GenericList <int> d2, GenericList <int> d3) { int p, q; while (!d1.ListEmpty() && !d2.ListEmpty()) { p = d1.Top(); q = d2.Top(); if (p < q) { p = d1.Pop(); d3.AddLast(p); } else { q = d2.Pop(); d3.AddLast(q); } } while (!d1.ListEmpty()) { p = d1.Pop(); d3.AddLast(p); } while (!d2.ListEmpty()) { q = d2.Pop(); d3.AddLast(q); } }
public static void FileTotwolist(GenericList <int> ds1, GenericList <int> ds2) { using (StreamReader sr = new StreamReader("Dayso1.txt")) { var n = int.Parse(sr.ReadLine()); for (int i = 0; i < n; i++) { var x = int.Parse(sr.ReadLine()); ds1.AddLast(x); } } using (StreamReader sr1 = new StreamReader("Dayso2.txt")) { var n = int.Parse(sr1.ReadLine()); for (int i = 0; i < n; i++) { var x = int.Parse(sr1.ReadLine()); ds2.AddLast(x); } } }
public static void TestLL() { GenericList <int> ds; ds = new GenericList <int>(); var ds1 = new GenericList <int>(); var ds2 = new GenericList <int>(); var ds3 = new GenericList <int>(); var chon = 0; do { chon = MenuDSLK(); switch (chon) { case 1: { FileToList(ds); break; } case 2: { Input(ds); break; } case 3: { ds.PrintAllNodes(); break; } case 4: { Console.Write("PHAN TU CAN NHAP: "); var n = int.Parse(Console.ReadLine()); ds.Addhead(n); ds.PrintAllNodes(); break; } case 5: { Console.Write("PHAN TU CAN NHAP: "); var n = int.Parse(Console.ReadLine()); ds.AddLast(n); ds.PrintAllNodes(); break; } case 6: { ds.PrintAllNodes(); Console.Write("PHAN TU CAN TIM: "); var n = int.Parse(Console.ReadLine()); Console.WriteLine("=> Phan tu {0} nam o vi tri {1} . ", n, ds.SearchNode1(n)); break; } case 7: { ds.PrintAllNodes(); Console.Write("PHAN TU CAN XOA: "); var n = int.Parse(Console.ReadLine()); ds.deleteallofX(n); //ds.Delete(n); ds.PrintAllNodes(); break; } case 8: { Console.WriteLine("Phan tu lon nhat cua link list: " + ds.maxnode(new IntComparer())); Console.WriteLine("Phan tu nho nhat cua link list: " + ds.minnode(new IntComparer())); Console.Write("Phan tu chan cua link list: "); Evennode(ds); break; } case 9: { FileTotwolist(ds1, ds2); Console.WriteLine("+ Kiem tra tinh tang dan cua hai danh sach :"); if (ds1.isSortedAsc(ds1.Head, new IntComparer()) == true && ds2.isSortedAsc(ds2.Head, new IntComparer()) == true) { Console.WriteLine("Ca 2 danh sach nhap tu FILE dau tang dan"); ds1.PrintAllNodes(); ds2.PrintAllNodes(); } else { Console.WriteLine("Out ra sua lai file"); } //ds3.MergeSortedList(ds1.Head, ds2.Head); //Console.Write("=> KET QUA danh sach 3: "); //ds3.PrintAllNodes(); Console.Write("=> KET QUA danh sach 3: "); Merge(ds1, ds2, ds3); ds3.PrintAllNodes(); Console.WriteLine("\nPress any key to terminate..."); break; } case 10: { break; } case 0: { break; } default: { Console.WriteLine("Hello"); break; } } Console.ReadKey(); Console.Clear(); } while (chon != 0); }