C语言链表指针编程,构建一个学生成绩管理系统
可以进行对已经录入的学生的成绩进行修改,删除,或者增加新的学生信息,并且可以进行保存。
操作界面
构建思路
参考我的上一篇博客C语言构建链表,本次博客中,我对上次的项目进行了升级与修改,使其变得更加的实用性,可以作为一个项目。
核心代码
//定义一个结构体
struct Node
{
struct student data;
struct Node* next;
};
//创建链表头
struct Node* creat_Head_list()
{
struct Node* Head_list = (struct Node*)malloc(sizeof(struct Node));
Head_list->next = NULL;
return Head_list;
}
//创建结点
struct Node* creat_node(struct student data)
{
struct Node* next_node = (struct Node*)malloc(sizeof(struct Node));
next_node->data = data;
next_node->next = NULL;
return next_node;
}
//打印链表
void print_list(struct Node* Head_list)
{
struct Node* Head_list_next = Head_list->next;
printf("姓名\t学号\t数学成绩\t语文成绩\t英语成绩\n");
while (Head_list_next != NULL)
{
printf("%s\t%d\t%d\t\t%d\t\t%d\n", Head_list_next->data.name,Head_list_next->data.num,Head_list_next->data.math,Head_list_next->data.chinese,Head_list_next->data.english);
Head_list_next = Head_list_next->next;
}
}
//从头部添加结点
void inset_by_Head_list(struct Node* Head_list, struct student data)
{
struct Node* inset_node = creat_node(data);
inset_node->next = Head_list->next;
Head_list->next = inset_node;
}
//删除指定的结点
void delet_node(struct Node* Head_list, int num)
{
struct Node* Pmove_front = Head_list;
struct Node* Pmove = Head_list->next;
if (Pmove == NULL)
return;
else
while (Pmove->data.num != num)
{
Pmove_front=Pmove;
Pmove = Pmove_front->next;
if (Pmove == NULL)
return;
}
Pmove_front->next=Pmove->next;
free(Pmove);
}
//修改指定位置的结点
void exchang_node(struct Node*Head_list,int num)
{
struct Node*ex_node=Head_list->next;
if(ex_node==NULL)
return;
else
while(ex_node->data.num!=num)
{
ex_node=ex_node->next;
if(ex_node==NULL)
return;
}
printf("请进行修改数据,输入学生姓名,学号,数学,语文,英语成绩:\n");
setbuf(stdin,NULL);
scanf("%s%d%d%d%d",ex_node->data.name,&ex_node->data.num,&ex_node->data.math,&ex_node->data.chinese,&ex_node->data.english);
}
//保存链表信息到文件夹
void save_node(struct Node*Head_list)
{
if (Head_list==NULL)
{
printf("节点为空,保存失败\n");
return;
}
FILE*fp=fopen(Path,"w");
if(fp==NULL)
{
printf("文件打开失败\n");
return;
}
struct Node*p=Head_list->next;
while(p!=NULL)
{
fwrite(&p->data,sizeof(struct student),1,fp);
p=p->next;
}
fclose(fp);
fp=NULL;
}
last but not least
最后,我将源代码放到这里,有需要的可以下载,瞅瞅,嘿嘿。
谢谢,各位大佬!完整代码下载