zoukankan      html  css  js  c++  java
  • Hello world!(内含自己编写的C语言二叉树同学录)

        修改:刷了一段时间的题,水平渐涨,发现同学录真的要做成市面可行的应用的话,应该按学号建立二叉平衡树,红黑树是一个可行的选择。

        在同学的推荐下,来到博客园来找志同道合的人交流代码。3个月后参加蓝桥杯比赛,报名的是C语言组,所以接下来3个月我会在此发布刷题时的心得与原创代码。

      以下代码是我的课程设计自己花12小时编写的二叉树同学录。

      反思一下自己的代码,删除节点用了取巧的办法,并没有完全删除节点内信息,仅仅是将学号数组第一个字符设为空,没有释放节点与接下来的连接节点。

      为什么用二叉树,并没有掌握精髓,应该是要用二叉排序树按学号排序,这样搜索起来就快了。不过因为仅仅是课程设计,也没心思真的去做一个很完美的程序。

      另外代码中有一段重复使用率比较高,也没去单独编一个函数,没有使用用C语言中字符串相关的函数,初学者也能弄懂。有兴趣的同学可以交流一下。

      最后庆祝一下!第一次开通博客园账户,第一次写博客,感觉还是挺新奇的哈哈。

      1 #include<stdio.h>
      2 #include<stdlib.h>
      3 typedef struct Student_node
      4 {
      5     char number[20];
      6     char name[20];
      7     char sex[20];
      8     char telephone[20];
      9     char interest[100];
     10     char favourite_sentence[1000];
     11     struct Student_node *left;
     12     struct Student_node *right;
     13 }student_node,*student;
     14 int n,k,flagf=1,flagd=1,flaga=1,c=0,insert;
     15 char findnumber[20];
     16 void init(student *stu)
     17 {
     18     *stu=NULL;
     19 }
     20 int make_student(student *stu)
     21 {
     22     int i;
     23     char t;
     24     if(k==0)
     25     {
     26         *stu=NULL;
     27         return 0;
     28     }
     29     printf("当前节点有值则输入1,无值则输入0
    ");
     30     scanf("
    %c",&t);
     31     if(t=='0')
     32         *stu=NULL;
     33     else if(t=='1')
     34     {
     35         *stu=(student)malloc(sizeof(student_node));
     36         printf("输入学号
    ");
     37         scanf("
    %c",&t);
     38         (*stu)->number[0]=t;
     39         for(i=1;t!='
    ';i++)
     40         {
     41             scanf("%c",&t);
     42             (*stu)->number[i]=t;
     43         }
     44         t='';
     45         printf("输入名字
    ");
     46         for(i=0;t!='
    ';i++)
     47         {
     48             scanf("%c",&t);
     49             (*stu)->name[i]=t;
     50         }
     51         t='';
     52         printf("输入性别
    ");
     53         for(i=0;t!='
    ';i++)
     54         {
     55             scanf("%c",&t);
     56             (*stu)->sex[i]=t;
     57         }
     58         t='';
     59         printf("输入电话
    ");
     60         for(i=0;t!='
    ';i++)
     61         {
     62             scanf("%c",&t);
     63             (*stu)->telephone[i]=t;
     64         }
     65         t='';
     66         printf("输入兴趣、爱好
    ");
     67         for(i=0;t!='
    ';i++)
     68         {
     69             scanf("%c",&t);
     70             (*stu)->interest[i]=t;
     71         }
     72         t='';
     73         printf("输入座右铭
    ");
     74         for(i=0;t!='
    ';i++)
     75         {
     76             scanf("%c",&t);
     77             (*stu)->favourite_sentence[i]=t;
     78         }
     79         t='';
     80         k--;
     81         make_student(&((*stu)->left));
     82         make_student(&((*stu)->right));
     83     }
     84     return 0;
     85 }
     86 int show_student(student stu)
     87 {
     88     int i;
     89     if(k==0)
     90         return 0;
     91     if(stu!=NULL)
     92     {
     93         if(stu->number[0]!='')
     94         {
     95             for(i=0;stu->number[i]!='
    ';i++)
     96                 printf("%c",stu->number[i]);
     97             printf("
    ");
     98             for(i=0;stu->name[i]!='
    ';i++)
     99                 printf("%c",stu->name[i]);
    100             printf("
    ");
    101             for(i=0;stu->sex[i]!='
    ';i++)
    102                 printf("%c",stu->sex[i]);
    103             printf("
    ");
    104             for(i=0;stu->telephone[i]!='
    ';i++)
    105                 printf("%c",stu->telephone[i]);
    106             printf("
    ");
    107             for(i=0;stu->interest[i]!='
    ';i++)
    108                 printf("%c",stu->interest[i]);
    109             printf("
    ");
    110             for(i=0;stu->favourite_sentence[i]!='
    ';i++)
    111                 printf("%c",stu->favourite_sentence[i]);
    112             printf("
    ");
    113             printf("-----------------
    ");
    114         }
    115         k--;
    116         show_student(stu->left);
    117         show_student(stu->right);
    118     }
    119     return 0;
    120 }
    121 int find(student stu)
    122 {
    123     int i,flag=0;
    124     if(k==0)
    125         return 0;
    126     if(stu!=NULL)
    127     {
    128         k--;
    129         for(i=0;stu->number[i]!='
    '||findnumber[i]!='
    ';i++)
    130         {
    131             if(findnumber[i]!=stu->number[i])
    132             {
    133                 flag=1;
    134                 break;
    135             }
    136         }
    137         if(!flag&&stu->number[i]==findnumber[i])
    138         {
    139             flagf=0;
    140             for(i=0;stu->number[i]!='
    ';i++)
    141                 printf("%c",stu->number[i]);
    142             printf("
    ");
    143             for(i=0;stu->name[i]!='
    ';i++)
    144                 printf("%c",stu->name[i]);
    145             printf("
    ");
    146             for(i=0;stu->sex[i]!='
    ';i++)
    147                 printf("%c",stu->sex[i]);
    148             printf("
    ");
    149             for(i=0;stu->telephone[i]!='
    ';i++)
    150                 printf("%c",stu->telephone[i]);
    151             printf("
    ");
    152             for(i=0;stu->interest[i]!='
    ';i++)
    153                 printf("%c",stu->interest[i]);
    154             printf("
    ");
    155             for(i=0;stu->favourite_sentence[i]!='
    ';i++)
    156                 printf("%c",stu->favourite_sentence[i]);
    157             printf("
    ");
    158             printf("-----------------
    ");
    159             return 0;
    160         }
    161         else
    162         {
    163             find(stu->left);
    164             find(stu->right);
    165         }
    166     }
    167     return 0;
    168 }
    169 int delete_student(student *stu)
    170 {
    171     int i,flag=0;
    172     if(k==0)
    173         return 0;
    174     if((*stu)!=NULL)
    175     {
    176         k--;
    177         for(i=0;(*stu)->number[i]!='
    '||findnumber[i]!='
    ';i++)
    178         {
    179             if(findnumber[i]!=(*stu)->number[i])
    180             {
    181                 flag=1;
    182                 break;
    183             }
    184         }
    185         if(!flag&&(*stu)->number[i]==findnumber[i])
    186         {
    187             flagd=0;
    188             (*stu)->number[0]='';
    189             return 0;
    190         }
    191         else
    192         {
    193             delete_student(&((*stu)->left));
    194             delete_student(&((*stu)->right));
    195         }
    196     }
    197     return 0;
    198 }
    199 int add_student(student *stu)
    200 {
    201     int i;
    202     char t;
    203     if(k==0)
    204         return 0;
    205     if((*stu)!=NULL)
    206     {
    207         if((*stu)->number[0]=='')
    208         {
    209             flaga=0;
    210             printf("输入学号
    ");
    211             scanf("
    %c",&t);
    212             (*stu)->number[0]=t;
    213             for(i=1;t!='
    ';i++)
    214             {
    215                 scanf("%c",&t);
    216                 (*stu)->number[i]=t;
    217             }
    218             t='';
    219             printf("输入名字
    ");
    220             for(i=0;t!='
    ';i++)
    221             {
    222                 scanf("%c",&t);
    223                 (*stu)->name[i]=t;
    224             }
    225             t='';
    226             printf("输入性别
    ");
    227             for(i=0;t!='
    ';i++)
    228             {
    229                 scanf("%c",&t);
    230                 (*stu)->sex[i]=t;
    231             }
    232             t='';
    233             printf("输入电话
    ");
    234             for(i=0;t!='
    ';i++)
    235             {
    236                 scanf("%c",&t);
    237                 (*stu)->telephone[i]=t;
    238             }
    239             t='';
    240             printf("输入兴趣、爱好
    ");
    241             for(i=0;t!='
    ';i++)
    242             {
    243                 scanf("%c",&t);
    244                 (*stu)->interest[i]=t;
    245             }
    246             t='';
    247             printf("输入座右铭
    ");
    248             for(i=0;t!='
    ';i++)
    249             {
    250                 scanf("%c",&t);
    251                 (*stu)->favourite_sentence[i]=t;
    252             }
    253             t='';
    254             return 0;
    255         }
    256         else
    257         {
    258             add_student(&((*stu)->left));
    259             add_student(&((*stu)->right));
    260         }
    261     }
    262     return 0;
    263 }
    264 int can_insert(student stu)
    265 {
    266     if(stu!=NULL)
    267     {
    268         c++;
    269         if(stu->left==NULL||stu->right==NULL)
    270         {
    271             printf("%d ",c);
    272             can_insert(stu->left);
    273             can_insert(stu->right);        
    274         }
    275         else
    276         {
    277             can_insert(stu->left);
    278             can_insert(stu->right);
    279         }
    280     }
    281     return 0;
    282 }
    283 void creat(student *stu)
    284 {
    285     int i;
    286     char t;
    287     *stu=(student)malloc(sizeof(student_node));
    288     n++;
    289     printf("输入学号
    ");
    290     scanf("
    %c",&t);
    291     (*stu)->number[0]=t;
    292     for(i=1;t!='
    ';i++)
    293     {
    294         scanf("%c",&t);
    295         (*stu)->number[i]=t;
    296     }
    297     t='';
    298     printf("输入名字
    ");
    299     for(i=0;t!='
    ';i++)
    300     {
    301         scanf("%c",&t);
    302         (*stu)->name[i]=t;
    303     }
    304     t='';
    305     printf("输入性别
    ");
    306     for(i=0;t!='
    ';i++)
    307     {
    308         scanf("%c",&t);
    309         (*stu)->sex[i]=t;
    310     }
    311     t='';
    312     printf("输入电话
    ");
    313     for(i=0;t!='
    ';i++)
    314     {
    315         scanf("%c",&t);
    316         (*stu)->telephone[i]=t;
    317     }
    318     t='';
    319     printf("输入兴趣、爱好
    ");
    320     for(i=0;t!='
    ';i++)
    321     {
    322         scanf("%c",&t);
    323         (*stu)->interest[i]=t;
    324     }
    325     t='';
    326     printf("输入座右铭
    ");
    327     for(i=0;t!='
    ';i++)
    328     {
    329         scanf("%c",&t);
    330         (*stu)->favourite_sentence[i]=t;
    331     }
    332     t='';
    333     init(&((*stu)->left));
    334     init(&(*stu)->right);
    335 }
    336 int creat_student(student *stu)
    337 {
    338     int x;
    339     if(stu!=NULL)
    340     {
    341         c++;
    342         if(c==insert)
    343         {
    344             if((*stu)->left==NULL&&(*stu)->right==NULL)
    345             {
    346                 printf("插入左节点请输入0,插入右节点请输入1
    ");
    347                 scanf("%d",&x);
    348                 if(!x)
    349                 {
    350                     creat(&((*stu)->left));
    351                     return 0;
    352                 }
    353                 else
    354                 {
    355                     creat(&((*stu)->right));
    356                     return 0;
    357                 }
    358             }
    359             else if((*stu)->left==NULL)
    360             {
    361                 printf("插入左节点
    ");
    362                 creat(&((*stu)->left));
    363                 return 0;
    364             }
    365             else if((*stu)->right==NULL)
    366             {
    367                 printf("插入右节点
    ");
    368                 creat(&((*stu)->right));
    369                 return 0;
    370             }
    371         }
    372         else
    373         {
    374             if((*stu)->left!=NULL)
    375             creat_student(&((*stu)->left));
    376             if((*stu)->right!=NULL)
    377             creat_student(&((*stu)->right));
    378         }
    379     }
    380     return 0;
    381 }
    382 int main()
    383 {
    384     char x;
    385     int i,key=0;
    386     student stu;
    387     printf("***************************创建同学录***************************
    ");
    388     printf("学生个数
    ");
    389     scanf("%d",&n);
    390     k=n;
    391     make_student(&stu);
    392     system("pause");
    393     while(key!=5)
    394     {
    395         system("cls");
    396         printf("|***********************************************************|
    ");
    397         printf("|**************************同学录***************************|
    ");
    398         printf("|***********************************************************|
    
    
    ");
    399         printf("1、遍历输出  2、查找  3、删除  4、增加  5、退出程序
    ");
    400         scanf("%d",&key);
    401         if(key==1)
    402         {
    403             k=n;
    404             printf("学号、姓名、性别、电话、爱好、座右铭
    -----------------
    ");
    405             show_student(stu);
    406         }
    407         if(key==2)
    408         {
    409             printf("请输入要查找的学生的学号
    ");
    410             k=n;
    411             scanf("
    %c",&x);
    412             findnumber[0]=x;
    413             for(i=1;x!='
    ';i++)
    414             {
    415                 scanf("%c",&x);
    416                 findnumber[i]=x;
    417             }
    418             printf("学号、姓名、性别、电话、爱好、座右铭
    -----------------
    ");
    419             find(stu);
    420             if(flagf)
    421                 printf("查无此人!
    ");
    422             flagf=1;
    423         }
    424         if(key==3)
    425         {
    426             printf("请输入要删除的学生的学号
    ");
    427             k=n;
    428             scanf("
    %c",&x);
    429             findnumber[0]=x;
    430             for(i=1;x!='
    ';i++)
    431             {
    432                 scanf("%c",&x);
    433                 findnumber[i]=x;
    434             }
    435             delete_student(&stu);
    436             if(flagd)
    437             printf("输入学号不正确,删除失败!
    ");
    438             else
    439             printf("删除成功!
    ");
    440             flagd=1;
    441         }
    442         if(key==4)
    443         {
    444             k=n;
    445             add_student(&stu);
    446             if(flaga)
    447             {
    448                 c=0;
    449                 printf("以下节点可供插入
    ");
    450                 can_insert(stu);
    451                 printf("
    ");
    452                 printf("插入到哪个节点之下?
    ");
    453                 scanf("%d",&insert);
    454                 c=0;
    455                 creat_student(&stu);
    456             }
    457             flaga=1;
    458         }
    459         if(key==5)
    460         {
    461             exit(0);
    462         }
    463         system("pause");
    464     }    
    465     return 0;
    466 }
  • 相关阅读:
    一些PC小软件/工具/神器备份
    三角函数与反三角函数
    常用网站整理(书签整理)
    谷歌和谷歌学术镜像网站
    微擎系统jssdk系统快速签名变量
    phpexcel 导入超过26列时的解决方案
    js循环对象,(多层数组)
    CentOS 6 下无法wget https链接的解决方法
    centos6 7 yum安装mongodb 3.6
    yum except KeyboardInterrupt, e: 错误
  • 原文地址:https://www.cnblogs.com/search-the-universe/p/search-the-universe-0.html
Copyright © 2011-2022 走看看