#include#include #include #include #define LEN sizeof(struct student) //定义学生结构体 struct student { long num; int score; struct student *next; }; int n=0;//定义学生人数为全局变量 int menu(); struct student *create(struct student *head);//批量录入 void print(struct student *head);//输出 struct student *del(struct student *head,long num);//删除 struct student *insert(struct student *head,struct student *inStu);//插入 void query(struct student *head,long num); //查询 void main() { struct student *head=NULL; //学生链表开始为空 int answer; //用户的回答 long num; //要查询或者要删除的学生的学号 struct student *inStu=NULL; //要插入的学生结点 do{ system("cls"); answer=menu(); switch(answer) { case 1: system("cls"); head=create(head); printf(" nnntt你已经退出学生信息的录入,按任意键返回主菜单……"); getch(); break; case 2: system("cls"); print(head); printf(" 按任意键返回主菜单……"); getch(); break; case 3: system("cls"); if(head==NULL) { printf("nnntt抱歉,学生数据为空,无法删除!!!nnn"); printf(" 按任意键返回主菜单……"); getch(); } else { printf("请输入要删除的学生的学号:"); scanf("%ld",&num); head=del(head,num); printf(" 按任意键返回主菜单……"); getch(); } break; case 4: system("cls"); inStu=(struct student *)malloc(LEN); printf("请输入要插入的学生的学号和成绩:"); scanf("%ld%d",&inStu->num ,&inStu->score ); head=insert(head,inStu); printf(" 按任意键返回主菜单……"); getch(); break; case 5: system("cls"); if(head==NULL) { printf("nnntt抱歉,学生数据为空,无法查询!!!nnn"); printf(" 按任意键返回主菜单……"); getch(); } else { printf("nntt请输入要查询的学生的学号:"); scanf("%ld",&num); query(head,num); printf("nn 按任意键返回主菜单……"); getch(); } break; } }while(answer!=0); } //主界面 int menu() { int option; printf("nnnt*********************欢迎使用学生信息管理系统********************nnn"); printf("t 1.录入学生信息 nn"); printf("t 2.显示学生信息 nn"); printf("t 3.删除学生信息 nn"); printf("t 4.插入学生信息 nn"); printf("t 5.查询学生信息 nn"); printf("t 0.退出系统 nnn"); printf("t*****************************************************************nnn"); printf(" tt请选择0—5中的数字之一,以进行学生信息管理:"); while(1) { scanf("%d",&option); if(option>=0&&option<=5) break; else printf("nn您选择的选项不对,应该输入0、1、2、3、4、5中的一个数字,请重新选择:"); } return(option); } //创建链表 struct student *create(struct student *head) { struct student *p=NULL;//要插入的新结点 struct student *q=NULL;//链表的尾部结点 p=(struct student *)malloc(LEN); printf("nnt请输入学生的学号和成绩(数据之间用空格分隔,输入完成按回车键确认。nnt如果想结束录入工作,请输入成绩-1):n"); scanf("%ld",&p->num); scanf("%d",&p->score); if(head==NULL) { q=p; } else { q=head; while(q->next !=NULL) q=q->next ; } while(p->score!=-1) { n++; if(head==NULL) head=p; else { q->next=p; q=p; } p=(struct student * )malloc(LEN); scanf("%ld",&p->num ); scanf("%d",&p->score); } q->next=NULL; return(head); } //显示学生信息 void print(struct student *head) { struct student *p=NULL; //指向链表当前结点的指针 p=head; if(head!=NULL) { printf("nntt*********共有 %d 个记录*********nn",n); printf("tt————————————————nn"); printf("ttt%-15s%-15snn","学号","成绩"); while(p!=NULL) { printf("ttt%-15ld%-15dnn",p->num,p->score); p=p->next; } printf("tt********************************nn"); } else printf("nntt抱歉,没有学生数据!nn"); } //删除学生信息 struct student *del(struct student *head,long num) { struct student *p=NULL;//要删除的结点 struct student *q=NULL;//要删除的结点的前一个结点 p=head; while(p->num!=num&&p->next !=NULL) { q=p; p=p->next ; } if(num==p->num ) { if(p==head) head=p->next ; else q->next =p->next ; n=n-1; printf("nnntt已成功删除学号为%ld的学生信息nnnn",num); } else printf("nntt抱歉,没有找到学号为%ld的学生的信息nnn",num); return(head); } //在链表的尾部插入学生信息 struct student *insert(struct student *head,struct student *inStu) { struct student *p0=NULL;//要插入的结点 struct student *p1=NULL;//要插入的结点的前一个结点 if(head!=NULL) { p1=head; while(p1->num !=inStu->num &&p1->next !=NULL) p1=p1->next ; if(p1->num ==inStu->num ) { printf("nnntt很遗憾,你要插入的学生的学号%ld已经存在,无法插入!!nnn",inStu->num ); return(head); } } p0=inStu; p1=head; if(head==NULL) { head=p0; p0->next =NULL; } else { while(p1->next !=NULL) p1=p1->next ; p1->next =p0; p0->next=NULL; } n=n+1; printf("nnnttt恭喜您,已成功插入学号为%ld的学生信息!nnnn",inStu->num); return(head); } //根据学号查询学生信息 void query(struct student *head,long num) { struct student *p=NULL; p=head; while(p->num!=num&&p->next !=NULL) { p=p->next ; } if(p->num==num) { printf("nntt*********查询到的学生记录*********nn",n); printf("tt————————————————nn"); printf("ttt%-15s%-15snn","学号","成绩"); printf("ttt%-15ld%-15dnn",p->num,p->score); printf("tt********************************nn"); } else printf("nntt抱歉,没有找到学号为%ld的学生的信息nnn",num); return; }



