废话不多说,直接上代码,有需要可自取。完整压缩包可留言留下邮箱发送。
1.main.c
#include "main.h"
#include "menu.h"
#include "add.h"
#include "print.h"
#include "can.h"
#include "del.h"
#include "xiu.h"
struct Node *p=NULL;
int main()
{
int m=0;
struct Node *head= Create_Node();//第一个结点
while(1)
{
m=Menu();
switch(m)
{
case 1:add(head);break;
case 2:xiu(head);break;
case 3:can(head);break;
case 4:print(head);break;
case 5:del(head);break;
case 6:return 0;break;
default:printf("你输入的有误,请重新输入!\n");
}
}
return 0;
}
//开辟头节点
struct Node * Create_Node(void)
{
struct Node *head=(struct Node *)malloc(sizeof(struct Node));//头节点
if(head==NULL)
{
perror("malloc");//该函数为打印错误信息。打印离该函数最近的函数。
}
head->next=NULL;
return head;
}
2.menu.c 菜单界面
#include "menu.h"
int Menu(void)
{
int n=0;
printf("****************************************\n");
printf("**********欢迎使用学生管理系统**********\n");
printf("*************1.添加学生信息*************\n");
printf("*************2.修改学生信息*************\n");
printf("*************3.查看学生信息*************\n");
printf("*************4.遍历学生信息*************\n");
printf("*************5.删除学生信息*************\n");
printf("*************6.退出学生系统*************\n");
scanf("%d",&n);
return n;}
3、add.c 添加
#include "add.h"
void add(struct Node *head)//收到头结点
{
//遍历整个链表,判断原先有没有信息
int num;
printf("请输入学号:\n");
scanf("%d",&num);
getchar();
struct Node *tmp1=head->next;
while(tmp1!=NULL)//遍历。判断学号原先存在否。
{
if(num==tmp1->ymk.id)
{
printf("学好已存在!\n");
return ;
}
tmp1=tmp1->next;
//最后一次进循环的条件:tmp1==NULL
//temp就是最后一个
}
//此时tmp1走到了链表外边,那最后一次金循环就算尾结点的时候。
//若学号不存在执行下面。首先 尾插法
struct Node *temp=head;//temp=head,拉到头结点
while(temp->next!=NULL)//
{//先判断下一个指针域,再移动。没有到尾结点,在尾结点前一个
temp=temp->next;//
//出循环的条件:temp->next==NULL
//temp就是最后一个,出循环的时候temp=NULL
}
struct Node *new=(struct Node *)malloc(sizeof(struct Node));//开辟新的结点.
new->ymk.id=num;
printf("请输入姓名:\n");
scanf("%s",new->ymk.name);
getchar();
printf("请输入成绩:\n");
scanf("%f",&new->ymk.score);
//将第一个结点和第二个结点连接起来。
new->next=NULL;
temp->next=new;
}
4.xiu.c修改
#include "xiu.h"
#include "print.h"
void xiu(struct Node *head)
{
int num;
printf("请输入你要查看学生的学号\n");
scanf("%d",&num);
struct Node *tmp;
tmp=head->next;
while(tmp!=NULL)
{
if(num==tmp->ymk.id)
{
printf("请输入新的姓名:\n");
scanf("%s",tmp->ymk.name);
getchar();
printf("请输入新的成绩:\n");
scanf("%f",&tmp->ymk.score);
printf("修改成功!\n");
print(head);
return ;
}
tmp=tmp->next;
}
printf("你所要修改的学号学生不存在!\n");}
5.can.c 查看
#include "can.h"
void can(struct Node *head)
{
int num;
printf("请输入你要查看学生的学号\n");
scanf("%d",&num);
struct Node *tmp;
tmp=head->next;
while(tmp!=NULL)
{
if(num==tmp->ymk.id)
{
printf("姓名\t学号\t成绩\n");
printf("%s\t%d\t%.2f\n",tmp->ymk.name,tmp->ymk.id,tmp->ymk.score);
return ;
}
tmp=tmp->next;
}
printf("你所查看的学号学生不存在!\n");
}
6.print .c 遍历
#include "print.h"
void print(struct Node *head)
{
printf("姓名\t学号\t成绩\n");
struct Node *tmp;
tmp=head->next;
while(tmp!=NULL)
{
printf("%s\t%d\t%.2f\n",tmp->ymk.name,tmp->ymk.id,tmp->ymk.score);
tmp=tmp->next;
}
}
7.del.c 删除
#include "del.h"
#include "print.h"
void del(struct Node *head)
{
printf("请输入要删除的学号:\n");
int num;
scanf("%d",&num);
struct Node *temp=head;
struct Node *tmp=head->next;
while(tmp!=NULL)
{
if(num==tmp->ymk.id)
{
temp->next=tmp->next;
free(tmp);
print(head);
return ;
}
temp=temp->next;
tmp=tmp->next;
}
printf("查无此人\n");
}
8.wen.c
#include "wen.h"
void save(struct Node *head)
{
FILE *fp=fopen("student.txt","w+");
if(fp==NULL)
{
perror("fopen");
}
fprintf(fp,"学号\t姓名\t成绩\n");
struct Node *tmp=head->next;
while(tmp!=NULL)
{
fprintf(fp,"%d\t%s\t%.2f\n",tmp->ymk.id,tmp->ymk.name,tmp->ymk.score);
tmp=tmp->next;
}
fclose(fp);
}void load(struct Node *head)
{
FILE *fp=fopen("student.txt","r");
struct Node *end=head;
if(fp==NULL)
{
perror("fopen");
}
fscanf(fp,"学号\t姓名\t成绩\n");
while(fgetc(fp)!=EOF)
{
fseek(fp,-1,1);
struct Node *new=(struct Node *)malloc(sizeof(struct Node));
fscanf(fp,"%d\t%s\t%f\n",&new->ymk.id,new->ymk.name,&new->ymk.score);
new->next=NULL;
end->next=new;
end=new;
}
fclose(fp);}