求大神帮忙,为什么不能显示输入的信息?
# include <stdio.h># include <malloc.h>
# include <stdlib.h>
# include <conio.h>
# include <string.h>
int linklen = 0; /* 全局变量,用于记录链表长度 */
typedef struct Node /* 学生结构体 */
{
int Num;
char name[10];
char sex[10];
int year;
float score;
struct Node *Next; /* 保存下一位学生信息 */
}Student;
/* 输入学生信息 */
Student *Input()
{
int n;
int i;
Student *stu;
Student *p;
Student *link = (Student *)malloc(sizeof(Student));
if ( !link ){ printf("Failed to init !\n"); exit(0); }
link->Next = NULL;
p = link;
printf("请输入所需要输入学生信息的学生人数:");
scanf("%d", &n);
getchar(); //吸收回车符
system("cls");
for ( i=1; i<=n; i++ )
{
stu = (Student *)malloc(sizeof(Student));
if ( !stu ){ printf("Failed to new!\n"); exit(0); }
printf("第%d位学生信息:\n", i);
printf("学号:");
scanf("%d", &stu->Num);
printf("姓名:");
scanf("%s", stu->name);
printf("性别:");
scanf("%s", stu->sex);
printf("年龄:");
scanf("%d", &stu->year);
printf("成绩:");
scanf("%f", &stu->score);
stu->Next = NULL;
p->Next = stu;
p = stu;
++linklen;
system("cls"); /* 跳到下一位学生信息 */
}
return link;
}
/* 查找学生信息 */
void Seek(Student *link)
{
int flag=0;
char name[10];
Student *stu= link->Next;
printf("请输入学生姓名:");
scanf("%s", name);
while ( stu )
{
if ( !strcmp(stu->name,name) )
{
printf("查找成功!\n");
system("cls");
printf("学号:%d\n", stu->Num);
printf("姓名:%s\n", stu->name);
printf("性别:%s\n", stu->sex);
printf("年龄:%d\n", stu->year);
printf("成绩:%f\n", stu->score);
flag = 1;
break;
}
else
stu = stu->Next;
}
if ( !flag )
printf("不存在该学生信息!\n");
}
/* 修改学生信息 */
void Change(Student *link)
{
int flag = 0;
char name[10];
Student *stu= link->Next;
printf("请输入学生姓名:");
scanf("%s", name);
while ( stu )
{
if ( !strcmp(stu->name,name) )
{
printf("查找成功!\n");
system("cls");
printf("新的学生信息:");
printf("学号:");
scanf("%d", &stu->Num);
printf("姓名:");
scanf("%s", stu->name);
printf("性别:");
scanf("%s", stu->sex);
printf("年龄:");
scanf("%d", &stu->year);
printf("成绩:");
scanf("%f", &stu->score);
printf("修改完成!\n");
flag = 1;
break;
}
else
stu = stu->Next;
}
if ( !flag )
printf("不存在该学生信息!\n");
}
/* 添加新的学生学生信息 */
void Insert(Student *link)
{
int pos, flag;
Student *p = link;/* 首位插入 */
Student *stu = link->Next;/* 中间插入 */
Student *newstu = (Student *)malloc(sizeof(Student));
if ( !newstu ){ printf("Failed to new!\n"); exit(0); }
newstu->Next = NULL;
printf("请填入新学生的信息:\n");
printf("学号:");
scanf("%d", &newstu->Num);
printf("姓名:");
scanf("%s", newstu->name);
printf("性别:");
scanf("%s", newstu->sex);
printf("年龄:");
scanf("%d", &newstu->year);
printf("成绩:");
scanf("%f", &newstu->score);
system("cls");
printf("请选择插入位置pos:");
scanf("%d", &pos);
if ( pos>linklen && pos<1 )
{
printf("越界!\n");
exit(0);
}
else if ( pos == 1 )
{
newstu->Next = p->Next;
p->Next = newstu;
printf("添加完成!\n");
}
else
{
flag = 1;/* 起始位置 */
while ( stu && flag<pos-1 )
{
stu = stu->Next;
++flag;
}
if ( !stu && flag>pos-1 )
{
printf("Data exception!\n");
exit(0);
}
newstu->Next = stu->Next;
stu->Next = newstu;
++linklen;/* 链表长度增加1 */
printf("添加完成!\n");
}
}
/* 删除学生信息 */
int Delete(Student *link)
{
int tag = 0;
int flag;
int pos;
char ch = '\0'; /* 用于选择 */
Student *stu = link->Next;
char name[10];
printf("请输入学生姓名:");
scanf("%s", name);
pos = 1; /* 用于记录所要删除学生的位置 */
while ( stu )
{
if ( !strcmp(stu->name,name) )
{
printf("查找成功!\n");
printf("学号:%d\n", stu->Num);
printf("姓名:%s\n", stu->name);
printf("性别:%s\n", stu->sex);
printf("年龄:%d\n", stu->year);
printf("成绩:%f\n", stu->score);
system("cls");
printf("您是否要删除该学生信息?(Y or N)\n");
printf(" 您的选择是:");
getchar(); /* 吸收回车符 */
scanf("%c", &ch);
tag = 1;
break;
}
else
{
stu = stu->Next;
++pos;
}
if(!tag )
{
printf("没有该学生信息!\n");
return 0;
}
if ( 'Y' == ch )
{
if ( pos == 1 )
{
Student *Q = link->Next;
link->Next = Q->Next;
free(Q);
Q = NULL;
}
else
{
Student *q = NULL;
Student *p = link->Next;
flag = 1; /* 查找结点位置 */
while ( p && flag<pos-1 )
{
p = p->Next;
++flag;
}
if ( !p && flag>pos-1 )
{
printf("Data exception!\n");
exit(0);
}
q = p->Next;
p->Next = q->Next;
free(q);
q = NULL;
printf("删除成功!\n");
}
}
}
return 1;
}
void Show(Student *link)
{
Student *stu=link->Next;
while(stu!=NULL)
{
printf("学号:%d\n", stu->Num);
printf("姓名:%s\n", stu->name);
printf("性别:%s\n", stu->sex);
printf("年龄:%d\n", stu->year);
printf("成绩:%f\n", stu->score);
stu->Next=stu;
printf("\n");
}
}
/* 主函数运行学生管理系统 */
int main(void)
{
Student *link = NULL;
int select;
int flag=0;
mark:
system("cls");
printf("\t\t\t----------------------------------\n");
printf("\t\t\t* 欢迎进入学生信息管理系统 *\n");
printf("\t\t\t----------------------------------\n");
printf("\n\n");
printf("\t\t****************************************************\n");
printf("\t\t* 1.输入(Input) *\n");
printf("\t\t* 2.查询(Search) *\n");
printf("\t\t* 3.修改(Change) *\n");
printf("\t\t* 4.插入(Insert) *\n");
printf("\t\t* 5.删除(Delete) *\n");
printf("\t\t* 6.显示(Show) *\n");
printf("\t\t* 7.退出(Exit) *\n");
printf("\t\t****************************************************\n");
printf("\t\t请输入您的选项:");
scanf("%d", &select);
system("cls");
switch ( select )
{
case 1:
link = Input();
system("pause");
goto mark;
break;
case 2:
Seek(link);
system("pause");
goto mark;
break;
case 3:
Change(link);
system("pause");
goto mark;
break;
case 4:
Insert(link);
system("pause");
goto mark;
break;
case 5:
Delete(link);
system("pause");
goto mark;
break;
case 6:
Show(link);
system("pause");
goto mark;
break;
case 7:
exit(0);
default:
printf("没有这个选项!\n");
system("pause");
goto mark;
break;
}
return 0;
}