Advertisement
nguyenvanquan7826

list_link_sv

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