#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;
}