void push(int data) { NodeL new_node = new NodeL(data); new_node.next = head; head = new_node; }
void PrintList(NodeL headRef) { while (headRef != null) { Console.WriteLine(headRef.val + ""); headRef = headRef.next; } }
private NodeL getMiddle(NodeL h) { if (h == null) { return(h); } NodeL fastptr = h.next; NodeL slowptr = h; while (fastptr != null) { fastptr = fastptr.next; if (fastptr != null) { slowptr = slowptr.next; fastptr = fastptr.next; } } return(slowptr); }
public NodeL PerformMergeSort(NodeL h) { if (h == null || h.next == null) { return(h); } //get middle of the list NodeL middle = getMiddle(h); NodeL nextmiddle = middle.next; middle.next = null; //mergesort on the left side NodeL left = PerformMergeSort(h); NodeL right = PerformMergeSort(nextmiddle); NodeL sortedList = sortedMerge(left, right); return(sortedList); }
NodeL sortedMerge(NodeL a, NodeL b) { NodeL result = null; if (a == null) { return(b); } if (b == null) { return(a); } if (a.val < b.val) { result = a; result.next = sortedMerge(a.next, b); } else { result = b; result.next = sortedMerge(a, b.next); } return(result); }
public void Insert() { Nodehs tg = new Nodehs(); tg.hs = new HocSinh(); tg.link = null; //??Kiem tra ma lop da co chua? Neu chua=> bao loi; neu co: 1 ghi them SV vao tep hocsinh.txt; cap nhat siso tep Lop.txt? //TT HS hop le thi them vao DS HS va ghi vao tep Nodehs tghs = new Nodehs(); danhsachlop dsl = new danhsachlop(); dsl.doctep(); NodeL tgl = new NodeL(); int mahs; string malop; bool ok1; bool ok2;//SV có mã lớp chưa có do { ok1 = true; Console.Write("Nhap Ma hoc sinh: "); mahs = int.Parse(Console.ReadLine()); tghs = this.ds; while (tghs != null)//XL { if (tghs.hs.mahs == mahs) { ok1 = false; } tghs = tghs.link; } if (!ok1) { Console.WriteLine("DL ko hop le: Ma HS da co! Moi nhap lai"); } } while (!ok1); tg.hs.mahs = mahs; tg.hs.nhap1(); do { ok2 = false; Console.Write("Nhap ma lop: "); malop = Console.ReadLine(); tgl = dsl.ds; while (tgl != null) { if (string.Compare(tgl.L.malop, malop) == 0) { ok2 = true; break; } tgl = tgl.link; } if (!ok2) { Console.WriteLine("DL ko hop le: Ma lop chua co! Moi nhap lai"); } } while (!ok2);//XL tg.hs.malop = String.Copy(malop); ghitep(tg); //Cap nhat si so tep Lop.txt tg.link = ds; ds = tg; }