c语言链表输入学生数据在屏幕打印,【求助】用c语言实现学生数据管理,谢谢...

这里是我写的=_=,有很多问题

#include

#include

#include

#include

#define NULL 0

#define LEN sizeof(struct student)

struct student

{

int num;

char name[20];

float score1,score2,score3;

struct student *next;

};

int n;

struct student *creat(void)

{

struct student *head;

struct student *p1,*p2;

n=0;

p1=p2=(struct student*)malloc(LEN);

scanf("%s,%ld,%f,%f,%f",p1->name,&p1->num,&p1->score1,&p1->score2,&p1->score3);

head=NULL;

while(p1->num!=0)

{

n=n+1;

if(n==1)head=p1;

else p2->next=p1;

p2=p1;

p1=(struct student*)malloc(LEN);

scanf("%s,%ld,%f,%f,%f",p1->name,&p1->num,&p1->score1,&p1->score2,&p1->score3);

printf("\n");

}

p2->next=NULL;

return (head);

}

struct student *del(struct student *head,long num)

{

struct student *p1,*p2;

if(head==NULL)

{

printf("\nlist null!\n");return head;

}

p1=head;

while(num!=p1->num&&p1->next!=NULL)

{

p2=p1;

p1=p1->next;

}

if (num==p1->num)

{

if(p1==head)

head=p1->next;

else p2->next=p1->next;

printf("delete:%d\n",num);

n=n-1;

}

else printf("%d not been found!\n",num);

return (head);

}

void print(struct student *head)

{

struct student *p;

printf("\nNow,These %d records are:\n",n);

p=head;

if(head!=NULL)

do

{

printf("%s,%ld,%f,%f,%f\n",p->name,p->num,p->score1,p->score2,p->score3);

p=p->next;

}

while(p!=NULL);

}

void Del_list(struct student *head )//delete the whole list

{

struct student *p,*L; p->next = L->next;head = L->next;

while( head != NULL )

{

p->next = head->next;

free( head );

head = p->next;

}

L->next = head;

return ;

}

struct student *insert(struct student *head,struct student *stud)

{

struct student *p0,*p1,*p2;

p1=head;

p0=stud;

if(head==NULL)

{

head=p0;p0->next=NULL;

}

else

{

while(p0->num>p1->num)

{

p2=p1;

p1=p1->next;

}

if(p0->num<=p1->num)

{

if(head==p1)head=p0;

else p2->next=p0;

p0->next=p1;

}

else

{p1->next=p0;p0->next=NULL;}

}

n=n+1;

return (head);

}

void PrintHelp()//打印选项

{

printf("1: 创建学生数据\n");

printf("2: 删除某个学生信息\n");

printf("3: 打印所有学生信息\n");

printf("4: 删除所有学生信息\n");

printf("5: 保存学生信息到文件\n");

printf("6: 显示帮助信息\n");

printf("0: 退出程序\n");

}

void output(struct Student *head)   //保存学生信息到txt文件功能函数

{

FILE *write;

char filename[20];

struct Student *p1,*p2;

if (head == NULL)   //对空链表进行处理

{

printf("   没有学生信息,保存失败\n   ");

system("pause");

return;

}

p1 = p2 = head;

printf("   请输入保存的文件名(如save): ");

while(!scanf("%s",filename))    //接受文件名,并进行排错(几乎不会出现错误的现象,慎重而设)

{

printf("   输入的文件名有误,请重新输入:");

fflush(stdin);

}

strcat(filename,".txt");    //链接后缀名

write = fopen(filename,"w");    //以写入方式打开文件

fclose(write);

printf("   保存成功\n   ");

}

void Run()

{

char c;int a=0;struct student *head,*stu;long del_num;

PrintHelp();//第一次运行告诉用户那些选项对应哪些操作

printf("请输入操作选项:\n");

while(a!=1)

{

//读入用户的选项

switch(c=getchar())

{

case '0': a=1; break;

case '1':{creat();head=creat();break;}

case '2':del(head,del_num);break;

case'3': print(head);break;

case'4': Del_list(head);break;

case'5':output(head);break;

case'6': Del_list(head);break;

case'7': PrintHelp(); break;

}

}

}

void main()

{

Run();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值