Code bài toán quản lí,sắp xếp, tìm kiếm sử dụng liên kết đơn trong C khai bao CTDL dang DSLK don 2 ham khoi tao danh sach 3 ham tao nut chua sach x 3 kiem tra danh sach rong 3 ham chen them mot nut vao dau danh sach 4 chen nut vao cuoi danh sach 4 ham nhap thong tin cua mot quyen sach 5 ham in thong tin cua mot quyen sach 6 nhap danh sach sach 6 ham in danh sach sach 6 ham in hoa don co so luong >5 7 Ham tim kiem sach 7 Ham dem so sach 8 ham tinh tong tien tat ca hoa don 8 cho biet hoa don co tong tien max 9 sap xep hoa don theo thu tu tang dan cua so luong 9 main 11
Mục Lục Code toán quản lí,sắp xếp, tìm kiếm sử dụng liên kết đơn C // khai bao CTDL dang DSLK don // ham khoi tao danh sach // ham tao nut chua sach x //kiem tra danh sach rong // ham chen them mot nut vao dau danh sach // chen nut vao cuoi danh sach // ham nhap thong tin cua mot quyen sach .5 // ham in thong tin cua mot quyen sach // nhap danh sach sach .6 //ham in danh sach sach //ham in hoa don co so luong >5 //Ham tim kiem sach //Ham dem so sach // ham tinh tong tien tat ca hoa don //cho biet hoa don co tong tien max .9 //sap xep hoa don theo thu tu tang dan cua so luong //main 11 #include #include #include #include // khai bao CTDL dang DSLK don struct sach { int MaS,MaHD; char TenS[30]; char TenTG[30]; char Theloai[30]; float Gt, Tt; int SL; }; struct node { sach info; struct node *next; }; struct LIST { node *Head; node *Tail; }; // ham khoi tao danh sach void Init (LIST &Q) { Q.Head = NULL; Q.Tail = NULL; }; // ham tao nut chua sach x node *getnode(sach x){ node *p; p = new node; if(p == NULL){ printf("\n Khong tao nut cong"); exit(0); } else{ p->info = x; p->next = NULL; } return p; }; //kiem tra danh sach rong int empty(LIST Q){ if(Q.Head == NULL) return 1; else return 0; }; // ham chen them mot nut vao dau danh sach void insertHead(LIST &Q, node *p) { if(Q.Head == NULL) { Q.Head = p; Q.Tail = p; } else { p->next = Q.Head; Q.Head = p; } }; // chen nut vao cuoi danh sach void inserttail(LIST &Q, node *p){ if(Q.Head == NULL){ Q.Head = Q.Tail = p; } else { Q.Tail->next = p; Q.Tail = p; } } // ham nhap thong tin cua mot quyen sach void Nhapsach(sach &x) { printf("Nhap ma hoa don: "); scanf("%d", &x.MaHD ); printf("\nNhap ma sach: "); scanf("%d", &x.MaS ); printf("\nNhap ten sach: "); fflush(stdin); gets(x.TenS); printf("\nNhap ten tac gia: "); fflush(stdin); gets(x.TenTG); printf("\nNhap the loai: "); fflush(stdin); gets(x.Theloai); printf("\nNhap gia tien: "); scanf("%f", &x.Gt); printf("\nNhap so luong: "); scanf("%d", &x.SL ); x.Tt=x.Gt*x.SL; } // ham in thong tin cua mot quyen sach void insach(sach x) { printf("\n%3d %5d %10s %12s %10s \t %0.2f %6d \t %0.2f\n",x.MaHD,x.MaS,x.TenS,x.TenTG,x.Theloai,x.Gt,x.SL,x.Tt); } // nhap danh sach sach void nhapdanhsach(LIST &Q, int &n){ int i; sach x; node *p; printf("Nhap so luong hoa don sach: "); scanf("%d", &n); for(i=0; inext) insach(p->info); } //ham in hoa don co so luong >5 void inhd(LIST Q) { node *p; printf("\nDanh sach hoa don co so luong lon hon la:\n"); printf("\n%3s%10s%10s%9s%10s%12s%12s%12s\n","Ma HD","Ma sach","Ten sach","Ten TG","The loai","Giatien","Soluong","Tongtien"); for(p=Q.Head;p!=NULL;p=p->next) if(p->info.SL > 5) insach(p->info); } //Ham tim kiem sach node *Timtensach(LIST Q, char k[]) { node *p; printf("\n%5s%7s%10s%14s%10s%12s%12s%12s\n","Ma HD","Ma sach","Ten sach","Ten TG","The loai","Giatien","Soluong","Tongtien"); for(p=Q.Head;p != NULL;p = p->next) if(strcmp(p->info.TenTG,k)==0) insach(p->info); } //Ham dem so sach node *Demsach(LIST Q, char k2[]) { int d=0; node *p; for(p=Q.Head;p != NULL;p=p->next) if(strcmp(p->info.Theloai,k2)==0) d=d+1; printf("So sach can tim la: %d",d); } // ham tinh tong tien tat ca hoa don void tongtien(LIST Q) { node *p; int tt; tt=0; for(p=Q.Head; p!=NULL; p=p->next) tt=tt + (p->info.SL*p->info.Gt); printf("\n Tong tien hoa don sach la : %d",tt); } //cho biet hoa don co tong tien max void tongmax(LIST Q) { node *p; printf("\nDanh sach hoa don co tong tien lon nhat la:\n"); printf("\n%5s%7s%10s%14s%10s%12s%12s%12s\n","Ma HD","Ma sach","Ten sach","Ten TG","The loai","Giatien","Soluong","Tongtien"); float max=0; for(p=Q.Head; p!=NULL; p=p->next){ if (p->info.Tt>max) { max=p->info.Tt; } } for(p=Q.Head; p!=NULL; p=p->next){ if (p->info.Tt==max) { insach(p->info); } } } //sap xep hoa don theo thu tu tang dan cua so luong void sapxep(LIST Q) { node *p,*q; sach tg; if(Q.Head==NULL) printf("\n\n Danh sach rong"); else { printf("\n\n DSHD tang dan theo so luong"); printf("\n%5s%7s%10s%14s%10s%12s%12s%12s\n","Ma HD","Ma sach","Ten sach","Ten TG","The loai","Giatien","Soluong","Tongtien"); for(p=Q.Head; p!=NULL; p=p->next) for(q=p->next; q!=NULL; q=q->next) if (p->info.SL>q->info.SL) { tg=p->info; p->info=q->info; q->info=tg; } for(p=Q.Head; p!=NULL; p=p->next) insach(p->info); } } //main int main() { LIST Q; node *p,*q; Init(Q); sach x; int chon,n; { printf("\n 0.Thoat"); printf("\n 1.Nhap DSS"); printf("\n 2.In DSS"); printf("\n 3.In hoa don co so luong >5"); printf("\n 4.Tim kiem sach"); printf("\n 5.Tinh tong tien"); printf("\n 6.Hoa don co tong tien max"); printf("\n 7.Sap xep hoa don theo thu tu tang dan cua so luong"); printf("\n Nhap so:"); scanf("%d",&chon); switch(chon) { case 0: exit(0); break; case 1: nhapdanhsach(Q,n); break; case 2: indssach(Q); break; case 3:inhd(Q);break; case 4: char k[30]; printf("Nhap ten tac gia can tim:"); scanf("%s",&k); p=Timtensach(Q,k); break; case 5:tongtien(Q);break; case 6:tongmax(Q);break; case 7:sapxep(Q);break; } } while (chon!=0); getch(); } ... scanf("%d",&chon); switch(chon) { case 0: exit(0); break; case 1: nhapdanhsach(Q,n); break; case 2: indssach(Q); break; case 3:inhd(Q);break; case 4: char k[30]; printf("Nhap ten tac gia can tim:"); scanf("%s",&k);...#include #include #include #include // khai bao CTDL dang DSLK don struct sach { int MaS,MaHD; char TenS[30]; char TenTG[30]; char Theloai[30];... nhap thong tin cua mot quyen sach void Nhapsach(sach &x) { printf("Nhap ma hoa don: "); scanf("%d", &x.MaHD ); printf(" Nhap ma sach: "); scanf("%d", &x.MaS ); printf(" Nhap ten sach: "); fflush(stdin);