思维导图
main.c
#include"student.h"
int main(int argc, const char *argv[])
{
stuPtr h=creat();
int n=0;
add_node(h);
add_node(h);
add_node(h);
show(h);
save(h,"student.txt");
stuPtr ptr=creat();
load(ptr,"student.txt",n);
stuPtr ptr1=creat();
change(ptr1,"student.txt",n);
return 0;
}
student.c
#include"student.h"
stuPtr creat()
{
stuPtr h=(stuPtr)malloc(sizeof(stu));
if(NULL==h)
{
printf("创建失败\n");
return NULL;
}
h->len=0;
h->next=NULL;
return h;
}stuPtr creatNode()
{
stuPtr newnode=(stuPtr)malloc(sizeof(stu));
if(NULL==newnode)
{
printf("节点申请失败\n");
return NULL;
}
newnode->next=NULL;
printf("姓名:");
fscanf(stdin,"%s",newnode->Data.name);
printf("数学:");
fscanf(stdin,"%d",&newnode->Data.math);
printf("语文:");
fscanf(stdin,"%d",&newnode->Data.chinese);
printf("英语:");
fscanf(stdin,"%d",&newnode->Data.english);
printf("物理:");
fscanf(stdin,"%d",&newnode->Data.physical);
printf("化学:");
fscanf(stdin,"%d",&newnode->Data.chemical);
printf("生物:");
fscanf(stdin,"%d",&newnode->Data.biological);
}
int empt(stuPtr h)
{
if(NULL==h)
{
printf("判空失败\n");
return -1;
}
return h->next==NULL;
}
int add_node(stuPtr h)
{
if(NULL==h)
{
printf("添加数据失败\n");
return -1;
}
stuPtr newnode=creatNode();
stuPtr temp=h;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
h->len++;
return 1;
}
void show(stuPtr h)
{
if(NULL==h)
{
printf("遍历失败\n");
return;
}
stuPtr p=h->next;
for(int i=0;i<h->len;i++)
{
fprintf(stdout,"%s %d %d %d %d %d %d\n",p->Data.name,p->Data.math,p->Data.chinese,p->Data.english,p->Data.physical,p->Data.chemical,p->Data.biological);
p=p->next;
}
return;
}
void save(stuPtr h,const char* filename)
{
FILE* p=fopen(filename,"w");
stuPtr p1=h->next;
for(int i=0;i<h->len;i++)
{
fprintf(p,"%s %d %d %d %d %d %d\n",p1->Data.name,p1->Data.math,p1->Data.chinese,p1->Data.english,p1->Data.physical,p1->Data.chemical,p1->Data.biological);
p1=p1->next;
}
fclose(p);
}
void load(stuPtr ptr,const char* filename,int n)
{
if(NULL==ptr)
{
printf("失败\n");
return;
}
FILE* p=fopen(filename,"r");
for(int i=0;i<n;i++)
{
stuPtr p1=creat();
fscanf(p,"%s %d %d %d %d %d %d\n",p1->Data.name,&p1->Data.math,&p1->Data.chinese,&p1->Data.english,&p1->Data.physical,&p1->Data.chemical,&p1->Data.biological);
stuPtr p2=ptr;
while(p2->next!=ptr)
{
p2=p2->next;
}
p2->next=p1;
p1->next=ptr;
ptr->len++;
}
return;
}
void change(stuPtr h,const char* filename,int n)
{
load(h,filename,n);
stuPtr p1=h->next;
for(int i=0;i<n;i++)
{
printf("请输入修改后%s的数学成绩",p1->Data.name);
scanf("%d",&p1->Data.math);
p1=p1->next;
}
save(h,filename);
}
student.h
#ifndef __STUDENT__
#define __STUDENT__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char name[20];
int math;
int chinese;
int english;
int physical;
int chemical;
int biological;
}data,*dataPtr;
typedef struct student
{
union
{
data Data;
int len;
};
struct student *next;
}stu,*stuPtr;
stuPtr creat();
stuPtr creatNode();int empt(stuPtr h);
int add(stuPtr h);
int add_node(stuPtr h);
void show(stuPtr h);
void save(stuPtr h,const char* filename);
void load(stuPtr h,const char* filename,int n);
void change(stuPtr h,const char* filename,int n);
#endif
结果