#include #include #include #include #define MAX 100 using namespace std; struct SinhVien{ char hoten[20]; float dtb; }; struct Node { SinhVien sv; Node *next; }; struct List { Node *dau, *cuoi; }; void Them_dau_danh_sach(List &l){ SinhVien x; cout << "\nHo ten: "; fflush(stdin); gets(x.hoten); cout << "\nDiem tb: "; cin >> x.dtb; Node *p = (Node*) malloc (sizeof(Node)); p -> sv = x; p -> next = NULL; if (!l.dau) { l.dau = l.cuoi = p; } else { p -> next = l.dau; l.dau = p; } } void Them_cuoi_danh_sach(List &l){ SinhVien x; cout << "\nHo ten: "; fflush(stdin); gets(x.hoten); cout << "\nDiem tb: "; cin >> x.dtb; Node *p = (Node*)malloc(sizeof(Node)); p -> sv = x; p -> next = NULL; if (!l.dau){ l.dau = l.cuoi = p; } else { p -> next = NULL; l.cuoi -> next = p; l.cuoi = p; } } void NhapDS(List &l,int n){ int i; for (i = 0;i< n;i++){ Them_cuoi_danh_sach(l); } } void HienDS(List &l){ Node *i = l.dau; while (i) { cout << "\nHo ten: " << i->sv.hoten; cout << "\nDiem tb: " << i->sv.dtb; i = i -> next; } } void XoaDauList(List &l){ cout << "\nXoa dau list"; if (!l.dau){ // xet danh sach rong return; } else if (l.dau == l.cuoi) { l.dau = l.cuoi = NULL; } else { Node *p = l.dau; l.dau = p -> next; free(p); } HienDS(l); } void XoaCuoiList ( List &l){ cout << "\nXoa cuoi list"; if (!l.dau){ // xet danh sach rong return; } else if (l.dau == l.cuoi){ l.dau = l.cuoi = NULL; } else { Node *p = l.dau; Node *q = (Node*)malloc(sizeof(Node)); while ( p -> next != NULL){ q = p; p = p -> next; } q -> next = NULL; l.cuoi = q; free(q); } HienDS(l); } //void Them_vitri_k ( List &l, int n ){ // cout << "\nThem vi tri thu k"; // int k; // cout << "\nk: "; cin >> k; // if (k <= 1){ // Them_dau_danh_sach(l); // } else if (k >= n) { // Them_cuoi_danh_sach(l); // } else { // int vt = 1; // while ( vt != k-1 && p -> next != NULL ){ // p = p -> next; // vt ++; // } // } //} // //void Xoa_vitri_k(List &l,int n){ // cout << "\nThem vi tri thu k"; // int k; // cout << "\nk: "; cin >> k; // if (k <= 1){ // XoaDauList(l); // } else if (k >= n) { // XoaCuoiList(l); // } else { // int vt = 1; // while (vt != k-1 && p -> next != NULL) { // p = p -> next; // vt++ // } // p -> next = p -> next -> next // } //} int main(){ int n; cout << "\nn: "; cin >> n; List l; l.dau = l.cuoi = NULL; NhapDS(l,n); HienDS(l); XoaCuoiList(l); return 0; }