c语言指针链表做的学生管理系统,当录入一个人后,再删除这个人,最后去浏览是,编译器会出现屯屯屯,跪
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言指针链表做的学生管理系统,当录入一个人后,再删除这个人,最后去浏览是,编译器会出现屯屯屯,跪相关的知识,希望对你有一定的参考价值。
c语言指针链表做的学生管理系统,当录入一个人后,再删除这个人,最后去浏览是,编译器会出现屯屯屯,跪求大神!
#include "stdio.h"#include "stdlib.h"
#include "string.h"
#include "windows.h"
int shoudsave=0; /* */
struct student
char num[10];/* 学号 */
char name[20];
char sex[4];
int cgrade;
int mgrade;
int egrade;
int totle;
int ave;
char neartime[10];/* 最近更新时间 */
;
typedef struct node
struct student data;
struct node *next;
Node,*Link;
void menu()
printf("********************************************************************************");
printf("\t1登记学生资料\t\t\t\t\t2删除学生资料\n");
printf("\t3查询学生资料\t\t\t\t\t4修改学生资料\n");
printf("\t5保存学生资料\t\t\t\t\t0退出系统\n");
printf("********************************************************************************\n");
void printstart()
printf("-----------------------------------------------------------------------\n");
void Wrong()
printf("\n=====>提示:输入错误!\n");
void Nofind()
printf("\n=====>提示:没有找到该学生!\n");
void printc() /* 本函数用于输出中文 */
printf(" 学号\t 姓名 性别 英语成绩 数学成绩 C语言成绩 总分 平均分\n");
void printe(Node *p)/* 本函数用于输出英文 */
printf("%-12s%s\t%s\t%d\t%d\t%d\t %d\t %d\n",p->data.num,p->data.name,p->data.sex,p->data.egrade,p->data.mgrade,p->data.cgrade,p->data.totle,p->data.ave);
Node* Locate(Link l,char findmess[],char nameornum[]) /* 该函数用于定位连表中符合要求的接点,并返回该指针 */
Node *r;
if(strcmp(nameornum,"num")==0) /* 按学号查询 */
r=l->next;
while(r!=NULL)
if(strcmp(r->data.num,findmess)==0)
return r;
r=r->next;
else if(strcmp(nameornum,"name")==0) /* 按姓名查询 */
r=l->next;
while(r!=NULL)
if(strcmp(r->data.name,findmess)==0)
return r;
r=r->next;
return 0;
void Add(Link l) /* 增加学生 */
Node *p,*r,*s;
char num[10];
r=l;
s=l->next;
while(r->next!=NULL)
r=r->next; /* 将指针置于最末尾 */
while(1)
printf("请你输入学号(以'0'返回上一级菜单:)");
scanf("%s",num);
if(strcmp(num,"0")==0)
break;
while(s)
if(strcmp(s->data.num,num)==0)
printf("=====>提示:学号为'%s'的学生已经存在,若要修改请你选择'4 修改'!\n",num);
printstart();
printc();
printe(s);
printstart();
printf("\n");
return;
s=s->next;
p=(Node *)malloc(sizeof(Node));
strcpy(p->data.num,num);
printf("请你输入姓名:");
scanf("%s",p->data.name);
getchar();
printf("请你输入性别:");
scanf("%s",p->data.sex);
getchar();
printf("请你输入c语言成绩:");
scanf("%d",&p->data.cgrade);
getchar();
printf("请你输入数学成绩:");
scanf("%d",&p->data.mgrade);
getchar();
printf("请你输入英语成绩:");
scanf("%d",&p->data.egrade);
getchar();
p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade;
p->data.ave=p->data.totle / 3;
/* 信息输入已经完成 */
p->next=NULL;
r->next=p;
r=p;
shoudsave=1;
void Qur(Link l) /* 查询学生 */
int sel;
char findmess[20];
Node *p;
if(!l->next)
printf("\n=====>提示:没有资料可以查询!\n");
return;
printf("\n=====>1按学号查找\n=====>2按姓名查找\n");
scanf("%d",&sel);
if(sel==1)/* 学号 */
printf("请你输入要查找的学号:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
printf("\t\t\t\t查找结果\n");
printstart();
printc();
printe(p);
printstart();
else
Nofind();
else if(sel==2) /* 姓名 */
printf("请你输入要查找的姓名:");
scanf("%s",findmess);
p=Locate(l,findmess,"name");
if(p)
printf("\t\t\t\t查找结果\n");
printstart();
printc();
printe(p);
printstart();
else
Nofind();
else
Wrong();
void Del(Link l) /* 删除 */
int sel;
Node *p,*r;
char findmess[20];
if(!l->next)
printf("\n=====>提示:没有资料可以删除!\n");
return;
printf("\n=====>1按学号删除\n=====>2按姓名删除\n");
scanf("%d",&sel);
if(sel==1)
printf("请你输入要删除的学号:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n=====>提示:该学生已经成功删除!\n");
shoudsave=1;
else
Nofind();
else if(sel==2)
printf("请你输入要删除的姓名:");
scanf("%s",findmess);
p=Locate(l,findmess,"name");
if(p)
r=l;
while(r->next!=p)
r=r->next;
r->next=p->next;
free(p);
printf("\n=====>提示:该学生已经成功删除!\n");
shoudsave=1;
else
Nofind();
else
Wrong();
void Modify(Link l)
Node *p;
char findmess[20];
if(!l->next)
printf("\n=====>提示:没有资料可以修改!\n");
return;
printf("请你输入要修改的学生学号:");
scanf("%s",findmess);
p=Locate(l,findmess,"num");
if(p)
printf("请你输入新学号(原来是%s):",p->data.num);
scanf("%s",p->data.num);
printf("请你输入新姓名(原来是%s):",p->data.name);
scanf("%s",p->data.name);
getchar();
printf("请你输入新性别(原来是%s):",p->data.sex);
scanf("%s",p->data.sex);
printf("请你输入新的c语言成绩(原来是%d分):",p->data.cgrade);
scanf("%d",&p->data.cgrade);
getchar();
printf("请你输入新的数学成绩(原来是%d分):",p->data.mgrade);
scanf("%d",&p->data.mgrade);
getchar();
printf("请你输入新的英语成绩(原来是%d分):",p->data.egrade);
scanf("%d",&p->data.egrade);
p->data.totle=p->data.egrade+p->data.cgrade+p->data.mgrade;
p->data.ave=p->data.totle/3;
printf("\n=====>提示:资料修改成功!\n");
shoudsave=1;
else
Nofind();
void Disp(Link l)
int count=0;
Node *p;
p=l->next;
if(!p)
printf("\n=====>提示:没有资料可以显示!\n");
return;
printf("\t\t\t\t显示结果\n");
printstart();
printc();
printf("\n");
while(p)
printe(p);
p=p->next;
printstart();
printf("\n");
void Tongji(Link l)
Node *pm,*pe,*pc,*pt,*pa; /* 用于指向分数最高的接点 */
Node *r=l->next;
if(!r)
printf("\n=====>提示:没有资料可以统计!\n");
return ;
pm=pe=pc=pt=pa=r;
while(r!=NULL)
if(r->data.cgrade>=pc->data.cgrade)
pc=r;
if(r->data.mgrade>=pm->data.mgrade)
pm=r;
if(r->data.egrade>=pe->data.egrade)
pe=r;
if(r->data.totle>=pt->data.totle)
pt=r;
if(r->data.ave>=pa->data.ave)
pa=r;
r=r->next;
printf("------------------------------统计结果--------------------------------\n");
printf("总分最高者:\t%s %d分\n",pt->data.name,pt->data.totle);
printf("平均分最高者:\t%s %d分\n",pa->data.name,pa->data.ave);
printf("英语最高者:\t%s %d分\n",pe->data.name,pe->data.egrade);
printf("数学最高者:\t%s %d分\n",pm->data.name,pm->data.mgrade);
printf("c语言最高者:\t%s %d分\n",pc->data.name,pc->data.cgrade);
printstart();
void Sort(Link l)
Link ll;
Node *p,*rr,*s;
ll=(Link)malloc(sizeof(Node)); /* 用于做新的连表 */
ll->next=NULL;
if(l->next==NULL)
printf("\n=====>提示:没有资料可以排序!\n");
return ;
p=l->next;
while(p)
s=(Node*)malloc(sizeof(Node)); /* 新建接点用于保存信息 */
s->data=p->data;
s->next=NULL;
rr=ll;
while(rr->next!=NULL && rr->next->data.totle>=p->data.totle)
rr=rr->next;
if(rr->next==NULL)
rr->next=s;
else
s->next=rr->next;
rr->next=s;
p=p->next;
free(l);
l->next=ll->next;
printf("\n=====>提示:排序已经完成!\n");
void Save(Link l)
FILE* fp;
Node *p;
int flag=1,count=0;
fp=fopen("c:\\student","wb");
if(fp==NULL)
printf("\n=====>提示:重新打开文件时发生错误!\n");
exit(1);
p=l->next;
while(p)
if(fwrite(p,sizeof(Node),1,fp)==1)
p=p->next;
count++;
else
flag=0;
break;
if(flag)
printf("\n=====>提示:文件保存成功.(有%d条记录已经保存.)\n",count);
shoudsave=0;
fclose(fp);
void main()
Link l;/* 连表 */
FILE *fp; /* 文件指针 */
int sel;
char ch;
char jian;
int count=0;
Node *p,*r;
printf("\t\t\t\t学生成绩管理系统\n\t\t\t\t----------------------实验项目\n");
l=(Node*)malloc(sizeof(Node));
l->next=NULL;
r=l;
fp=fopen("C:\\student","rb");
if(fp==NULL)
printf("\n=====>提示:文件还不存在,是否创建?(y/n)\n");
scanf("%c",&jian);
if(jian=='y'||jian=='Y')
fp=fopen("C:\\student","wb");
else
exit(0);
printf("\n=====>提示:文件已经打开,正在导入记录......\n");
while(!feof(fp))
p=(Node*)malloc(sizeof(Node));
if(fread(p,sizeof(Node),1,fp)) /* 将文件的内容放入接点中 */
p->next=NULL;
r->next=p;
r=p; /* 将该接点挂入连中 */
count++;
fclose(fp); /* 关闭文件 */
printf("\n=====>提示:记录导入完毕,共导入%d条记录.\n",count);
while(1)
menu();
printf("请你选择操作:");
scanf("%d",&sel);
if(sel==0)
if(shoudsave==1)
getchar();
printf("\n=====>提示:资料已经改动,是否将改动保存到文件中(y/n)?\n");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
Save(l);
printf("\n=====>提示:你已经退出系统,再见!\n");
break;
switch(sel)
case 1:Add(l);break; /* 增加学生 */
case 2:Del(l);break;/* 删除学生 */
case 3:Qur(l);break;/* 查询学生 */
case 4:Modify(l);break;/* 修改学生 */
case 5:Save(l);break;/* 保存学生 */
case 9:printf("\t\t\t==========帮助信息==========\n");break;
default: Wrong();getchar();break;
Sleep(100);
/* */ 参考技术A #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
# define LEN sizeof(struct Student)
struct Student
char num[10];
char name[20];
char sex[10];
int age;
char phone[12];
char qq[12];
float cscore;
float escore;
struct Student *next;
;
char filename[30];
struct Student *Creat(int n)
void menu_print_in(void);
struct Student *head;
struct Student *p1, *p2;
system("cls");
for(int i=1;i<n+1;i++)
p1 = (struct Student*)malloc(LEN);
menu_print_in();
scanf("%s%s%s%d%s%s%f%f",p1->num,p1->name,p1->sex,
&p1->age,p1->phone,p1->qq,&p1->cscore,&p1->escore);
p1->next = NULL;
if(i==1)
head = p2 = p1;
else
p2->next = p1;
p2 = p1;
return(head);
void WriteData_wb(struct Student *head)
FILE *fp;
struct Student *p;
if((fp = fopen(filename, "wb"))==NULL)
printf("\\a error! Can not open the file!");
p = head;
while(p!=NULL)
if(fwrite(p,LEN,1,fp)!=1)
printf("写入数据出错\\n");
fclose(fp);
return;
p=p->next;
fclose(fp);
void WriteData_ab(struct Student *head)
FILE *fp;
struct Student *p;
if((fp = fopen(filename, "ab"))==NULL)
printf("\\a error! Can not open the file!");
p = head;
while(p!=NULL)
if(fwrite(p,LEN,1,fp)!=1)
printf("写入数据出错\\n");
fclose(fp);
return;
p=p->next;
fclose(fp);
struct Student *ReadData(void)
struct Student *head = NULL;
struct Student *p1, *p2;//s = p1;p = p2;
FILE *fp;
if((fp=fopen(filename,"rb+"))==NULL)
printf("打开文件出错\\n");
exit(0);
while(!feof(fp))
if((p1=(struct Student*)malloc(LEN))==NULL)
printf("内存申请出错\\n");
fclose(fp);
exit(0);
if(fread(p1,LEN,1,fp)!=1)
free(p1);
break;
if(head==NULL)
head=p2=p1;
else
p2->next=p1;
p2=p1;
fclose(fp);
return (head);
void Print_inquire_all(void)
void menu_print_out(void);
struct Student *pt;
pt = ReadData();
menu_print_out();
do
printf("%-10s%6s%8s%4d%13s%11s %4.1f %4.1f %4.1f %4.1f\\n",
pt->num,pt->name,pt->sex,pt->age,pt->phone,pt->qq,pt->cscore,
pt->escore,(pt->cscore+pt->escore)/2,pt->cscore+pt->escore);
pt = pt->next;
while(pt!=NULL);
printf("\\n\\n");
int Print_inquire_num()
void menu_print_out(void);
struct Student *pt;
char str_num[10];
printf("◎请输入您要查询的学号:");
scanf("%s", str_num);
pt = ReadData();
menu_print_out();
do
if(strcmp(pt->num,str_num)==0)
printf("%-10s%6s%8s%4d%13s%11s %4.1f %4.1f %4.1f %4.1f\\n",
pt->num,pt->name,pt->sex,pt->age,pt->phone,pt->qq,pt->cscore,
pt->escore,(pt->cscore+pt->escore)/2,pt->cscore+pt->escore);
printf("\\n\\n");
return 0;
pt = pt->next;
while(pt!=NULL);
printf("数据库中没有存储您要查询的数据!\\n");
printf("\\n\\n");
return 0;
int Print_inquire_name()
void menu_print_out(void);
struct Student *pt;
char str_name[20];
printf("◎请输入您要查询的姓名:");
scanf("%s", str_name);
pt = ReadData();
menu_print_out();
do
if(strcmp(pt->name,str_name)==0)
printf("%-10s%6s%8s%4d%13s%11s %4.1f %4.1f %4.1f %4.1f\\n",
pt->num,pt->name,pt->sex,pt->age,pt->phone,pt->qq,pt->cscore,
pt->escore,(pt->cscore+pt->escore)/2,pt->cscore+pt->escore);
printf("\\n\\n");
return 0;
pt = pt->next;
while(pt!=NULL);
printf("数据库中没有存储您要查询的数据!\\n");
printf("\\n\\n");
return 0;
int Print_inquire_fuzzy(void)
void menu_print_out(void);
struct Student *pt;
char str_find[20];
int m = 0;
printf("◎请输入您要查询的关键词:");
scanf("%s", str_find);
pt = ReadData();
menu_print_out();
do
if(strstr(pt->num,str_find)!=0||strstr(pt->name,str_find)!=0
||strstr(pt->sex,str_find)!=0||strstr(pt->phone,str_find)!=0
||strstr(pt->qq,str_find)!=0)
printf("%-10s%6s%8s%4d%13s%11s %4.1f %4.1f %4.1f %4.1f\\n",
pt->num,pt->name,pt->sex,pt->age,pt->phone,pt->qq,pt->cscore,
pt->escore,(pt->cscore+pt->escore)/2,pt->cscore+pt->escore);
m = 1;
pt = pt->next;
while(pt!=NULL);
if(!m)
printf("数据库中没有存储您要查询的数据!\\n");
printf("\\n\\n");
return 0;
int Print_inquire_stats(void)
struct Student *head, *p1;
float CMIN, CMAX, EMIN, EMAX, SMIN, SMAX;
head = ReadData();
p1 = head;
CMIN = CMAX = p1->cscore;
EMIN = EMAX = p1->escore;
SMIN = SMAX = p1->cscore + p1->escore;
while(p1->next!=NULL)
if(p1->cscore > CMAX)
CMAX = p1->cscore;
if(p1->cscore < CMIN)
CMIN = p1->cscore;
if(p1->escore > EMAX)
EMAX = p1->escore;
if(p1->escore < EMIN)
EMIN = p1->escore;
if((p1->cscore + p1->escore) > SMAX)
SMAX = (p1->cscore + p1->escore);
if((p1->cscore + p1->escore) < SMIN)
SMIN = (p1->cscore + p1->escore);
p1 = p1->next;
printf("\\n");
printf("◎数据库中C语言最高分为:%4.1f \\n", CMAX);
printf("◎数据库中C语言最低分为:%4.1f \\n\\n", CMIN);
printf("◎数据库中英语最高分为:%4.1f \\n", EMAX);
printf("◎数据库中英语最低分为:%4.1f \\n\\n", EMIN);
printf("◎数据库中总成绩最高分为:%4.1f \\n", SMAX);
printf("◎数据库中总成绩最低分为:%4.1f \\n\\n", SMIN);
printf("\\n");
printf("【PS:更多统计功能更在开发中,敬请期待O(∩_∩)O】\\n\\n");
return 0;
/*【1】修改数据之删除记录*/
int Delete()
struct Student *pt1, *pt2, *head;
char str_num[20];
printf("\\n◎请输入您要删除的学号信息:");
scanf("%s", str_num);
pt1 = ReadData();
pt2 = pt1->next;
head = pt1;
while(pt2!=NULL)
if(strcmp(pt1->num,str_num)==0)
WriteData_wb(pt2);
else if(strcmp(pt2->num,str_num)==0)
pt1->next = pt2->next;
WriteData_wb(head);
pt2 = pt2->next;
pt1 = pt1->next;
if(pt2!=NULL)
printf("数据库中没有存储您要删除的数据!\\n");
printf("\\n\\n");
return 0;
/*【2】修改数据之修改记录*/
int Amend()
void menu_print_in(void);
struct Student *pt1, *pt2, *head;
char str_num[20];
printf("◎请输入您要修改的学号信息:");
scanf("%s", str_num);
pt1 = ReadData();
pt2 = pt1->next;
head = pt1;
while(pt2!=NULL)
if(strcmp(pt1->num,str_num)==0)
menu_print_in();
scanf("%s%s%s%d%s%s%f%f",pt1->num,pt1->name,pt1->sex,
&pt1->age,pt1->phone,pt1->qq,&pt1->cscore,&pt1->escore);
WriteData_wb(head);
else if(strcmp(pt2->num,str_num)==0)
menu_print_in();
scanf("%s%s%s%d%s%s%f%f",pt2->num,pt2->name,pt2->sex,
&pt2->age,pt2->phone,pt2->qq,&pt2->cscore,&pt2->escore);
WriteData_wb(head);
pt2 = pt2->next;
pt1 = pt1->next;
if(pt2!=NULL)
printf("数据库中没有存储您要删除的数据!\\n");
return 0;
int Neaten()
struct Student *first;
struct Student *tail;
struct Student *p_min;
struct Student *min;
struct Student *p;
struct Student *head;
head = ReadData();
first = NULL;
while(head!=NULL)
for(p=head,min=head; p->next!=NULL; p=p->next)
if(strcmp(p->next->num,min->num)<0)
p_min = p;
min = p->next;
if(first==NULL)
first = min;
tail = min;
else
tail->next = min;
tail = min;
if(min==head)
head = head->next;
else
p_min->next = min->next;
if(first!=NULL)
tail->next = NULL;
head = first;
WriteData_wb(head);
return 0;
int Creat_num(void)
printf("\\n◎请输入您此次要添加的数据个数:");
int n;
if(scanf("%d", &n)!=1)
printf("\\a error!");
return n;
int File_name()
printf("\\n◎请输入您想要打开的文件:");
if(scanf("%s", filename)!=1)
printf("\\a error!");
return 0;
void menu(void)
void menu_add(void);
void menu_inquire(void);
void menu_amend(void);
printf(" ╭════════╮ \\n");
printf("╭══════╣学生管理系统V1.0╠══════╮\\n");
printf("║ ╰════════╯ ║\\n");
printf("║ 【1】添加数据 【3】修改数据 ║\\n");
printf("║ ║\\n");
printf("║ 【2】查询数据 【4】退出系统 ║\\n");
printf("║ ║\\n");
printf("╰══════════════════════╯\\n");
printf("◎请输入功能前的序号进入相应的工具:【 】\\b\\b");
int a = 0;
a = getchar();
while(a!='1'&&a!='2'&&a!='3'&&a!='4')
printf("error! please input the right number!\\n");
putchar('\\a');
getchar();
printf("◎请重新输入功能前的序号进入相应的工具:【 】\\b\\b");
a = getchar();
switch(a)
case '1': File_name();menu_add();
break;
case '2': File_name();menu_inquire();
break;
case '3': File_name();menu_amend();
break;
case '4': exit(0);
break;
getchar();
/*二级菜单之添加数据*/
void menu_add(void)
system("cls");
getchar();
printf(" ╭════════╮ \\n");
printf("╭══════╣ 添加数据方式 ╠══════╮\\n");
printf("║ ╰════════╯ ║\\n");
printf("║ 【1】新建文件 【2】增添数据 【3】返回菜单 ║\\n");
printf("║ ║\\n");
printf("╰══════════════════════╯\\n");
printf("◎请输入功能前的序号进入相应的工具:【 】\\b\\b");
int a = 0;
a = getchar();
while(a!='1'&&a!='2'&&a!='3')
printf("error! please input the right number!\\n");
putchar('\\a');
getchar();
printf("◎请重新输入功能前的序号进入相应的工具:【 】\\b\\b");
a = getchar();
switch(a)
case '1': WriteData_wb(Creat(Creat_num()));
printf("\\n◎新建文件成功且数据已成功保存◎\\n");
system("pause");
system("cls");
menu_add();
break;
case '2': WriteData_ab(Creat(Creat_num()));
printf("\\n◎数据已成功添加◎\\n");
system("pause");
system("cls");
menu_add();
break;
case '3': system("cls");
getchar();
menu();
break;
/*二级菜单之查询数据*/
void menu_inquire(void)
system("cls");
getchar();
while(1)
system("cls");
printf(" ╭════════╮ \\n");
printf("╭══════╣ 查询数据方式 ╠══════╮\\n");
printf("║ ╰════════╯ ║\\n");
printf("║ 【1】全量查询 【4】模糊查询 ║\\n");
printf("║ ║\\n");
printf("║ 【2】学号查询 【5】成绩统计 ║\\n");
printf("║ ║\\n");
printf("║ 【3】姓名查询 【6】返回菜单 ║\\n");
printf("╰══════════════════════╯\\n");
printf("◎请输入功能前的序号进入相应的工具:【 】\\b\\b");
int a = 0;
a = getchar();
while(a!='1'&&a!='2'&&a!='3'&&a!='3'&&a!='4'&&a!='5'&&a!='6')
printf("error! please input the right number!\\n");
putchar('\\a');
getchar();
printf("◎请重新输入功能前的序号进入相应的工具:【 】\\b\\b");
a = getchar();
switch(a)
case '1': Print_inquire_all();system("pause");getchar();
break;
case '2': Print_inquire_num();system("pause");getchar();
break;
case '3': Print_inquire_name();system("pause");getchar();
break;
case '4': Print_inquire_fuzzy();system("pause");getchar();;
break;
case '5': Print_inquire_stats();system("pause");getchar();
break;
case '6': system("cls");getchar();menu();
break;
参考技术B 这个现象一般是内存泄漏或者读取或者删除链表有误造成的!没有添加删除源代码,不好说呀
学生成绩管理系统C(链表)语言
#include"stdio.h" #include"stdlib.h" #include"string.h" //用于调用一些函数 struct person { char name[20]; int ID; int chinese; int english; int math; struct person *next; //连接处的指针 }; //由于create里面就有initialize所以先把initialize放在前面 void initialize(struct person *p, int num) { //初始化链表里面的值 printf("innitialize person %d name is:", num); //num用于计入输入的第几个同学 scanf_s("%s", &p->name, sizeof(p->name)); getchar(); printf("ID:"); scanf_s("%d", &p->ID); getchar(); printf("chinese:"); scanf_s("%d", &p->chinese); getchar(); printf("math:"); scanf_s("%d", &p->math); getchar(); printf("english:"); scanf_s("%d", &p->english); getchar(); } struct person *create(int len) { int num = 0; struct person *h = 0, *c, *pre = 0; while (num < len) { c = (struct person*)malloc(sizeof(struct person)); //malloc取内存 sizeof为这个内存的大小 然后转化成指针 if (num == 0) { h = c; pre = c; } // 如果num=0 h存下了首地址 为以后拿做准备 c->next = NULL; if (num) { pre->next = c; //pre为前一块内存的地址 把后一块的首地址赋给前一块的尾 pre = c; //收尾连接后 pre指前一块的功能就完成了 然后再指向现在的内存 为下一次拿内存、赋地址做准备 } initialize(c, num); //每取一块地址就去输入一次 ++num; }return h; } void traverse(struct person *head) { int index = 1; // 用于计数第几个学生 while (head != NULL) { //同样一直到后面没有地址结束 printf("name is: %s ID is:%d chinese is:%d englishi is:%d math is:%d ", head->name, head->ID, head->chinese, head->english, head->math); head = head->next; //前一个输完后就要指向下一块地址 ++index; } } int getlength(struct person *head) { int num = 0; while (head != NULL) { //当head指向后面没有了 它就是NULL 结束 ++num; head = head->next; //如果head 不是NULL ++num后要把head指针指向最后 }return num; } //增加学生信息 void append_node(struct person *h) { struct person *t = h, *p; while (t->next != NULL) { t = t->next; }p = (struct person *)malloc(sizeof(struct person)); initialize(p, 0); p->next = NULL; t->next = p; traverse(h); } //删除 struct person * delete_ID(struct person *head, int ID, int len) { struct person *t = head; struct person *temp; for (int i = 0; i < (len - 1); ++i) { if (i == 0) { if (head->ID == ID) { head = head->next; free(t); traverse(head); return head; } if ((t->next)->ID == ID) { temp = t->next; t->next = (t->next)->next; free(temp); traverse(head); return head; } } if (i != 0) { if ((t->next)->ID == ID) { temp = t->next; t->next = (t->next)->next; free(temp); traverse(head); return head; }t = t->next; } }return head; } //学号查询 void search_ID(struct person *head, int ID) { while (head != NULL) { if (head->ID == ID) { printf("name is: %s ID is:%d chinese is:%d englishi is:%d math is:%d ", head->name, head->ID, head->chinese, head->english, head->math); }head = head->next; } } //姓名查询 void search_name(struct person *head, char name[20]) { while (head != NULL) { if ((strcmp(head->name, name)) == 0) { printf("name is: %s ID is:%d chinese is:%d englishi is:%d math is:%d ", head->name, head->ID, head->chinese, head->english, head->math); } head = head->next; } } //指定学生修改 void change(struct person *head, char name[20]) { while (head != NULL) { if (strcmp(head->name, name) == 0) { printf(" name is:"); scanf_s("%s", &head->name, sizeof(head->name)); getchar(); printf("ID:"); scanf_s("%d", &head->ID); getchar(); printf("chinese:"); scanf_s("%d", &head->chinese); getchar(); printf("english:"); scanf_s("%d", &head->english); getchar(); printf("math:"); scanf_s("%d", &head->math); getchar(); } head = head->next; } traverse(head); } //数学分数平均数 int average_math(struct person *head, int len) { int i = 0, sum = 0, average; struct person *t = head; while (t != NULL) { sum += t->math; t = t->next; } average = (sum / len); return average; } //英语分数平均数 int average_english(struct person *head, int len) { int i = 0, sum = 0, average; struct person *t = head; while (t != NULL) { sum += t->english; t = t->next; } average = (sum / len); return average; } //语文分数平均数 int average_chinese(struct person *head, int len) { int sum = 0, average; struct person *t = head; while (t != NULL) { sum += t->chinese; t = t->next; } average = (sum / len); return average; } //成绩区间统计 int statistics_math(struct person*head, int min, int max) { int conter = 0; while (head!= NULL) { if (head->math >= min&&head->math <= max) { ++conter; printf("name:%s math score:%d ", head->name, head->math); }head = head->next; }return conter; } int statistics_chinese(struct person*head, int min, int max) { int conter = 0; while (head != NULL) { if (head->chinese >= min&&head->chinese <= max) { ++conter; printf("name:%s chinese score:%d ", head->name, head->chinese); }head = head->next; }return conter; } int statistics_english(struct person*head, int min, int max) { int conter = 0; while (head != NULL) { if (head->english >= min&&head->english <= max) { ++conter; printf("name:%s english score:%d ", head->name, head->english); }head = head->next; }return conter; } //某名学生的平均成绩 int average_name(struct person*head, char name[20]) { int av = 0; while (head != NULL) { if (strcmp(head->name, name) == 0) { av += head->math; av += head->chinese; av += head->english; } head = head->next; } av = av / 3; return av; } //排序数学 void rank_math(struct person *h, int len) { struct person *t = h, *pre = h; int i, math, chinese, english,ID; char name[20]; t = t->next; for (i = 0; i < (len - 1); ++i) { while (t != NULL) { if ((t->math) >(pre->math)) { ID = t->ID; t->ID = pre->ID; pre->ID = ID; math = t->math; t->math = pre->math; pre->math = math; strcpy_s(name, t->name); strcpy_s(t->name, pre->name); strcpy_s(pre->name, name); chinese = t->chinese; t->chinese = pre->chinese; pre->chinese = chinese; english = t->english; t->english = pre->english; pre->english = english; } t = t->next; }pre = pre->next; t = pre->next; } traverse(h); } //排序语文 void rank_chinese(struct person *h, int len) { struct person *t = h, *pre = h; int i, math, chinese, english,ID; char name[20]; t = t->next; for (i = 0; i < (len - 1); ++i) { while (t != NULL) { if ((t->chinese) >(pre->chinese)) { ID = t->ID; t->ID = pre->ID; pre->ID = ID; math = t->math; t->math = pre->math; pre->math = math; strcpy_s(name, t->name); strcpy_s(t->name, pre->name); strcpy_s(pre->name, name); chinese = t->chinese; t->chinese = pre->chinese; pre->chinese = chinese; english = t->english; t->english = pre->english; pre->english = english; } t = t->next; }pre = pre->next; t = pre->next; } traverse(h); } //排序英语 void rank_english(struct person *h, int len) { struct person *t = h, *pre = h; int i, math, chinese, english,ID; char name[20]; t = t->next; for (i = 0; i < (len - 1); ++i) { while (t != NULL) { if ((t->english) >(pre->english)) { ID = t->ID; t->ID = pre->ID; pre->ID = ID; math = t->math; t->math = pre->math; pre->math = math; strcpy_s(name, t->name); strcpy_s(t->name, pre->name); strcpy_s(pre->name, name); chinese = t->chinese; t->chinese = pre->chinese; pre->chinese = chinese; english = t->english; t->english = pre->english; pre->english = english; } t = t->next; }pre = pre->next; t = pre->next; } traverse(h); } void release(struct person *head) { struct person *n; //需要一个指针存着下一个地址 while (head != NULL) { n = head->next; //把n指向下一块要释放的地址 free(head); head = n; //然后再把head从前一个地址移到下一个地址 } } //取长度 int getlen(struct person *head) { int conter = 0; struct person*t = head; while (t != NULL) { ++conter; t = t->next; } return conter; } //存入文件 void openfile(struct person *head, int len) { FILE *fp; struct person*t = head; errno_t err; int temp; char str[100]; char s[10]; if ((err = fopen_s(&fp, "D:\学生信息", "w")) != 0) { printf("文件打开错误 "); } else { printf("文件打开成功 "); } for (int i = 0; i < len; ++i) { strcpy_s(str, t->name); fputs("name:", fp); fputs(str, fp); fputs(": ", fp); fputs("ID:", fp); sprintf_s(s, "%d", t->ID); fputs(s, fp); fputs(" ", fp); sprintf_s(s, "%d", t->chinese); fputs("chinese:", fp); fputs(s, fp); fputs(" ", fp); sprintf_s(s, "%d", t->math); fputs("math:", fp); fputs(s, fp); fputs(" ", fp); sprintf_s(s, "%d", t->english); fputs("english:", fp); fputs(s, fp); fputs(" ", fp); t = t->next; } return; } void menu(struct person *head, int len) { int m = 0; int min, max; int ID = 0; int conter = 0; char name[20]; while (1) { printf(" 请选择您需要的操作: "); printf(" 0. 遍历学生信息 "); printf(" 1. 增加学生信息 "); printf(" 2. 删除学生信息 "); printf(" 3. 修改学生信息 "); printf(" 4. 按姓名查询 "); printf(" 5. 按学号查询 "); printf(" 6. 语文成绩在某个区间段的人数以及学生 "); printf(" 7. 数学成绩在某个区间段的人数以及学生 "); printf(" 8. 英语成绩在某个区间段的人数以及学生 "); printf(" 9. 语文平均分 "); printf(" 10. 数学平均分 "); printf(" 11. 英语平均分 "); printf(" 12. 某个学生的三科平均成绩 "); printf(" 13. 按语文成绩从高到低排序 "); printf(" 14. 按数学成绩从高到低排序 "); printf(" 15. 按英语成绩从高到底排序 "); printf(" 16. 结束功能并把信息写入文件中 "); scanf_s("%d", &m); switch (m) { case 0: traverse(head); break; case 1: append_node(head); break; case 2: {printf("要删除学生信息的学号:"); scanf_s("%d", &ID); head=delete_ID(head, ID, getlen(head)); }break; case 3: {printf("需要修改学生信息的同学姓名:"); scanf_s("%s", &name, sizeof(name)); change(head, name); }break; case 4: {printf("search by name:"); scanf_s("%s", &name, sizeof(name)); search_name(head, name); }break; case 5: {printf("学号查询:"); scanf_s("%d", &ID); search_ID(head, ID); }break; case 6: {printf("请输入语文成绩的区间:"); printf("min="); scanf_s("%d", &min); printf("max="); scanf_s("%d", &max); printf("语文成绩在区间%d到%d之间的学生:%d人 ", min, max, statistics_chinese(head, min, max)); }break; case 7: {printf("请输入数学成绩的区间:"); printf("min="); scanf_s("%d", &min); printf("max="); scanf_s("%d", &max); printf("数学成绩在区间%d到%d之间的学生:%d人 ", min, max, statistics_math(head, min, max)); }break; case 8: {printf("请输入英语成绩的区间:"); printf("min="); scanf_s("%d", &min); printf("max="); scanf_s("%d", &max); printf("英语成绩在区间%d到%d之间的学生:共有%d人 ", min, max, statistics_english(head, min, max)); }break; case 9: { printf("average of chinese is%d", average_chinese(head, getlen(head))); }break; case 10: { printf("average of math is%d", average_math(head, getlen(head))); }break; case 11: { printf("average of english is%d", average_english(head, getlen(head))); }break; case 12: {printf("请输入学生的姓名:"); scanf_s("%s", &name, sizeof(name)); printf("%s 的平均成绩为:%d", name, average_name(head, name)); }break; case 13: {printf("按照语文成绩从高到底排序:"); rank_chinese(head, getlen(head)); }break; case 14: {printf("按照数学成绩从高到底排序:"); rank_math(head, getlen(head)); }break; case 15: {printf("按照英语成绩从高到底排序:"); rank_english(head, getlen(head)); }break; case 16: openfile(head, getlen(head)); return; } } } int main() { struct person *head; int len; int min, max; int ID = 0; char name[20]; printf("请输入学生信息"); printf("学生人数:"); scanf_s("%d", &len); //输入要取的地址多少 head = create(len); // 创建地址 在create里面就有初始复制函数嵌套 traverse(head); //遍历 menu(head, getlen(head)); release(head); //释放内存 system("pause"); return 0; }
以上是关于c语言指针链表做的学生管理系统,当录入一个人后,再删除这个人,最后去浏览是,编译器会出现屯屯屯,跪的主要内容,如果未能解决你的问题,请参考以下文章