Advertisement
nguyenvanquan7826

list_link_sv

May 14th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.07 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. typedef struct sinhvien
  6. {
  7.     char ht[50], qq[50];
  8.     float diem;
  9. }
  10. typedef struct Node
  11. {
  12.     item Data;
  13.     Node *next;
  14. };
  15.  
  16. typedef Node *List;
  17.  
  18. void Init (List &L)
  19. {
  20.     L=NULL;
  21. }
  22.  
  23. int Isempty (List L)
  24. {
  25.     return (L==NULL);
  26. }
  27.  
  28. int Len (List L)
  29. {
  30.     Node *P=L;
  31.     int i=0;
  32.     while (P!=NULL)
  33.     {
  34.         i++;
  35.         P=P->next;
  36.     }
  37.     return i;
  38. }
  39.  
  40. void Make_sv (item &sv)
  41. {
  42.     char ht[50], qq[50];
  43.     float diem;
  44.  
  45.     printf ("\tHo ten: ");
  46.     fflush(stdin);
  47.     gets(ht);
  48.     strcpy(sv.ht,ht);
  49.  
  50.     printf ("\tQue quan: ");
  51.     fflush(stdin);
  52.     gets(qq);
  53.     strcpy(sv.qq,qq);
  54.  
  55.     fflush(stdin);
  56.     printf ("\tDiem: ");
  57.     scanf("%f",&diem);
  58.     sv.diem=diem;
  59. }
  60.  
  61. void Make_Node (Node *P, item sv)
  62. {
  63.     P->next = NULL;
  64.     strcpy (P->Data.ht,sv.ht);
  65.     strcpy (P->Data.qq,sv.qq);
  66.     P->Data.diem = sv.diem;
  67. }
  68.  
  69. void Insert_first (List &L, item sv)
  70. {
  71.     Node *P;
  72.     P = (Node *) malloc (sizeof (Node));
  73.     Make_Node(P,sv);
  74.     P->next = L;
  75.     L = P;
  76. }
  77.  
  78. void Insert_k (List &L, item sv, int k)
  79. {
  80.     Node *P, *Q = L;
  81.     P = (Node *) malloc (sizeof (Node));
  82.     int i=1;
  83.     if (k<1 || k> Len(L)+1) printf("Vi tri chen khong hop le !");
  84.     else
  85.     {
  86.         Make_Node(P,sv);
  87.         if (k == 1) Insert_first(L,sv);
  88.         else
  89.         {
  90.             while (Q != NULL && i != k-1)
  91.             {
  92.                 i++;
  93.                 Q = Q->next;
  94.             }
  95.             P->next = Q->next;
  96.             Q->next = P;
  97.         }
  98.     }
  99. }
  100.  
  101. int Search_ht (List L, char ht[50])
  102. {
  103.     Node *P=L;
  104.     int i=1;
  105.     while (P != NULL && strcmp(P->Data.ht,ht))
  106.     {
  107.         P = P->next;
  108.         i++;
  109.     }
  110.     if (P != NULL) return i;
  111.     else return 0;
  112. }
  113.  
  114. int Search_qq (List L, char qq[50])
  115. {
  116.     Node *P=L;
  117.     int i=1;
  118.     while (P != NULL && strcmp(P->Data.qq,qq))
  119.     {
  120.         P = P->next;
  121.         i++;
  122.     }
  123.     if (P != NULL) return i;
  124.     else return 0;
  125. }
  126.  
  127. void Del_frist (List &L, item &sv)
  128. {
  129.     sv = L->Data;
  130.     L = L->next;
  131. }
  132.  
  133. void Del_k (List &L, item &sv, int k)
  134. {
  135.     Node *P=L;
  136.     int i=1;
  137.     if (k<1 || k>Len(L)) printf("Vi tri xoa khong hop le !");
  138.     else
  139.     {
  140.         if (k==1) Del_frist(L,sv);
  141.         else
  142.         {
  143.             while (P != NULL && i != k-1)
  144.             {
  145.                 P=P->next;
  146.                 i++;
  147.             }
  148.             P->next = P->next->next;
  149.         }
  150.     }
  151. }
  152.  
  153. void Del_sv_ht (List &L, item &sv, char ht[50])
  154. {
  155.     while (Search_ht(L,ht)) Del_k (L,sv,Search_ht(L,ht));
  156. }
  157.  
  158. void Input (List &L)
  159. {
  160.     int i=0;
  161.     char ht[50], qq[50];
  162.     float diem;
  163.     item sv;
  164.     do
  165.     {
  166.         i++;
  167.         printf ("Nhap thong tin sinh vien thu %d : \n",i);
  168.         printf ("\tHo ten: ");
  169.         fflush(stdin);
  170.         gets(ht);
  171.         if (ht[0] !='\0')
  172.         {
  173.             strcpy(sv.ht,ht);
  174.             printf ("\tQue quan: ");
  175.             fflush(stdin);
  176.             gets(qq);
  177.             strcpy(sv.qq,qq);
  178.             fflush(stdin);
  179.             printf ("\tDiem: ");
  180.             scanf("%f",&diem);
  181.             sv.diem=diem;
  182.             Insert_k(L,sv,Len(L)+1);
  183.         }
  184.  
  185.     } while(ht[0] !='\0');
  186. }
  187.  
  188. void Output (List L)
  189. {
  190.     Node *P=L;
  191.     printf ("%20s %20s %10s\n","Ho ten","Que quan","Diem");
  192.     while (P != NULL)
  193.     {
  194.         printf("%20s %20s %10.2f\n",P->Data.ht,P->Data.qq,P->Data.diem);
  195.         P = P->next;
  196.     }
  197.     printf("\n");
  198. }
  199.  
  200. int main()
  201. {
  202.     List L;
  203.     Init(L);
  204.     Input(L);
  205.     Output(L);
  206.  
  207.     int lua_chon;
  208.     printf("Moi ban chon phep toan voi DS LKD:");
  209.     printf("\n1: Kiem tra DS rong");
  210.     printf("\n2: Do dai DS");
  211.     printf("\n3: Chen sinh vien vao vi tri k trong DS");
  212.     printf("\n4: Tim sinh vien theo Ho ten");
  213.     printf("\n5: Tim sinh vien theo Que quan");
  214.     printf("\n6: Xoa sinh vien tai vi tri k");
  215.     printf("\n7: Xoa sinh vien theo Ho ten");
  216.     printf("\n8: Thoat");
  217.     do
  218.     {
  219.         printf("\nBan chon: ");
  220.         scanf("%d",&lua_chon);
  221.     switch (lua_chon)
  222.     {
  223.         case 1:
  224.         {
  225.             if (Isempty(L)) printf("DS rong !");
  226.             else printf ("DS khong rong !");
  227.             break;
  228.         }
  229.         case 2: printf ("Do dai DS la: %d.",Len(L));break;
  230.         case 3:
  231.         {
  232.             item sv;
  233.             int k;
  234.             printf ("Nhap phan tu can chen vao DS: \n");
  235.             Make_sv(sv);
  236.             printf ("Nhap vi tri can chen: ");
  237.             scanf ("%d",&k);
  238.             Insert_k (L,sv,k);
  239.             printf ("DS sau khi chen:\n");
  240.             Output(L);
  241.             break;
  242.         }
  243.         case 4:
  244.         {
  245.             char ht[50];
  246.             printf ("Moi ban nhap vao Ho ten can tim: ");
  247.             fflush(stdin);
  248.             gets(ht);
  249.             int k = Search_ht (L,ht);
  250.             if (k) printf ("Tim thay sinh vien co %s trong DS tai vi tri thu: %d",ht,k);
  251.             else printf ("Khong tim thay sinh vien co %d trong danh sach !",ht);
  252.             break;
  253.         }
  254.         case 5:
  255.         {
  256.             char qq[50];
  257.             printf ("Moi ban nhap vao Que quan can tim: ");
  258.             fflush(stdin);
  259.             gets(qq);
  260.             int k = Search_qq (L,qq);
  261.             if (k) printf ("Tim thay sinh vien co %s trong DS tai vi tri thu: %d",qq,k);
  262.             else printf ("Khong tim thay sinh vien co %d trong danh sach !",qq);
  263.             break;
  264.         }
  265.         case 6:
  266.         {
  267.             int k;
  268.             item sv;
  269.             printf ("Nhap vi tri can xoa: ");
  270.             scanf ("%d",&k);
  271.             Del_k (L,sv,k);
  272.             printf ("DS sau khi xoa:\n");
  273.             Output(L);
  274.             break;
  275.         }
  276.         case 7:
  277.         {
  278.             item sv;
  279.             char ht[50];
  280.             printf ("Nhap Ho ten can xoa: ");
  281.             fflush(stdin);
  282.             gets(ht);
  283.             Del_sv_ht (L,sv,ht);
  284.             printf ("DS sau khi xoa:\n");
  285.             Output(L);
  286.             break;
  287.         }
  288.         case 8: break;
  289.     }
  290.     }while (lua_chon !=7);
  291.     return 0;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement