#include<stdio.h>#include<stdlib.h>structNODE{char name[20];int age ;char sex ;char num[20];structNODE* next ;};structNODE*CreateLink(void);voidIntputLink(structNODE*);voidOutputLink(structNODE*);voidDestroyLink(structNODE*);intmain(){char ch ='\0';structNODE* head =NULL;/*-----------------------------创建链表------------------------------*/printf("是否创建链表(Y/N):");while(1){scanf("%c",&ch);getchar();if(('Y'== ch )||('y'== ch )){
head =CreateLink();IntputLink(head);OutputLink(head);break;}elseif(('N'== ch )||('n'== ch )){return0;}else{printf("请重新输入:\n");}}/*------------------------------销毁链表--------------------------------*/printf("是否销毁链表(Y/N):");
ch ='\0';while(1){scanf("%c",&ch);getchar();if(('Y'== ch )||('y'== ch )){DestroyLink(head);break;}elseif(('N'== ch )||('n'== ch )){break;}else{printf("请重新输入(Y/N):");}}return0;}structNODE*CreateLink(void){int i =0;int cnt =0;structNODE* head =malloc(sizeof* head );structNODE* move ;if(NULL== head ){printf("内存分配失败,程序终止!\n");exit(-1);}
move = head ;
move->next =NULL;printf("请输入学生的数量:");scanf("%d",&cnt);getchar();for( i =1; i <= cnt ; i ++){structNODE* fresh =malloc(sizeof* fresh );if(NULL== fresh ){printf("内存分配失败,程序终止!\n");exit(-1);}//结点连接三部曲
move->next = fresh ;
fresh->next =NULL;
move = fresh ;}return head ;}voidIntputLink(structNODE* head ){int i =1;structNODE* move = head->next ;while(NULL!= move ){printf("请输入第%d个学生的信息:",i);scanf("%s%d %c%s",move->name ,&move->age ,&move->sex , move->num );getchar();
move = move->next ;
i++;}return;}voidOutputLink(structNODE* head ){structNODE* move = head ;if(NULL== move ){printf("为创建链表\n");return;}if(NULL== move->next ){printf("链表为空\n");}while(NULL!= move->next ){printf("【姓名:%s,年龄:%d,性别:%c,学号:%s】->",move->next->name , move->next->age , move->next->sex , move->next->num );
move = move->next ;}printf("[^]\n");}voidDestroyLink(structNODE* head ){structNODE* save = head ;while(NULL!= head ){
save = head->next ;free(head);
head = save ;}if(NULL== head ){printf("链表以销毁!\n");}}
是否创建链表(Y/N):y
请输入学生的数量:3
请输入第1个学生的信息:欧阳丽 24 F Z1207038
请输入第2个学生的信息:冯小莉 20 F Z1207025
请输入第3个学生的信息:陈商清 23 F Z1207024
【姓名:欧阳丽,年龄:24,性别:F,学号:Z1207038】->【姓名:冯小莉,年龄:20,性别:F,学号:Z1207025】->【姓名:陈商清,年龄:23,性别:F,学号:Z1207024】->[^]
是否销毁链表(Y/N):y
链表以销毁!
Program ended with exit code:0