Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. typedef struct sv {
  4. int mssv;
  5. struct sv *psvtieptheo;
  6. }sv;
  7. typedef struct DanhSachSinhVien {
  8. struct sv *head;
  9. }danhsach;
  10. void inDanhSachSV(struct DanhSachSinhVien ds) {
  11. sv *p = ds.head;
  12. while(p != NULL)
  13. {
  14. printf("%d\n", p->mssv);
  15. p = p->psvtieptheo;
  16. }
  17. }
  18. sv themSinhVien1(struct DanhSachSinhVien *ds, int mssv) {
  19. sv *svmoi = (sv*)malloc(sizeof(sv));
  20. svmoi->mssv = mssv;
  21. sv *p1 = ds->head;
  22. while(p1->psvtieptheo != NULL)
  23. {
  24. p1 = p1->psvtieptheo;
  25. }
  26. p1->psvtieptheo = svmoi;
  27. svmoi->psvtieptheo = NULL;
  28. }
  29. sv themSinhVien2(struct DanhSachSinhVien *ds, int mssv1, int mssv2) {
  30. sv *svmoi1 = (sv*)malloc(sizeof(sv));
  31. svmoi1->mssv = mssv2;
  32. sv *pds = ds->head;
  33. while(pds != NULL)
  34. {
  35. if(pds->mssv == mssv1) {
  36. svmoi1->psvtieptheo = pds->psvtieptheo;
  37. pds->psvtieptheo = svmoi1;
  38. break;
  39. }
  40. pds = pds->psvtieptheo;
  41. }
  42. }
  43. sv xoaSinhVien(struct DanhSachSinhVien* ds, int mssv) {
  44. sv *pds = ds->head;
  45. while(pds != NULL) {
  46. if(ds->head->mssv == mssv) {
  47. ds->head = pds->psvtieptheo;
  48. free(pds);
  49. break;
  50. }
  51. if(pds->psvtieptheo->mssv == mssv) {
  52. sv *p = pds->psvtieptheo;
  53. pds->psvtieptheo = pds->psvtieptheo->psvtieptheo;
  54. free(p);
  55. break;
  56. }
  57. pds = pds->psvtieptheo;
  58. }
  59. }
  60. int main() {
  61. int mssv1 = 91011;
  62. int mssv2 = 111213;
  63. int mssv = 789;
  64. danhsach ds;
  65. danhsach *p2 = &ds;
  66.  
  67. sv *sv1 = (sv*)malloc(sizeof(sv));
  68. ds.head = sv1;
  69. ds.head->mssv = 123;
  70. ds.head->psvtieptheo = NULL;
  71.  
  72. sv *p = (sv*)malloc(sizeof(sv));
  73. p = ds.head;
  74.  
  75. sv *sv2 = (sv*)malloc(sizeof(sv));
  76. sv2->mssv = 345;
  77. sv2->psvtieptheo = NULL;
  78. sv1->psvtieptheo = sv2;
  79.  
  80. sv *sv3 = (sv*)malloc(sizeof(sv));
  81. sv3->mssv = 567;
  82. sv3->psvtieptheo = NULL;
  83. sv2->psvtieptheo = sv3;
  84.  
  85. sv *sv4 = (sv*)malloc(sizeof(sv));
  86. sv4->mssv = mssv1;
  87. sv3->psvtieptheo = sv4;
  88. sv4->psvtieptheo = NULL;
  89.  
  90. themSinhVien2(p2, mssv1, mssv2);
  91.  
  92. themSinhVien1(p2, mssv);
  93.  
  94. xoaSinhVien(p2, 111213);
  95.  
  96. inDanhSachSV(ds);
  97.  
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement