#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <string.h> typedef struct node { int num; char name[10]; int score; struct node *pnext; }NODE,*PNODE;//头插法创建单链表 PNODE createlist1(PNODE phead); //尾插法创建单链表 PNODE createlist2(PNODE phead); //遍历链表 void traverselist(PNODE phead); //对链表中的元素进行查找void findvalue(PNODE phead);//在链表中插入新元素(后插法)PNODE insert1(PNODE phead);//在链表中插入新元素(前插法)PNODE insert2(PNODE phead);//在链表中插入新元素(改进的前插法)PNODE insert3(PNODE phead); //删除链表中的元素 PNODE deletelist(PNODE phead);//修改链表中的元素void changelist(PNODE phead); void menu(PNODE phead){int i;printf("\t\t\t学生管理系统\n"); while(1) { printf("********************************************************************************\n"); printf("\t\t\t1建立链表\n\t\t\t2遍历链表\n\t\t\t3查找学生\n\t\t\t4插入学生信息\n\t\t\t5删除记录\n\t\t\t6修改学生信息\n\t\t\t7清屏\n\t\t\t8退出\n"); printf("********************************************************************************\n"); printf("\t\t\t请输入你的选择:\n"); scanf("%d",&i); switch(i){case 1:printf("\t\t\t1头插法建立单链表\n\t\t\t2尾插法建立单链表\n");scanf("%d",&i);if(i==1){phead=createlist1(phead);}else if(i==2){phead=createlist2(phead);}else{printf("输入格式错误!\n");}break;case 2:traverselist(phead);break;case 3:findvalue(phead); break;case 4:phead=insert3(phead);break;case 5: phead=deletelist(phead); break;case 6:changelist(phead);break;case 7: system("cls"); break;case 8:exit(1);break;default:printf("输入有误,从新输入!\n");break;}}}int main() { PNODE phead=NULL; menu(phead); return 0; } //判断链表是否为空int isempty(PNODE phead){if(phead==NULL){printf("链表为空!\n");return 1;}return 0;}//头插法建立单链表PNODE createlist1(PNODE phead) { int i,n; PNODE pnew=NULL; printf("请输入你想创建的节点数\n"); scanf("%d",&n); if(n==0)return phead; //头插法建立节点 for(i=1;i<n+1;i++) { printf("创建第%d个节点",i); pnew=(PNODE)malloc(sizeof(NODE)); printf("请输入学生的数据(学号 姓名 成绩)\n"); scanf("%d %s %d",&(pnew->num),pnew->name,&pnew->score); if(phead==NULL){pnew->pnext=NULL;}else{pnew->pnext=phead;}phead=pnew; } return phead; }//尾插法建立单链表PNODE createlist2(PNODE phead){int i,n; PNODE ptail=NULL,pnew=NULL;printf("请输入你想创建的节点数\n"); scanf("%d",&n); if(n==0)return phead;//尾插法建立节点 for(i=1;i<n+1;i++) { printf("创建第%d个节点",i); pnew=(PNODE)malloc(sizeof(NODE)); printf("请输入学生的数据(学号 姓名 成绩)\n"); scanf("%d %s %d",&(pnew->num),pnew->name,&pnew->score);pnew->pnext=NULL; if(phead==NULL){phead=pnew;}else{ptail->pnext=pnew;}ptail=pnew; } return phead; }//尾插法建表的改进算法思想:设头结点,使第一个结点和其余结点的插入操作一致。头结点的数据域:可有可无.建议采用该种写法/*PNODE createlist2(PNODE phead){int i,n; PNODE ptail=NULL,pnew=NULL;printf("请输入你想创建的节点数\n"); scanf("%d",&n); if(n==0)return phead;//建立头结点phead=(PNODE)malloc(sizeof(NODE));ptail=phead;phead->pnext=NULL;//尾插法建立节点 for(i=1;i<n+1;i++) { printf("创建第%d个节点",i); pnew=(PNODE)malloc(sizeof(NODE)); printf("请输入学生的数据(学号 姓名 成绩)\n"); scanf("%d %s %d",&(pnew->num),pnew->name,&pnew->score);pnew->pnext=NULL;ptail->pnext=pnew;ptail=pnew; } return phead; }*/void traverselist(PNODE phead) { PNODE pcurrent; if(isempty(phead)) { return; } pcurrent=phead; printf("学号\t\t姓名\t\t成绩\n"); while(pcurrent!=NULL) { printf("%d\t\t%s\t\t%d\n",pcurrent->num,pcurrent->name,pcurrent->score); pcurrent=pcurrent->pnext; } } void findvalue(PNODE phead){int i;PNODE pcurrent;if(isempty(phead)){return;}pcurrent=phead;printf("输入你要查找的学生的学号:\n");scanf("%d",&i);while(pcurrent!=NULL){if(pcurrent->num==i){printf("查找到该学生的信息!\n");printf("学号\t\t姓名\t\t成绩\n");printf("%d\t\t%s\t\t%d\n",pcurrent->num,pcurrent->name,pcurrent->score);return;}pcurrent=pcurrent->pnext;}printf("没有找到该学生的信息!\n");}//后插法PNODE insert1(PNODE phead){int i;PNODE pcurrent=phead,pnew;printf("你想要在哪个学号之后插入新的学生信息?\n");scanf("%d",&i);if(isempty(phead)){return phead;}while(pcurrent!=NULL){if(pcurrent->num==i){pnew=(PNODE)malloc(sizeof(NODE));printf("输入新学生的信息:\n");scanf("%d %s %d",&(pnew->num),pnew->name,&pnew->score);pnew->pnext=pcurrent->pnext;pcurrent->pnext=pnew;printf("新学生信息插入成功!\n");return phead;}pcurrent=pcurrent->pnext;}printf("找不到该学号!\n");return phead;}//前插法PNODE insert2(PNODE phead){int i;PNODE pcurrent=phead,pfront=phead,pnew;printf("你想要在哪个学号之前插入新的学生信息?\n");scanf("%d",&i);if(isempty(phead)){return phead;}while(pcurrent!=NULL){if(pcurrent->num==i){pnew=(PNODE)malloc(sizeof(NODE));printf("输入新学生的信息:\n");scanf("%d %s %d",&(pnew->num),pnew->name,&pnew->score);if(pcurrent==phead){pnew->pnext=pcurrent;phead=pnew;}else{pfront->pnext=pnew;pnew->pnext=pcurrent;}printf("新学生信息插入成功!\n");return phead;}pfront=pcurrent;pcurrent=pcurrent->pnext;}printf("找不到该学号!\n");return phead;}//改进的前插算法,后插算法时间复杂度为O(1),而前插算法为O(n),改进后可在p之后先插入新结点s,然后交换*s和*p的值。PNODE insert3(PNODE phead){int i;PNODE pcurrent=phead,pnew;int tnum,tscore;//用于交换信息的临时变量char tname[10];//用于交换信息的临时变量printf("你想要在哪个学号之前插入新的学生信息?\n");scanf("%d",&i);if(isempty(phead)){return phead;}while(pcurrent!=NULL){if(pcurrent->num==i){pnew=(PNODE)malloc(sizeof(NODE));printf("输入新学生的信息:\n");scanf("%d %s %d",&pnew->num,pnew->name,&pnew->score);pnew->pnext=pcurrent->pnext;pcurrent->pnext=pnew;//交换*s和*p的值tnum=pcurrent->num;pcurrent->num=pnew->num;pnew->num=tnum;tscore=pcurrent->score;pcurrent->score=pnew->score;pnew->score=tscore;strcpy(tname,pcurrent->name);strcpy(pcurrent->name,pnew->name);strcpy(pnew->name,tname);printf("新学生信息插入成功!\n");return phead;}pcurrent=pcurrent->pnext;}printf("找不到该学号!\n");return phead;} PNODE deletelist(PNODE phead) { int i; PNODE pcurrent,pfront; pcurrent=pfront=phead; if(isempty(phead)){return phead;}printf("输入你要删除的学生的学号:\n");scanf("%d",&i); while(pcurrent!=NULL) { if(pcurrent->num==i) { if(pcurrent==phead){phead=phead->pnext;free(pcurrent);printf("删除成功!\n");return phead;}else{pfront->pnext=pcurrent->pnext;free(pcurrent);printf("删除成功!\n");return phead;} } pfront=pcurrent;pcurrent=pcurrent->pnext; } printf("没有找到对应的学号!\n");return phead;} void changelist(PNODE phead){int i;PNODE pcurrent=phead;if(isempty(phead)){return;}printf("输入你要修改信息的学生的学号:\n");scanf("%d",&i);while(pcurrent!=NULL){if(pcurrent->num==i){printf("输入修改学生的新的学生信息(姓名 成绩):\n");scanf("%s %d",pcurrent->name,&pcurrent->score);printf("学生信息修改成功!\n");return;}pcurrent=pcurrent->pnext;}}