zoukankan      html  css  js  c++  java
  • 链表

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    static struct stuNode *head=NULL;
    struct stuNode {
     char  num[16];
     char  name[8];
     char  age[4];
     struct stuNode *next;
    };

    struct stuNode *CreateList( int n ) /* 1.创建链表 */
    {
     struct stuNode *pb, *pf;
     int  i;
     int count=1;
     for ( i = 0; i < n; i++ )
     {  
      pb = (struct stuNode *) malloc( sizeof(struct stuNode) );
      fflush(stdin);//记得清空缓存区,这个很重要
      printf( "请输入第%d个学生的学号: ",count );
      gets(pb->num);
      printf( "请输入学生的姓名: " );
      gets(pb->name);
      printf( "请输入学生的年龄: " );
         gets(pb->age);
      count++;
      pb->next = NULL;
      if ( i == 0 )
       head = pb;
      else
       pf->next = pb;
      pf = pb;
     }
     printf( "表已创建完毕 " );
     return(head);
    }


    void TraversalList( struct stuNode *h ) /* 2.遍历链表 */
    {
     struct stuNode *p;
     p = h;
     if ( p == NULL )
      printf( "表为空 " );
     else{
      printf( "学号 姓名 年龄 " );
      while ( p )
      {
       printf( "%s %s %s ", p->num, p->name, p->age );
       p = p->next;
      }
     }
    }
     

    struct stuNode * InsertList(struct stuNode *h,char *pnum){//3.插入学生

      struct stuNode *pa,*pb;

      pa=h;
      if(!strcmp(pa->num,pnum)){
      pb=(struct stuNode *)malloc(sizeof(struct stuNode));
      fflush(stdin);
      printf("请输入待插入学生的信息: ");
      printf("请输入学生的学号: ");
      gets(pb->num);
      printf("请输入学生的姓名: ");
      gets(pb->name);
      printf("请输入学生的年龄: ");
      scanf("%d",&(pb->age));
      printf("插入学生信息完成");
      pb->next=pa;
      return pb;
      }
      else{
     

     while(pa->next!=NULL){


       if(!strcmp(pa->next->num,pnum)){
       pb=(struct stuNode *)malloc(sizeof(struct stuNode));
       fflush(stdin);
       printf("请输入待插入学生的信息 ");
       printf("请输入学生的学号: ");
       gets(pb->num);
      printf("请输入学生的姓名: ");
       gets(pb->name);
       printf("请输入学生的年龄: ");
       scanf("%s",pb->age);
       printf("插入学生信息完成 ");
       pb->next=pa->next;
       pa->next=pb;
       return h;
       }
       else
       pa=pa->next;
      }
      printf("未找到插入位置: ");
      return h;
     

      }
      }


     struct stuNode *DeleteList(struct stuNode *h,char *pnum){//4.删除学生


      struct stuNode * pa,*pb;
      pa=h;
      if(!strcmp(pa->num,pnum)){//删除的是第一个节点
         h=pa->next;
       free(pa);
       return h;
        printf("学生已删除: ");
    }

      else{
      while(pa->next!=NULL){
     
       if(strcmp(pa->next->num,pnum)){
     
       pb=pa->next;
       pa->next=pb->next;
       free(pb);
       return h;
       printf("学生已删除: ");
       }
      else
      pa=pa->next;
      }
       printf("未找到删除的学生 ");
       return h;

      }

     }

    void SearchList(struct stuNode *h,char *pname){//5.查找学生
     
      struct stuNode *pa;
      pa=h;
      while(pa!=NULL){
      if(!strcmp(pa->name,pname)){
      printf("找到了 ");
      printf("学号:%s  姓名:%s  年龄:%s ",pa->num,pa->name,pa->age);
      return ;
      }
      else
     pa=pa->next;
     }
      printf("找不到姓名为%s的学生: ",pa->name);
     
     
     }


    void DestroyList(struct stuNode **ph){//6.摧毁链表
        struct stuNode *p;
      p=*ph;
      while(p!=NULL){
     
      *ph=p->next;
       free(p);
      p=*ph;
     }
     printf("链表已经全部摧毁 ");
      *ph=NULL;
     }
     


     int main()
     {
      
      
     
      

      while ( 1 )
      {
       int  n, x;
        char z[12];
       char y[12];
       char pname[8];
       printf( "----------------欢迎来到涛帝的学生管理系统------------------- " );
       printf( "1.建立链表 " );
       printf("2.查看所有学生信息 ");
       printf( "3.插入学生 " );
       printf( "4.删除学生 " );
       printf( "5.查找学生 " );
       printf( "6.摧毁链表 " );
       printf( "请输入你要进行的操作: " );
       scanf( "%d", &n );
       switch ( n )
       {
       case 1:
        printf( "请输入你要录入的学生人数: " );
        scanf( "%d", &x );
        CreateList(x);
        break;
         case 2:
                TraversalList( head );
                   break;
             case 3:
       
         printf("请输入想要插入位置学生的学号: ");
         scanf("%s",y);
         InsertList(head,y);
         break;
         case 4:
       
         printf("请输入想要删除学生的学号: ");
                  scanf("%s",z);
           DeleteList(head,z);
         break;
        case 5:
       
                           printf("请输入想要查找学生的姓名: ");
            scanf("%s",pname);
         SearchList(head,pname);
       break;
        case 6:
       
         DestroyList(&head);
          break;
       default: printf( "老子要你输1,2,3,4,5,6 ntm瞎输什么输 " ); break;
       }
      }
      
     return 0;
     }

  • 相关阅读:
    MSIL指令速查表
    .NET中的内存管理,GC机制,内存释放过程,各种内存释放方法
    程序员学专业英语
    C#入门不简单(上网找beginning c# objects时看到的一些话,很有同感)
    关掉在打开的做法很有必要
    视图刷新
    创建 平面求交点
    3dmas 的硬显示 这个就是其中既各个 最后两个是 对应显示操作的
    这句话是我在渲染 程序贴图用到的代码
    设置到基础层
  • 原文地址:https://www.cnblogs.com/wantao/p/7828592.html
Copyright © 2011-2022 走看看