Share mấy bác bài này tham khảo chơi Nhớ Thanks nhé Mã: /***************************************************************************** * Chuong trinh : Chuong trinh quan ly danh sach sinh vien, cai dat bang * * danh sach ke * * Nguoi viet chuong trinh: Khong ro * * Ngay cap nhat moi nhat: 13/10/2011 * *****************************************************************************/ #include <stdio.h> #include <conio.h> #define MAXLIST 100 #define TRUE 1 #define FALSE 0 typedef struct sinhvien { int mssv; char hoten[20]; }; typedef struct list { int numnodes; sinhvien nodes[MAXLIST]; }; // tac vu initialize: khoi dong danh sach ke void initialize(struct list *plist) { plist->numnodes = 0; } // Tac vu listsize: xac dinh so nut trong danh sach int listsize(struct list *plist) { return(plist->numnodes); } // Tac vu empty: kiem tra danh sach co bi rong khong int empty(struct list *plist) { return((plist->numnodes == 0) ? TRUE : FALSE); } // Tac vu full: kiem tra danh sach bi day khong int full(struct list *plist) { return((plist->numnodes == MAXLIST) ? TRUE : FALSE); } // Tac vu insert: chen nut co noi dung x vao vi tri pos void insert(struct list *plist, int pos, sinhvien x) { int i; if(pos < 0 || pos > listsize(plist)) { printf("Vi tri chen khong phu hop."); return; } else if(full(plist)) { printf("Danh sach bi day."); return; } else { for(i = listsize(plist)-1; i >= pos; i--) plist->nodes[i+1] = plist->nodes[i]; plist->nodes[pos] = x; plist->numnodes++; } } // Tac vu remove: xoa nut tai vi tri pos trong danh sach sinhvien remove(struct list *plist, int pos) { int i; sinhvien x; if(pos < 0 || pos >= listsize(plist)) printf("Vi tri xoa khong phu hop."); else if(empty(plist)) printf("Danh sach khong co sinh vien."); else { x = plist->nodes[pos]; for(i = pos; i <= listsize(plist)-1; i++) plist->nodes[i] = plist->nodes[i+1]; plist->numnodes--; return(x); } } // Tac vu clearlist: xoa tat ca cac nut trong danh sach void clearlist(struct list *plist) { plist->numnodes = 0; } // Tac vu replace: hieu chinh noi dung cua nut tai vi tri pos trong danh sach void replace(struct list *plist, int pos, sinhvien x) { if(pos < 0 || pos >= listsize(plist)) { printf("Vi tri hieu chinh khong phu hop."); return; } else if(empty(plist)) { printf("Danh sach khong co sinh vien."); return; } else plist->nodes[pos] = x; } // Tac vu traverse: duyet danh sach sinh vien void traverse(struct list *plist) { int i; if(plist->numnodes == 0) { printf("\n Danh sach khong co sinh vien"); return; } for(i = 0; i < plist->numnodes; i++) printf("\n%7d%7d%16s", i, plist->nodes[i].mssv, plist->nodes[i].hoten); } // Tac vu selectionsort: sap xep danh sach theo MSSV tang dan void selectionsort(struct list *plist) { int i, j, vitrimin; sinhvien svmin; for(i = 0; i < plist->numnodes-1; i++) { svmin = plist->nodes[i]; vitrimin = i; for(j = i+1; j < plist->numnodes; j++) if(svmin.mssv > plist->nodes[j].mssv) { svmin = plist->nodes[j]; vitrimin = j; } // hoan doi plist->nodes[vitrimin] = plist->nodes[i]; plist->nodes[i] = svmin; } } /* Tac vu linearsearch: tim kiem tuyen tinh, neu khong tim thay ham nay tra ve tri -1, neu tim thay ham nay tra ve vi tri tim thay */ int linearsearch(struct list *plist, int mssv) { int vitri = 0; while(plist->nodes[vitri].mssv != mssv && vitri < plist->numnodes) vitri++; if(vitri == plist->numnodes) // khong tim thay return(-1); return(vitri); // tim thay } void main() { struct list ds; sinhvien sv; int chucnang, vitri; char c; // clrscr(); initialize(&ds); do { // menu chinh cua chuong trinh printf("\n\nCHUONG TRINH QUAN LY DANH SACH SINH VIEN:\n"); printf("Cac chuc nang cua chuong trinh:\n"); printf(" 1: Xem danh sach sinh vien\n"); printf(" 2: Them mot sinh vien vao danh sach\n"); printf(" 3: Xoa mot sinh vien trong danh sach\n"); printf(" 4: Hieu chinh sinh vien\n"); printf(" 5: Sap xep danh sach theo MSSV\n"); printf(" 6: Tim kiem sinh vien theo MSSV\n"); printf(" 7: Xoa toan bo danh sach\n"); printf(" 0: Ket thuc chuong trinh\n"); printf("Chuc nang ban chon: "); scanf("%d", &chucnang); switch(chucnang) { case 1: { printf("\nDanh sach sinh vien: "); printf("\n STT MSSV HO TEN"); traverse(&ds); break; } case 2: { printf("\nVi tri them (0, 1, 2, ...): "); scanf("%d", &vitri); printf("Ma so sinh vien: "); scanf("%d", &sv.mssv); printf("Ho ten sinh vien: "); scanf("%s", &sv.hoten); insert(&ds, vitri, sv); break; } case 3: { printf("\nVi tri xoa (0, 1, 2, ...): "); scanf("%d", &vitri); remove(&ds, vitri); break; } case 4: { printf("\nVi tri hieu chinh (0, 1, 2, ...): "); scanf("%d", &vitri); printf("\nSTT:%d MSSV:%d HOTEN:%s", vitri, ds.nodes[vitri].mssv, ds.nodes[vitri].hoten); printf("\nMa so sv moi: "); scanf("%d", &sv.mssv); printf("Ho ten sv moi: "); scanf("%s", &sv.hoten); replace(&ds, vitri, sv); break; } case 5: { printf("\nBan co chac khong (c/k): "); c = getche(); if(c == 'c' || c == 'C') selectionsort(&ds); break; } case 6: { printf("\nMa so sinh vien can tim: "); scanf("%d", &sv.mssv); vitri = linearsearch(&ds, sv.mssv); if(vitri == -1) printf("Khong co sinh vien co MSSV %d trong danh sach", sv.mssv); else printf("Tim thay o vi tri %d trong danh sach", vitri); break; } case 7: { printf("\nBan co chac khong (c/k): "); c = getche(); if(c == 'c' || c == 'C') clearlist(&ds); break; } } } while(chucnang != 0); }