Danh sách kết nối kép

8 824 1
Danh sách kết nối kép

Đang tải... (xem toàn văn)

Thông tin tài liệu

Danh sách kết nối kép

// Dia chi email cua 2 lop 'cntt_tk6_2@googlegroups.com'; 'tk61@googlegroups.com'// Day la bai tap ban Quy lop Tk62 hoi toi, toi da sua theo y cua ban //ay, cac em xem tham khao nhe.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace danh_saach_noi_kep{ class dnode { public string tenchinhanh; public string diachi; public int sodienthoai; public dnode pre; public dnode next; } class list { public dnode head, tail; } class Program { static dnode Ggetnode(string x, string y, int z) { dnode p = new dnode(); if (p == null) { throw new Exception("khong du bo nho"); } else { p.tenchinhanh = x; p.diachi = y; p.sodienthoai = z; p.pre = null; p.next = null; return p; } } static void chendau(ref list l, string x, string y, int z) { dnode p = Ggetnode(x, y, z); if (l.head == null) { l.head = l.tail = p; } else { p.next = l.head; l.head.pre = p; l.head = p; } } static void chencuoi(ref list l, string x, string y, int z) { dnode p = Ggetnode(x, y, z); { if (l.head == null) { l.head = l.tail = p; } else { l.tail.next = p; p.pre = l.tail; l.tail = p; } } } static dnode Tim(list l, string x, string y, int z) { dnode p = new dnode(); while ((p != null) && (p.tenchinhanh != x) && (p.diachi != y) && (p.sodienthoai != z)) { //p = l.head; ko tim duoc cau lenh nay, va luon la vong lap vo han neu danh sach khac rong hoac nut can tim ko phai la nut dau tien, vi em luon gan con tro p ve dau danh sach p = p.next; } return p; } static void themdau(ref list l, dnode p) { if (l.head == null) { l.head = l.tail = p; } else { p.next = l.head; l.head.pre = p; l.head = p; } } // Thu tuc chen sau phan tu co nghia la em chen nut co cac truong tt x, y, z vao sau nut q. // Em da hieu sai de bai static void chensauphantu(ref list l, dnode q, string x, string y, int z) { /* Console.WriteLine("ban muon them vao phan tu sau phan tu nay ."); Console.Write("Ten chi nhanh :"); string a = Console.ReadLine(); Console.Write("Ten chi nhanh :"); string b = Console.ReadLine(); Console.Write("so dien thoai :"); int c= int.Parse(Console.ReadLine()); q = Tim(l, a,b,c);*/ // doan nay bi sai dnode p = Ggetnode(x, y, z); dnode s = q.next; if (q != null) { p.next = s; p.pre = q; q.next = p; if (s != null) { s.pre = p; } if (q == l.tail) { l.tail = p; } } else { chendau(ref l, x, y, z); } } // Tuong tu nhu thu tuc o tren static void chentruocmotphantu(ref list l, dnode q, string x, string y, int z) { /*Console.WriteLine("ban muon them vao phan tu truoc phan tu nay ."); Console.Write("Ten chi nhanh :"); string a = Console.ReadLine(); Console.Write("Ten chi nhanh :"); string b = Console.ReadLine(); Console.Write("so dien thoai :"); int c = int.Parse(Console.ReadLine()); q = Tim(l, a, b, c);*/ // doan nay cung bi sai dnode s = q.pre; dnode p = Ggetnode(x, y, z); if (q != null) { p.next = q; p.pre = s; q.pre = p; if (s != null) { s.next = p; } if (q == l.head) { l.head = p; } } else { chendau(ref l, x, y, z); } } static void Nhap(ref list l) { dnode p = new dnode(); ConsoleKeyInfo kt; do { Console.Write("Ten chi nhanh :"); string x = Console.ReadLine(); Console.Write("dia chi chi nhanh :"); string y = Console.ReadLine(); Console.Write("so dien thoai :"); int z = int.Parse(Console.ReadLine()); chencuoi(ref l, x, y, z); Console.WriteLine("ban hay chon 'c' de nhap tiep"); kt = Console.ReadKey(); } while (kt.KeyChar == 'c'); } static void Hien(list l) { dnode p = new dnode(); p = l.head; while (p != null) { Console.WriteLine("\t{0}\t{1}\t{2}\n", p.tenchinhanh, p.diachi, p.sodienthoai); p = p.next; } } //static void Hien1(list l) //{ // dnode p = new dnode(); // p = l.head; // while (p != null) // { // Console.WriteLine("\t{0}\t{1}\t{2}\n", p.tenchinhanh, p.diachi, p.sodienthoai); // p = p.pre; // } //} static void Xoadau(ref list l) { dnode p; //=new dnode(); if (l.head != null) { p = l.head; l.head = l.head.next; // dich chuyen con tro head den nut tiep theo // l.head.pre = null; // neu danh sach rong se sinh ra loi o cau lenh nay p = null; // huy bo nut dau tien trong danh sach if (l.head == null) // danh sach rong l.tail = null; else l.head.pre = null; // gan truong pre cua nut dau tien trong danh sach moi bang null } } static void xoacuoi(ref list l) { dnode p;// = new dnode(); if (l.tail != null) { p = l.tail; l.tail = l.tail.pre; //l.tail.next = null; // neu danh sach rong se sinh ra loi o cau lenh nay p = null; /* if (l.head == null) Cau lenh nay phai sua lai thanh l.tail = null; */ if (l.tail == null) l.head = null; else // l.head.pre = null; cau lenh nay bi sai phai sua lai thanh l.tail.next = null; } } static void xoasauphantuq(ref list l, dnode q) { // doan nay bi thua //Console.WriteLine("ban muon xoa sau phan tu nay ."); //Console.Write("Ten chi nhanh :"); string a = Console.ReadLine(); //Console.Write("Ten chi nhanh :"); string b = Console.ReadLine(); //Console.Write("so dien thoai :"); int c = int.Parse(Console.ReadLine()); //q = Tim( l, a, b, c); if (q != null) { dnode p = q.next; if (p != null) { q.next = p.next; if (p == l.tail) l.tail = q; else p.next.pre = q; // nut tiep theo cua nut bi xoa co truong pre tro den q p = null; } else { Xoadau(ref l); } } } static void xoatruocphantuq(ref list l, dnode q) { // Console.WriteLine("ban muon xoa truoc phan tu nao"); // Console.Write("Ten chi nhanh :"); string a = Console.ReadLine(); // Console.Write("Ten chi nhanh :"); string b = Console.ReadLine(); // Console.Write("so dien thoai :"); int c = int.Parse(Console.ReadLine()); // dnode p = new dnode(); // q = Tim(l,a,b,c); if (q != null) { dnode p = q.pre; //p tro den nut truoc cua q if (p != null) { q.pre = p.pre; if (p == l.head) // nut can xoa la nut dau danh sach l.head = q; // q se tro thanh nut dau danh sach else { p.pre.next = q; // truong next cua nut trc q tro den q } p = null; } else { xoacuoi(ref l); } } } static void huyphantukhoak(ref list l) { Console.WriteLine("ban muon xoa ngay phan tu nao"); Console.Write("Ten chi nhanh :"); string a = Console.ReadLine(); Console.Write("dia chi chi nhanh :"); string b = Console.ReadLine(); Console.Write("so dien thoai :"); int c = int.Parse(Console.ReadLine()); // dnode p = l.head; dnode q = Tim(l, a, b, c); // nut q la nut can xoa if (q != null) { dnode p = q.pre; xoasauphantuq(ref l, p); } /* while (p != null) { if (p.tenchinhanh == a || p.diachi ==b|| p.sodienthoai == c) break; p = p.next; } */ // q = p.pre; sai /* if (q != null) { p = q.next; if(p != null); { q.next=p.next; if(p==l.tail) {l.tail=p;} else p.next.pre=q; } } else{ l.head=p.next; if(l.head==null)l.tail=null; else l.head.pre=null;}p=null; */ } static void Main(string[] args) { list l = new list(); dnode q ;//= new dnode(); dnode p ;//= new dnode(); Nhap(ref l); Hien(l); /* doan nay bi sai ko co y ngia gi ca * Console.WriteLine("Ban muon them vao hay nhap "); Console.Write("Ten chi nhanh :"); string x = Console.ReadLine(); Console.Write("Ten chi nhanh :"); string y = Console.ReadLine(); Console.Write("so dien thoai :"); int z = int.Parse(Console.ReadLine()); chensauphantu(ref l, q, x, y, z); Hien(l); Console.WriteLine("Ban muon them vao hay nhap "); Console.Write("Ten chi nhanh :"); string d = Console.ReadLine(); Console.Write("Ten chi nhanh :"); string e = Console.ReadLine(); Console.Write("so dien thoai :"); int f = int.Parse(Console.ReadLine()); chentruocmotphantu(ref l, q, d, e, f); Hien(l); * */ Console.WriteLine("\txoa dau\n "); Xoadau(ref l); Hien(l); Console.WriteLine("\txoa cuoi\n "); xoacuoi(ref l); Hien(l); // can chi ro em muon xao sau phan tu nao Console.WriteLine("Ban hay nhap thong tin cua nut ban muon xoa "); Console.Write("Ten chi nhanh :"); string d = Console.ReadLine(); Console.Write("Dia chi chi nhanh :"); string e = Console.ReadLine(); Console.Write("so dien thoai :"); int f = int.Parse(Console.ReadLine()); q = Tim(l, d, e, f); if (q != null) { q = q.pre; xoasauphantuq(ref l, q); } Hien(l); Console.WriteLine("Ban hay nhap thong tin cua nut ban muon xoa "); Console.Write("Ten chi nhanh :"); d = Console.ReadLine(); Console.Write("Dia chi chi nhanh :"); e = Console.ReadLine(); Console.Write("so dien thoai :"); f = int.Parse(Console.ReadLine()); q = Tim(l, d, e, f); if (q != null) { q = q.next; xoatruocphantuq(ref l, q); } Hien(l); int i; do { huyphantukhoak(ref l); Hien(l); Console.Write(" Nhap 0 de ket thuc, hoac so khac 0 detep tuc xoa:"); i = int.Parse(Console.ReadLine()); } while (i != 0); Console.ReadKey (); } }} . neu danh sach rong se sinh ra loi o cau lenh nay p = null; // huy bo nut dau tien trong danh sach if (l.head == null) // danh. luon la vong lap vo han neu danh sach khac rong hoac nut can tim ko phai la nut dau tien, vi em luon gan con tro p ve dau danh sach p =

Ngày đăng: 17/08/2012, 11:34

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan