链表

#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个学生的学号:\n",count );
  gets(pb->num);
  printf( "请输入学生的姓名:\n" );
  gets(pb->name);
  printf( "请输入学生的年龄:\n" );
     gets(pb->age);
  count++;
  pb->next = NULL;
  if ( i == 0 )
   head = pb;
  else
   pf->next = pb;
  pf = pb;
 }
 printf( "表已创建完毕\n" );
 return(head);
}


void TraversalList( struct stuNode *h ) /* 2.遍历链表 */
{
 struct stuNode *p;
 p = h;
 if ( p == NULL )
  printf( "表为空\n" );
 else{
  printf( "学号\t\t姓名\t年龄\t\n" );
  while ( p )
  {
   printf( "%s\t%s\t%s\t\n", 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("请输入待插入学生的信息:\n");
  printf("请输入学生的学号:\n");
  gets(pb->num);
  printf("请输入学生的姓名:\n");
  gets(pb->name);
  printf("请输入学生的年龄:\n");
  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("请输入待插入学生的信息\n");
   printf("请输入学生的学号:\n");
   gets(pb->num);
  printf("请输入学生的姓名:\n");
   gets(pb->name);
   printf("请输入学生的年龄:\n");
   scanf("%s",pb->age);
   printf("插入学生信息完成\n");
   pb->next=pa->next;
   pa->next=pb;
   return h;
   }
   else
   pa=pa->next;
  }
  printf("未找到插入位置:\n");
  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("学生已删除:\n");
}

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

  }

 }

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


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


 int main()
 {
  
  
 
  

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

 

转载于:https://www.cnblogs.com/wantao/p/7828592.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值