Code bài toán quản li,sắp xếp, tìm kiếm sử dụng Liên kết kép trong C Khai bao cau truc du lieu 2 khoi tao nut moi 3 tao list 3 khoi tao dia chi daucuoi 3 tao nut moi 3 Insert the first 4 Insert the last 5 huy 1 phan tu o dau 5 Nhap danh sach 1 khach hang 6 Nhap danh sach n khach hang 6 Hien danh sach ds vua nhap 7 Dem so khach hang 7 Tim ma khach hang k trong danh sach 8 Sap xep khach hang theo tuoi 9 Nhap danh sach 1 nhan vien 9 Nhap danh sach n nhan vien 10 Hien danh sach ds vua nhap 10 Tinh tong luong cua nhan vien 11 Tim ma nhan vien k trong danh sach 11 Sap xep nhan vien theo luong 12 Nhap thong tin mot hang hoa 13 Ham in thong tin cua 1 hh 13 Nhap danh sach hang hoa 13 in danh sach hang hoa 14 Tinh tong tien cac hh 14
Mục lục Code toán quản li,sắp xếp, tìm kiếm sử dụng Liên kết kép C //Khai bao cau truc du lieu //khoi tao nut moi .3 //tao list //khoi tao dia chi dau-cuoi // tao nut moi /// Insert the first .4 /// Insert the last .5 /// huy phan tu o dau -///// /// Nhap danh sach khach hang /// Nhap danh sach n khach hang /// Hien danh sach ds vua nhap .7 /// Dem so khach hang - /// Tim ma khach hang k danh sach /// Sap xep khach hang theo tuoi /// Nhap danh sach nhan vien .9 /// Nhap danh sach n nhan vien 10 /// Hien danh sach ds vua nhap 10 /// Tinh tong luong cua nhan vien 11 /// Tim ma nhan vien k danh sach 11 /// Sap xep nhan vien theo luong 12 // Nhap thong tin mot hang hoa 13 // Ham in thong tin cua hh 13 // Nhap danh sach hang hoa .13 // in danh sach hang hoa 14 // Tinh tong tien cac hh 14 //tim kiem mot hang hoa co ma x nao .15 // sap xep DSHH tang dan theo so luong .15 // in danh sach hang hoa co so luong lon hon 10 16 ///Menu .17 //Menu 17 //Menu 18 //chuong trinh chinh 19 #include #include #include #include int i,n; //Khai bao cau truc du lieu struct data { // khach hang int makh; char hoten[50]; int sdt; char diachi[50]; // nhan vien int manv; char tenNV[25]; int tuoi; float luong,phucap,TT; //hang hoa int mahh; char tenhh[20]; int dg,sl,tt; }; //khoi tao nut moi struct dnode { data infor; dnode* next; dnode* prev; }; //tao list struct dlist { dnode *head; dnode *tail; }; dlist DQ; //khoi tao dia chi dau-cuoi void init(dlist &DQ) { DQ.head=NULL; DQ.tail=NULL; } // tao nut moi dnode* get_node(data x) { dnode *p; p=(dnode*)malloc(sizeof(dnode)); if(p==NULL) { printf("Khong du bo nho!"); exit(1); } p->infor=x; p->next=NULL; p->prev=NULL; return p; } /// Insert the first -void insert_first(dlist &DQ,data x){ dnode *p= get_node(x); if(DQ.head==NULL){ DQ.head = p; DQ.tail = p; } else{ p ->next =DQ.head; DQ.head->prev = p; DQ.head = p; } } /// Insert the last -void insert_last(dlist &DQ,data x){ dnode *p= get_node(x); if(DQ.head==NULL){ DQ.head = p; DQ.tail = p; } else{ DQ.tail->next = p; p->prev = DQ.tail; DQ.tail = p; } } /// huy phan tu o dau -///// void removehead(dlist &DQ) { dnode *p; if (DQ.head != NULL) { p = DQ.head; DQ.head = DQ.head->next; DQ.head->prev=NULL; free(p); } if (DQ.head == NULL) DQ.tail = NULL; } /// Nhap danh sach khach hang -void input_1ds(data &x){ printf("\nNhap ma khach hang: "); scanf("%d",&x.makh); fflush(stdin); printf("\nNhap ho ten: "); gets(x.hoten); fflush(stdin); printf("\nNhap tuoi: "); scanf("%d",&x.tuoi); printf("\nNhap dia chi: "); scanf("%s",&x.diachi); printf("\nNhap sdt khach hang: "); scanf("%d",&x.sdt); } /// Nhap danh sach n khach hang -void input_ds(dlist &DQ){ dnode *p; p=DQ.head; data x; for(i=1;iinfor.makh, p->infor.hoten, p->infor.tuoi, p->infor.diachi, p->infor.sdt); p=p->next; printf(" \n"); } } /// Dem so khach hang int demkh(dlist &DQ){ dnode *p; p=DQ.head; int dem=0; while(p!=NULL){ dem=dem+1; p=p->next; } return dem; } /// Tim ma khach hang k danh sach -void find_makh(dlist &DQ,int k){ dnode *p; int dem=0; for(p=DQ.head;p!=NULL;p=p->next){ if(p->infor.makh == k){ dem++; break; } } if(dem!=0){ printf(" \t Da tim thay "); } else printf(" \t Khong tim thay "); } /// Sap xep khach hang theo tuoi -void sapxep(dlist &DQ){ dnode *p,*q; p=DQ.head; data tmp; while(p!=NULL){ q=p->next; while(q!=NULL){ if(p->infor.tuoi > q->infor.tuoi){ tmp = p->infor; p->infor = q->infor; q->infor = tmp; } q=q->next; } p=p->next; } } /// Nhap danh sach nhan vien -void input_nv(data &x){ printf("\nNhap ma nhan vien: "); scanf("%d",&x.manv); fflush(stdin); printf("\nNhap ho ten: "); gets(x.hoten); fflush(stdin); printf("\nNhap tuoi: "); scanf("%d",&x.tuoi); printf("\nNhap luong nhan vien: "); scanf("%f",&x.luong); printf("\nNhap phu cap nhan vien: "); scanf("%f",&x.phucap); } /// Nhap danh sach n nhan vien -void input_dsnv(dlist &DQ){ dnode *p; p=DQ.head; data x; for(i=1;iinfor.manv, p>infor.hoten, p->infor.tuoi, p->infor.luong, p->infor.phucap); p=p->next; printf(" \n"); } } /// Tinh tong luong cua nhan vien -int total(dlist &DQ){ dnode *p; p=DQ.head; int tong=0; while(p!=NULL){ tong+=p->infor.luong + p->infor.phucap; p=p->next; } return tong; } /// Tim ma nhan vien k danh sach -void find_manv(dlist &DQ,int k){ dnode *p; int dem=0; for(p=DQ.head;p!=NULL;p=p->next){ if(p->infor.manv == k){ dem++; break; } } if(dem!=0){ printf(" \t Da tim thay "); } else printf(" \t Khong tim thay "); } /// Sap xep nhan vien theo luong -void sapxepluong(dlist &DQ){ dnode *p,*q; p=DQ.head; data tmp; while(p!=NULL){ q=p->next; while(q!=NULL){ if(p->infor.luong > q->infor.luong){ tmp = p->infor; p->infor = q->infor; q->infor = tmp; } q=q->next; } p=p->next; } } // Nhap thong tin mot hang hoa void nhaphh(data &x) { printf("\n Nhap ma hang hoa: "); scanf("%d",&x.mahh); printf(" Nhap ten hang hoa: "); fflush(stdin); gets(x.tenhh); printf(" Nhap so luong: "); scanf("%d",&x.sl); printf(" Nhap don gia: "); scanf("%d",&x.dg); x.tt=x.dg*x.sl; } // Ham in thong tin cua hh void inhh(data x) { printf("\n %5d",x.mahh); printf("%15s",x.tenhh); printf("%10d",x.sl); printf("%10d",x.dg); printf("%10d",x.tt); } // Nhap danh sach hang hoa void nhapdshh(dlist &DQ) { data x; dnode *p; for(i=0;inext) inhh(p->infor); } // Tinh tong tien cac hh int tongtien(dlist &DQ) { dnode *p; int tt; tt=0; for(p=DQ.head; p!=NULL; p=p->next) tt=tt + (p->infor.sl*p->infor.dg); printf("\n Tong tien hang hoa la : %d",tt); } //tim kiem mot hang hoa co ma x nao dnode *timhhma(dlist DQ, int x) { dnode *p; if(DQ.head==NULL) printf("\n Danh sach rong"); else { for(p=DQ.head; p!=NULL; p=p->next) if(p->infor.mahh==x) break; } if (p==NULL) printf(" Khong tim thay"); else printf(" Co tim thay"); } // sap xep DSHH tang dan theo so luong void sapxepsl(dlist DQ) { dnode *p,*q; data tg; if(DQ.head==NULL) printf("\n\n Danh sach rong"); else { printf("\n\n DSHH tang dan theo so luong"); printf("\n %5s%15s%10s%10s %10s","Mahh","Tenhh","Soluong","Dongia","Tongtien"); for(p=DQ.head; p!=NULL; p=p->next) for(q=p->next; q!=NULL; q=q->next) if (p->infor.sl>q->infor.sl) { tg=p->infor; p->infor=q->infor; q->infor=tg; } for(p=DQ.head; p!=NULL; p=p->next) inhh(p->infor); } } // in danh sach hang hoa co so luong lon hon 10 void InDSHH(dlist DQ) { dnode *p; printf("\n Danh sach hang hoa co so luong lon hon 10 la:\n"); printf("\n %5s%15s%10s%10s %10S","Mahh","Tenhh","Soluong","Dongia","Tongtien"); for(p=DQ.head;p!=NULL;p=p->next) if(p->infor.sl > 10) inhh(p->infor); } ///Menu int menu1(int &chon1) { printf("\n Moi ban chon thao tac thuc hien:"); printf("\n Thoat"); printf("\n Nhap danh sach Khach Hang"); printf("\n Hien danh sach Khach Hang"); printf("\n Dem so Khach Hang"); printf("\n Tim Ma Khach Hang k danh sach"); printf("\n Sap xep danh sach khach hang theo tuoi"); printf("\n Them khach hang vao dau danh sach"); printf("\n Them khach hang vao cuoi danh sach"); printf("\n Chon chuc nang: "); scanf("%d",&chon1); } //Menu int menu2(int &chon2) { printf("\n Moi ban chon thao tac thuc hien:"); printf("\n Thoat"); printf("\n Nhap danh sach Nhan Vien"); printf("\n Hien danh sach Nhan Vien"); printf("\n Tinh Tong Luong Nhan Vien"); printf("\n Tim Ma Nhan Vien k danh sach"); printf("\n Sap xep danh sach khach hang theo luong"); printf("\n Chon chuc nang: "); scanf("%d",&chon2); } //Menu int menu3(int &chon3) { printf("\n Moi ban chon thao tac thuc hien:"); printf("\n Thoat"); printf("\n Nhap danh sach Hang Hoa"); printf("\n Hien danh sach Hang Hoa"); printf("\n Tinh Tong tien Hang Hoa"); printf("\n Tim Ma Hang Hoa k danh sach"); printf("\n Sap xep danh sach khach hang theo so luong"); printf("\n IN DSHH co SL > 10"); printf("\n Chon chuc nang: "); scanf("%d",&chon3); } //chuong trinh chinh int main() { dnode *search_key; dlist quanli; ///Khoi tao danh sach int option,k; data x; init(quanli); do{ printf("\n Moi ban chon thao tac thuc hien:"); printf("\n Thoat"); printf("\n Quan Li Khach Hang"); printf("\n Quan Li Nhan Vien"); printf("\n Quan Li Hang Hoa"); printf("\n Chon chuc nang: "); scanf("%d",&option); switch (option){ case 0: ///thoat return 0; case 1: ///Quan Li Khach Hang int chon1; { menu1(chon1); //Menu switch (chon1) { case 0: ///thoat return 0; case 1: ///Nhap danh sach khach hang printf("\n Moi nhap so khach hang n= "); scanf("%d",&n); input_ds(quanli); break; case 2: ///Hien danh sach khach hang vua nhap printf("\n-> Danh sach ban vua nhap la: \n\n"); output_ds(quanli); break; case 3: ///Dem tong so khach hang printf("\n -> Tong so khach hang la: %d \n",demkh(quanli)); break; case 4: ///Tim ma khach hang k printf("\n -> Moi nhap Ma khach hang muon tim: k= "); scanf("%d",&k); printf("\n -> Ma khach hang k= %d: ",k); find_makh(quanli,k); break; case 5: ///Sap xep danh sach theo tuoi printf("\n -> Danh sach sau sap xep theo tuoi la: \n\n"); sapxep(quanli); output_ds(quanli); break; case 6: ///Them nhan vien vao dau ds printf("\n -> Nhap khach hang muon them vao dau danh sach: \n\n"); input_1ds(x); insert_first(quanli,x); break; case 7: ///Them nhan vien vao cuoi ds printf("\n -> Nhap khach hang muon them vao cuoi danh sach: \n\n"); input_1ds(x); insert_last(quanli,x); break; default: ///Tra ve gia tri mac dinh neu chon sai printf("\n -> Chon sai! Moi ban chon lai!"); } }while(chon1 != 0); ///ket thuc vong lap neu chon thoat case 2: ///Quan Li Nhan Vien int chon2; { menu2(chon2); //Menu switch (chon2) { case 0: ///thoat return 0; case 1: ///Nhap danh sach nhan vien printf("\n Moi nhap so nhan vien n= "); scanf("%d",&n); input_dsnv(quanli); break; case 2: ///Hien danh sach vua nhap printf("\n-> Danh sach ban vua nhap la: \n\n"); output_dsnv(quanli); break; case 3: ///Dem tong so luong printf("\n -> Tong luong nhan vien la: %d \n",total(quanli)); break; case 4: ///Tim ma khach hang k printf("\n -> Moi nhap Ma nhan vien muon tim: k= "); scanf("%d",&k); printf("\n -> Ma nhan vien k= %d: ",k); find_manv(quanli,k); break; case 5: ///Sap xep danh sach theo luong printf("\n -> Danh sach sau sap xep theo luong la: \n\n"); sapxepluong(quanli); output_dsnv(quanli); break; default: ///Tra ve gia tri mac dinh neu chon sai printf("\n -> Chon sai! Moi ban chon lai!"); } }while(chon2 != 0); ///ket thuc vong lap neu chon thoat case 3: ///Quan Li Hang Hoa int chon3; { menu3(chon3); //Menu switch (chon3) { case 0: ///thoat return 0; case 1: ///Nhap danh sach hang hoa printf("\n Moi nhap so hang hoa n= "); scanf("%d",&n); nhapdshh(quanli); break; case 2: ///Hien danh sach vua nhap printf("\n-> Danh sach ban vua nhap la: \n\n"); indshh(quanli); break; case 3: ///Tinh tong tien hang hoa tongtien(quanli); break; case 4: ///Tim ma hang hoa k printf("\n -> Moi nhap Ma Hang Hoa muon tim: k= "); scanf("%d",&k); printf("\n -> Ma Hang Hoa k= %d: ",k); timhhma(quanli,k); break; case 5: ///Sap xep danh sach theo so luong printf("\n -> Danh sach sau sap xep theo so luong la: \n\n"); sapxepsl(quanli); indshh(quanli); break; case 6: // In DSHH co SL >10 printf("\n DSHH co so luong > 10 la:\n"); InDSHH(quanli); break; default: ///Tra ve gia tri mac dinh neu chon sai printf("\n -> Chon sai! Moi ban chon lai!"); } }while(chon3 != 0); ///ket thuc vong lap neu chon thoat } }while(option !=0); } ... Hoa"); printf(" Chon chuc nang: "); scanf("%d",&option); switch (option){ case 0: ///thoat return 0; case 1: ///Quan Li Khach Hang int chon1; { menu1(chon1); //Menu switch (chon1) { case 0: ///thoat... Khach Hang k danh sach"); printf(" Sap xep danh sach khach hang theo tuoi"); printf(" Them khach hang vao dau danh sach"); printf(" Them khach hang vao cuoi danh sach"); printf(" Chon chuc... Hoa k danh sach"); printf(" Sap xep danh sach khach hang theo so luong"); printf(" IN DSHH co SL > 10"); printf(" Chon chuc nang: "); scanf("%d",&chon3); } //chuong trinh chinh int main()