Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<malloc.h>
- typedef struct sv {
- int mssv;
- struct sv *psvtieptheo;
- }sv;
- typedef struct DanhSachSinhVien {
- struct sv *head;
- }danhsach;
- void inDanhSachSV(struct DanhSachSinhVien ds) {
- sv *p = ds.head;
- while(p != NULL)
- {
- printf("%d\n", p->mssv);
- p = p->psvtieptheo;
- }
- }
- sv themSinhVien1(struct DanhSachSinhVien *ds, int mssv) {
- sv *svmoi = (sv*)malloc(sizeof(sv));
- svmoi->mssv = mssv;
- sv *p1 = ds->head;
- while(p1->psvtieptheo != NULL)
- {
- p1 = p1->psvtieptheo;
- }
- p1->psvtieptheo = svmoi;
- svmoi->psvtieptheo = NULL;
- }
- sv themSinhVien2(struct DanhSachSinhVien *ds, int mssv1, int mssv2) {
- sv *svmoi1 = (sv*)malloc(sizeof(sv));
- svmoi1->mssv = mssv2;
- sv *pds = ds->head;
- while(pds != NULL)
- {
- if(pds->mssv == mssv1) {
- svmoi1->psvtieptheo = pds->psvtieptheo;
- pds->psvtieptheo = svmoi1;
- break;
- }
- pds = pds->psvtieptheo;
- }
- }
- sv xoaSinhVien(struct DanhSachSinhVien* ds, int mssv) {
- sv *pds = ds->head;
- while(pds != NULL) {
- if(ds->head->mssv == mssv) {
- ds->head = pds->psvtieptheo;
- free(pds);
- break;
- }
- if(pds->psvtieptheo->mssv == mssv) {
- sv *p = pds->psvtieptheo;
- pds->psvtieptheo = pds->psvtieptheo->psvtieptheo;
- free(p);
- break;
- }
- pds = pds->psvtieptheo;
- }
- }
- int main() {
- int mssv1 = 91011;
- int mssv2 = 111213;
- int mssv = 789;
- danhsach ds;
- danhsach *p2 = &ds;
- sv *sv1 = (sv*)malloc(sizeof(sv));
- ds.head = sv1;
- ds.head->mssv = 123;
- ds.head->psvtieptheo = NULL;
- sv *p = (sv*)malloc(sizeof(sv));
- p = ds.head;
- sv *sv2 = (sv*)malloc(sizeof(sv));
- sv2->mssv = 345;
- sv2->psvtieptheo = NULL;
- sv1->psvtieptheo = sv2;
- sv *sv3 = (sv*)malloc(sizeof(sv));
- sv3->mssv = 567;
- sv3->psvtieptheo = NULL;
- sv2->psvtieptheo = sv3;
- sv *sv4 = (sv*)malloc(sizeof(sv));
- sv4->mssv = mssv1;
- sv3->psvtieptheo = sv4;
- sv4->psvtieptheo = NULL;
- themSinhVien2(p2, mssv1, mssv2);
- themSinhVien1(p2, mssv);
- xoaSinhVien(p2, 111213);
- inDanhSachSV(ds);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement