Đăng Nhập

Vui lòng khai báo chính xác tên truy cập và mật khẩu!

Quên mật khẩu?

Đăng Ký

Bạn phải điền đầy đủ thông tin đăng ký!

  

[CTDL VÀ GT] Danh sách liên kết đơn gần đầy đủ

    Phát triển viên

    Bài viết Bài viết : 8

    Danh vọng Danh vọng : 35

    Uy tín Uy tín : 15

    Huy hiệu : hulk,like,goodMod,mod

    #1

     on Mon May 18, 2015 11:04 pm 

    code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<time.h>
    #include<string.h>

    struct sv
    {
     char masv[20],ten[20];
     float diem_tb;
    };

    struct node
    {
     sv data;
     node* next;
    };

    node* tao_node(sv x)
    {
     node* s;
     s = new node;
     s ->data = x;
     s ->next = NULL;
     return s;
    };

    sv infor()
    {
     sv x;
     printf("----------------[Nhap thong tin]----------------\n");
     fflush(stdin);
     printf(" + Nhap ma sinh vien :");gets(x.masv);
     fflush(stdin);
     printf(" + Nhap ten :");gets(x.ten);
     fflush(stdin);
     do
     {
     fflush(stdin);
     printf(" + Nhap diem trung binh:");scanf("%f",&x.diem_tb);
     if(x.diem_tb < 0 || x.diem_tb >10)
     {
     printf("Loi nhap lai !!\n");
     }else return x;
     }while(x.diem_tb >= 0 || x.diem_tb <=10);
     printf("------------------------------------------------\n");
     return x;
    };

    void intput(node* &list,sv x)
    {
     node *p =tao_node(x);
     //ADD
     node* temp=list;
     if(temp == NULL)list = p;//Head
     else
     {
     while(temp->next != NULL)temp=temp->next;
     temp->next = p;
     temp = p;
     };//Tail
     return;
    };

    void output(node* list)
    {
     system("cls");
     if(list == NULL)return;
     node* r = list;int i=1;
     while(r != NULL)
     {
     if(r == NULL)return;
     printf(" **[ - Thong tin sinh vien [%d] - ]**\n",i);i++;
     printf(" * + Ma sinh vien : %s               *\n",r->data.masv);
     printf(" * + Ten : %s                        *\n",r->data.ten);
     printf(" * + Diem trung binh : %.1f         *\n",r->data.diem_tb);
     printf(" ***********************************\n");
     r = r->next;
     };
     return;
    };

    void NUK24(node* &list)
    {
     list = NULL;
     return;
    };



    int lenl(node* list)
    {
     int i=0;
     node* r = list;
     while(r != NULL)
     {
     i++;
     r=r->next;
     };
     return i;
    };

    void add_head(node* &list,sv x)
    {
     node* r = list;
     node* add=tao_node(x);
     add->next = list;
     list = add;
     return;
    };
    void add_last(node* &list,sv x)
    {
     node* r = list;
     node* add=tao_node(x);
     
     if(list == NULL)add_head(list,x);
     else
     {
     while(r->next != NULL)
     {
     r = r ->next;
     };
     add->next = r->next;
     r->next = add;
     };
     return;
    };

    void ssds(node* list,node*&list_sx,int azza)//1 a-z 2 z-a ()
    {
     NUK24(list_sx);
     node* r = list;
     
     //cpy_l to x
     int i=0;sv x[lenl(list)];
     while(r != NULL)
     {
     x[i] = r->data;i++;
     r = r->next;
     };
     
     //sx
     sv tempx;
     for(int a = 0; a<lenl(list);a++)
     for(int b = a+1; b<lenl(list);b++)
     if(x[a].diem_tb > x[b].diem_tb)
     {
     tempx = x[a];
     x[a] = x[b];
     x[b] = tempx;
     };
     
     //x->sx
     if(azza == 1)
     {
     for(int add = 0; add<lenl(list); add++)
     add_head(list_sx,x[add]);
     }else
     {
     for(int add = 0; add<lenl(list); add++)
     add_last(list_sx,x[add]);
     };
     return;
    };
    void auto_in(node *&list)
    {
     srand(time(NULL));
     for(int i=1;i<5;i++)
     {
     sv x;
     x.diem_tb = rand()%11;
     node* p = new node;
     p->data = x;
     p->next = NULL;
     //ADD
     node* te=list;
     if(te == NULL)list = p;//Head
     else
     {
     while(te->next != NULL)te=te->next;
     te->next = p;
     te = p;
     };//tail
     };
     printf(" Creat complete !!!\n");return;
    };

    void add_k(node* &list)
    {
     int n;fflush(stdin);
     printf(" Nhap vi tri can chen : ");scanf("%d",&n);
     if(n == 1)
     {
     add_head(list,infor());
     return;
     }
     if(n == lenl(list)+1)
     {
     add_last(list,infor());return;
     }
     if(n >1 || n<lenl(list))
     {
     node * l=list,*add=tao_node(infor());
     int i = 1;
     while(i < n-1)
     {
     printf(" %d-----",i);
     l=l->next;
     i++;
     };
     add->next = l->next;
     l->next = add;
     return;
     };
    };

    node* update_node(node* &list)
    {
     node* L=list;
     int  i=1,n;
     
     fflush(stdin);
     printf(" Nhap vi tri can cap nhat : ");scanf("%d",&n);
     
     if(n<1||n>lenl(list))printf(" Error!!.\n");
     else
     {
     while(i != n)
     {
     L = L->next;
     i++;
     };
     printf("----------------[Update in %d]----------------\n",n);
     fflush(stdin);
     printf(" + Nhap ma sinh vien :");gets(L->data.masv);
     fflush(stdin);
     printf(" + Nhap ten :");gets(L->data.ten);
     fflush(stdin);
     fflush(stdin);
     printf(" + Nhap diem trung binh:");scanf("%f",&L->data.diem_tb);
     printf("------------------------------------------------\n");
     };
     
    };

    void del_k(node* &list)
    {
     node* p = list ;
     int k;
     
     printf("Nhap sinh vien can xoa :");
     fflush(stdin);
     scanf("%d",&k);
     if(k<0 || k>lenl(list))return;
     

     if(k == 1)
     {
     list = list->next;
     return;
     };
     if(k == lenl(list))
     {
     int i = 1;
     while(i < k-1)
     {
     p = p->next;
     i++;
     };
     p->next = NULL;
     return;
     };
     int i = k-1;
     while(i > 1)
     {
     printf(" %d =",i);
     p = p->next;
     i--;
     };
     p->next = p->next->next;
     return;
    };

    void list_work()//printf(".\n");
    {
     printf(" ----------------[ Program ]----------------\n");
     printf(" - 1. Nhap danh sach sinh vien.            -\n");
     printf(" - 2. Xem danh sach sinh vien.             -\n");
     printf(" ___________________________________________\n");
     printf(" - 3. Sap xep danh sach diem Thap->Cao.    -\n");
     printf(" - 4. Sap xep danh sach diem Cao->Thap.    -\n");
     printf(" ___________________________________________\n");
     printf(" - 5. Chen sinh vien abc vao vi tri k.     -\n");
     printf(" - 6. Xoa sv tai vi tri k.                 -\n");
     printf(" - 7. Cap nhat du lieu sv tai vi tri k.    -\n");
     printf(" ___________________________________________\n");
     printf(" - 8. Tim sinh vien diem cao nhat.         -\n");
     printf(" - 9. Tim 3 sinh vien diem ]THAP[ nhat.    -\n");
     printf(" - 10.Tim 3 sinh vien diem [CAO] nhat.     -\n");
     printf(" - 11.Tim sinh vien theo ten.              -\n");
     printf(" ----------------===========----------------\n");
     printf("\n");
    };
    int F_XX(char *c1,char *c2)
    {
     int a = strlen(c1);
     int a2 = strlen(c2);
     int dk=1;
     if(a == a2)
     {
     for(int i=0;i<=a;i++)
     if(c1[i] == c2[i])dk=1;
     else return 0;
     }else return 0;
     return dk;
    };
    void find_name(node * list)
    {
     
     node* r = list;
     char *x;
     fflush(stdin);
     printf(" Nhap ten sinh vien can tim : ");
     gets(x);
     
     int i = 0;sv kk;
     while(r != NULL)
     {
     if(F_XX(x,r->data.ten) == 1)
     {
     kk=r->data;
     printf(" **[ - Sinh vien can tim - ]**\n");
     printf(" * + Ma sinh vien : %s        *\n",kk.masv);
     printf(" * + Ten : %s                 *\n",kk.ten);
     printf(" * + Diem trung binh : %.1f   *\n",kk.diem_tb);
     printf(" *****************************\n");
     };
     r = r->next;
     i++;
     };
     
    };

    void run_work(node*& list,node*& k24)
    {
     int dk;
     int chon;
     int run=0,run2=0;
     while(dk != 0)
     {
     printf("    Chon :");scanf("%d",&chon);
     switch(chon)
     {
     case 0:
     {
     auto_in(list);
     printf(" Danh sach co %d sinh vien.\n",lenl(list));
     break;
     };
     case 1:
     {
     system("color 0e");
     system("cls");
     char c;
     do
     {
     intput(list,infor());
     printf("Ban muon tiep tuc nhap danh sach (y/n)? : ");
     scanf("%s",&c);
     }while(c != 'n');
     
     printf("\n");
     system("color 0f");
     printf(" Danh sach co %d sinh vien.\n",lenl(list));
     break;
     };
     case 2:
     {
     output(list);
     printf(" Danh sach co %d sinh vien.\n",lenl(list));
     break;
     };
     case 3:
     {
     ssds(list,k24,2);
     output(k24);
     printf(" Danh sach co %d sinh vien.\n",lenl(k24));
     break;
     };
     case 4:
     {
     ssds(list,k24,1);
     output(k24);
     printf(" Danh sach co %d sinh vien.\n",lenl(k24));
     break;
     };
     case 5:
     {
     printf(" Danh sach co %d sinh vien.\n",lenl(list));
     add_k(list);output(list);
     printf(" Danh sach co %d sinh vien.\n",lenl(list));
     break;
     };
     case 6:
     {
     output(list);
     printf(" Danh sach co %d sinh vien.\n",lenl(list));
     del_k(list);
     output(list);
     printf(" Danh sach co %d sinh vien.\n",lenl(list));
     break;
     };
     case 7:
     {
     output(list);
     update_node(list);output(list);
     printf(" Danh sach co %d sinh vien.\n",lenl(list));
     break;
     };
     case 8:
     {
     ssds(list,k24,1);
     node* r =k24;
     int i=1;
     printf(" ***********[ - Top [%d] - ]***********\n",i);
     printf(" * + Ma sinh vien : %s                *\n",r->data.masv);
     printf(" * + Ten : %s                         *\n",r->data.ten);
     printf(" * + Diem trung binh : %.1f           *\n",r->data.diem_tb);
     printf(" **************************************\n",i);
     printf(" Danh sach co %d sinh vien.\n",lenl(list));
     break;
     };
     case 9:
     {
     ssds(list,k24,2);
     node* r =k24;
     if(lenl(k24)<3)output(k24);
     else
     for(int i = 1 ; i<= 3 ;i++)
     {
     printf(" ***********[ - Bad [%d] - ]***********\n",i);
     printf(" * + Ma sinh vien : %s                *\n",r->data.masv);
     printf(" * + Ten : %s                         *\n",r->data.ten);
     printf(" * + Diem trung binh : %.1f           *\n",r->data.diem_tb);
     printf(" **************************************\n",i);
     r = r->next;
     };
     printf(" Danh sach co %d sinh vien.\n",lenl(list));
     break;
     };
     case 10:
     {
     ssds(list,k24,1);
     node* r =k24;
     if(lenl(k24)<3)output(k24);
     else
     for(int i = 1 ; i<= 3;i++)
     {
     printf(" ***********[ - Top [%d] - ]***********\n",i);
     printf(" * + Ma sinh vien : %s                *\n",r->data.masv);
     printf(" * + Ten : %s                         *\n",r->data.ten);
     printf(" * + Diem trung binh : %.1f          *\n",r->data.diem_tb);
     printf(" **************************************\n",i);
     r = r->next;
     };
     printf(" Danh sach co %d sinh vien.\n",lenl(list));
     break;
     };
     case 11:
     {
     find_name(list);
     break;
     };
     default :
     {
     
     printf(" So ban nhap khong ton tai !!!\n");
     break;
     };
     };//switch
     
     printf("Ban muon tiep tuc program ? (0 = Thoat & 1 = Tiep tuc)\n ->");
     scanf("%d",&dk);
     if(dk != 0)
     {
     system("cls");
     system("color 0f");
     list_work();
     };
     
     };//while
    };

      main(void)
    {
     system("color 0f");
     node* list;
     NUK24(list);
     node* list_sx;
     NUK24(list_sx);
     node* list_sx2;NUK24(list_sx2);
     list_work();
     run_work(list,list_sx);
    };



    [You must be registered and logged in to see this link.] [You must be registered and logged in to see this link.]
    Bạn không có quyền trả lời bài viết