Quản lý sinh viên bằng DSLK đơn
Trong bài viết này, codehow sẽ hướng dẫn các bạn xây dựng chương trình quản lý sinh viên bằng DSLK đơn. Đây là một chương trình để ôn lại tổng quát các kiến thức mà chúng ta đã học về DSLK đơn ở các bài trước.
Chương trình bao gồm các thao tác như thêm, sửa, xóa và giá trị của các node được khởi tạo sẵn. Nếu các bạn muốn nhập dữ liệu động thì sử dụng câu lệnh nhập xuất để thêm nhé.
Xây dựng chương trình quản lý sinh viên sử dụng DSLK đơn
Đề bài: Cho cấu trúc của một sinh viên bao gồm: MSSV kiểu int và tên kiểu char. Sử dụng danh sách liên kết đơn được cài đặt bằng pHead để thực hiện các thao tác sau đây.
- Khởi tạo cấu trúc DSLK đơn.
- Khởi tạo cấu trúc node.
- Khởi tạo cấu trúc SinhVien.
- Thêm node vào cuối danh sách.
- Sắp xếp theo mã.
- Xóa node.
Bây giờ chúng ta sẽ đi thực hiện từng yêu cầu một nhé.
Đầu tiên sẽ khởi tạo cấu trúc của DSLK đơn. Trong hàm ta sử dụng pHead để tạo cấu trúc dữ liệu. Đây là cách tạo cơ bản nhất.
//khoi tao danh sach lien ket don void Initialize(SingleList *&list) { list=new SingleList; list->pHead=NULL; }
Tiếp đến khởi tạo cấu trúc node. Trong cấu trúc node bao gồm hai phần đó là data và pNext.
//tao cau truc node struct Node { SinhVien *data; Node *pNext; }; struct SingleList { Node *pHead; };
Khởi tạo cấu trúc SinhVien. Trong cấu trúc có hai thông tin đó là mã sinh viên và tên sinh viên.
//tao cau truc sinh vien struct SinhVien { int ma; char ten[150]; };
Tạo hàm thêm node vào cuối danh sách.
//them node vao cuoi danh sach void InsertLast(SingleList *&list,SinhVien *sv) { Node *pNode=CreateNode(sv); if(list->pHead==NULL) { list->pHead=pNode; } else { Node *pTmp=list->pHead; while(pTmp->pNext!=NULL) { pTmp=pTmp->pNext; } pTmp->pNext=pNode; } }
Tạo hàm sắp xếp danh sách. Sử dụng vòng lặp for để lặp từng phần tử trong danh sách. Sau đó so sánh và hoán đổi vị trí cho đúng với thứ tự.
//sap xep void SortList(SingleList *&list) { for(Node *pTmp=list->pHead;pTmp!=NULL;pTmp=pTmp->pNext) { for(Node *pTmp2=pTmp->pNext;pTmp2!=NULL;pTmp2=pTmp2->pNext) { SinhVien *svTmp=pTmp->data; SinhVien *svTmp2=pTmp2->data; if(svTmp2->ma<svTmp->ma) { int ma=svTmp->ma; char ten[150]; strcpy(ten,svTmp->ten); svTmp->ma=svTmp2->ma; strcpy(svTmp->ten,svTmp2->ten); svTmp2->ma=ma; strcpy(svTmp2->ten,ten); } } } }
Tạo hàm xóa node trong danh sách bất kì. Sử dụng vòng lặp while để lặp từng phần tử trong danh sách. Nếu tồn tại phần tử cần xóa trong danh sách ta thực hiện xóa nó, ngược lại nếu không tìm thấy ta thông báo ra màn hình.
//xoa void RemoveNode(SingleList *&list,int ma) { Node *pDel=list->pHead; if(pDel==NULL) { cout<<"Danh sách rỗng!"; } else { Node *pPre=NULL; while(pDel!=NULL) { SinhVien *sv=pDel->data; if(sv->ma==ma) break; pPre=pDel; pDel=pDel->pNext; } if(pDel==NULL) { cout<<"Không tìm thấy MSSV: "<<ma; } else { if(pDel==list->pHead) { list->pHead=list->pHead->pNext; pDel->pNext=NULL; delete pDel; pDel=NULL; } else { pPre->pNext=pDel->pNext; pDel->pNext=NULL; delete pDel; pDel=NULL; } } } }
Hàm hiển thị danh sách liên kết.
//hien thi danh sach void PrintList(SingleList *list) { Node *pTmp=list->pHead; if(pTmp==NULL) { cout<<"Danh sách rỗng!"; return; } while(pTmp!=NULL) { SinhVien *sv=pTmp->data; cout<<sv->ma<<"\t"<<sv->ten<<"\n"; pTmp=pTmp->pNext; } }
Full code:
#include <iostream> #include <string.h> #include <stdio.h> using namespace std; //tao cau truc sinh vien struct SinhVien { int ma; char ten[150]; }; //tao cau truc node struct Node { SinhVien *data; Node *pNext; }; struct SingleList { Node *pHead; }; //khoi tao danh sach lien ket don void Initialize(SingleList *&list) { list=new SingleList; list->pHead=NULL; } //nhap thong tin sinh vien SinhVien *NhapSinhVien() { SinhVien *sv=new SinhVien; cout<<"Nhập MSSV:"; cin>>sv->ma; cin.ignore(); cout<<"Nhập tên:"; cin >> sv->ten; return sv; } //tao node sinh vien Node *CreateNode(SinhVien *sv) { Node *pNode=new Node; if(pNode!=NULL) { pNode->data=sv; pNode->pNext=NULL; } else { cout<<"Cấp phát bộ nhớ thất bại!!!"; } return pNode; } //them node vao cuoi danh sach void InsertLast(SingleList *&list,SinhVien *sv) { Node *pNode=CreateNode(sv); if(list->pHead==NULL) { list->pHead=pNode; } else { Node *pTmp=list->pHead; while(pTmp->pNext!=NULL) { pTmp=pTmp->pNext; } pTmp->pNext=pNode; } } //hien thi danh sach void PrintList(SingleList *list) { Node *pTmp=list->pHead; if(pTmp==NULL) { cout<<"Danh sách rỗng!"; return; } while(pTmp!=NULL) { SinhVien *sv=pTmp->data; cout<<sv->ma<<"\t"<<sv->ten<<"\n"; pTmp=pTmp->pNext; } } //sap xep void SortList(SingleList *&list) { for(Node *pTmp=list->pHead;pTmp!=NULL;pTmp=pTmp->pNext) { for(Node *pTmp2=pTmp->pNext;pTmp2!=NULL;pTmp2=pTmp2->pNext) { SinhVien *svTmp=pTmp->data; SinhVien *svTmp2=pTmp2->data; if(svTmp2->ma<svTmp->ma) { int ma=svTmp->ma; char ten[150]; strcpy(ten,svTmp->ten); svTmp->ma=svTmp2->ma; strcpy(svTmp->ten,svTmp2->ten); svTmp2->ma=ma; strcpy(svTmp2->ten,ten); } } } } //xoa void RemoveNode(SingleList *&list,int ma) { Node *pDel=list->pHead; if(pDel==NULL) { cout<<"Danh sách rỗng!"; } else { Node *pPre=NULL; while(pDel!=NULL) { SinhVien *sv=pDel->data; if(sv->ma==ma) break; pPre=pDel; pDel=pDel->pNext; } if(pDel==NULL) { cout<<"Không tìm thấy MSSV: "<<ma; } else { if(pDel==list->pHead) { list->pHead=list->pHead->pNext; pDel->pNext=NULL; delete pDel; pDel=NULL; } else { pPre->pNext=pDel->pNext; pDel->pNext=NULL; delete pDel; pDel=NULL; } } } } int main(int argc, char** argv) { SingleList *list; Initialize(list); SinhVien *teo=NhapSinhVien(); InsertLast(list,teo); SinhVien *ty=NhapSinhVien(); InsertLast(list,ty); SinhVien *bin=NhapSinhVien(); InsertLast(list,bin); PrintList(list); SortList(list); cout<<"\nSau khi sắp xếp:\n"; PrintList(list); cout<<"\nBạn muốn xóa sinh viên có MSSV: "; int ma; cin>>ma; RemoveNode(list,ma); cout<<"\nSau khi xóa:\n"; PrintList(list); cout<<"\n---------------------------\n"; cout<<"Chương trình này được đăng tại codehow.net"; }
Kết quả:
Như vậy là chúng ta đã cùng nhau thực hiện chương trình quản lý sinh viên trong DSLK đơn. Đây là dạng bài tập cơ bản để áp dụng các kiến thức đã học. Các bạn hãy luyện tập thật nhiều để sử dụng DSLK đơn một cách thành thạo nhé. Chúc các bạn thành công.