用链表做学生信息管理出现问题
#include <stdio.h>#include <stdlib.h>
#define LEN sizeof(struct Student)
typedef struct Student
{
int num;
char name;
int score;
struct Student *next;
} DATE;
int n;
DATE *creat() /*链表的构建*/
{
DATE *head;
DATE *p1,*p2;
p1=p2=(DATE *)malloc(LEN);
printf("请输入学生学号 姓名 成绩(按0完成输入)\n");
scanf("%d %s %d",&p1->num,&p1->name,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else
{
p2=p1->next;
p2=p1;
p1=(DATE*)malloc(LEN);
scanf("%d %s %d",&p1->num,&p1->name,&p1->score);
}
}
p2->next=NULL;
return (head);
}
FILE *fp;
void save(DATE *head) /*信息的保存*/
{
DATE *p;
if((fp=fopen("caonima.dat","w+"))==NULL)
{
printf("打开写入文件出错\n");
fclose(fp);
}
while(p!=NULL)
{
fp=fopen("cnm.dat","w+");
fprint(fp,"%d %s %d",p->num,p->name,p->score);
p=p->next;
}
fclose(fp);
}
seek(DATE *head)
{
DATE *p;
int n,x;
char str;
p=head;
printf("请选择查找方式:1 or 2(学号 or 姓名):");
scanf("%d",&n);
switch(n)
{
case 1:
printf("请输入学号:");
scanf("%d",&x);
while(p!=NULL)
{
if(x==p->num)
{
printf("%d %s %d",p->num,p->name,p->score);
}
else
{
printf("请重新输入\n");
system("cls");
}
p=p->next;
}break;
case 2:
printf("请输入姓名:");
gets(str);
while(p!=NULL)
{
if(str==p->name)
{
printf("%d %s %d",p->num,p->name,p->score);
}
else
{
printf("请重新输入:");
system("cls");
}
}break;
}
}
choose(DATE*head)
{
DATE *p,*t;
int w;
for(p=head;p!=NULL;p=p->next)
{
for(t=p->next;t!=NULL;t=t->next)
{
if(p->score<t->score)
{
w=t->score;
t->score=p->score;
p->score=w;
}
}
}
}
print(DATE *head)
{
DATE *p;
p=head;
while(p!=NULL)
{
printf("%d %s %d",p->num,p->name,p->score);
p=p->next;
}
}
menu()
{
DATE *pt,head;
int n;
char ch;
printf("************1:录入学生信息\n");
printf("************2:查找学生信息\n");
printf("************3:输出学生信息\n");
printf("***********谢谢使用*******\n");
scanf("%d",&n);
system("cls");
switch(n)
{
case 1:
pt=creat();
save(pt);
break;
case 2:
seek(head);
break;
case 3:
pt=creat(); 这里怎么让choose这个函数既能引用creat函数的指针,又不让他去运行creat函数去输入数据;
choose(pt);
print(pt);
break; 求大神指点!!!!!!!!!!!!!!!!!!!!!!!
}
system("cls");
getch();
menu();
}
int main()
{
menu();
return 0;