数据结构课程设计---学生信息管理系统
生活随笔
收集整理的这篇文章主要介绍了
数据结构课程设计---学生信息管理系统
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1、 建立一个动态链表,链表中每一结点包括:学号、姓名、性别、年龄、成绩。程序能实现以下功能:
建立链表
显示链表
查找链表中是否存在某个元素,并显示这个元素的所有信息,若没有这个元素则显示“无此记录!”的信息。
删除链表中指定学号的结点。
在链表中指定的位置插入一个新结点(学号不能和其他结点重复)。
要求:程序运行中,先显示实现以上功能所构成的菜单,然后根据选项调用相应程序及显示其对应的结果,然后再显示菜单程序,直到按“退出”选项,程序执行结束。
完整的代码如下:
#include "stdio.h" #include "stdlib.h" typedef struct student {int id; //学号char name[20]; //姓名char sex; //性别(f或m)int age; //年龄int score; //成绩struct student *next; }student; student *head=NULL; int length; //链表的长度 void create() {student *p1,*p2;length=0;p1=(student *)malloc(sizeof(student));p1->id=-1;if(head==NULL)head=p1;printf("请输入学生的学号、姓名、性别、年龄、成绩信息:\n");while(1) //学号为0的时候退出{p2=(student *)malloc(sizeof(student));scanf("%d %s %c %d %d",&p2->id,p2->name,&p2->sex,&p2->age,&p2->score); //输入学生信息if(p2->id==0){printf("链表创建完成!\n");break;}length++; //链表的长度p1->next=p2;p2->next=NULL;p1=p1->next;}return ; }void display() {student *p=head->next;printf("链表中所有的学生信息如下:\n");while(p!=NULL){printf("%d %s %c %d %d\n",p->id,p->name,p->sex,p->age,p->score);p=p->next;}return ; } void search() {int num;student *p=head->next;printf("需要查找的学生学号为:");scanf("%d",&num);while(p!=NULL){if(p->id==num){printf("学号为%d的学生的信息如下:\n",num);printf("%d %s %c %d %d\n",p->id,p->name,p->sex,p->age,p->score);return;} p=p->next;}if(p==NULL)printf("无此记录!\n");return ; }void insert() {int num,i;student *p,*q;p=head;printf("请输入你要插入位置: ");scanf("%d",&num);if(num>length){printf("找不到要插入的位置\n");return ;}else{printf("请输入你要插入的学生的学号、姓名、性别、年龄、成绩信息:\n");q=(student *)malloc(sizeof(student));scanf("%d %s %c %d %d",&q->id,q->name,&q->sex,&q->age,&q->score);while(p!=NULL){if(p->id==q->id){printf("该学号已经存在,无法插入!\n");return ;}p=p->next;}p=head;for(i=0;i<num;i++)p=p->next;q->next=p->next;p->next=q;length++;printf("插入成功!\n");return ;} } void Delete() {int num;student *p,*q;q=head,p=head->next;printf("请输入要删除的学生的学号:\n");scanf("%d",&num);while(p!=NULL){if(p->id==num){q->next=p->next;free(p);length--;printf("删除成功!\n");return ;}p=p->next;q=q->next;}if(p==NULL){printf("找不到要删除的编号!\n");return ;} } void menu() {printf("________________________________________________________________\n");printf("| 学生信息管理系统 |\n");printf("| 0、 退出系统 |\n");printf("| 1、 建立链表 |\n");printf("| 2、 显示链表 |\n");printf("| 3、 查找链表中的某个元素 |\n");printf("| 4、 删除链表中指定学号的结点 |\n");printf("| 5、 指定的位置上插入一个新结点 |\n");printf("________________________________________________________________\n");return ; } int main(void) {int a;menu();while(1){printf("请选择相应的功能:");scanf("%d",&a);switch(a){case 0:return 0;case 1:create();menu();break;case 2:if(head){display();menu();}else{printf("链表为空,请先建立链表!\n");menu();}break;case 3:if(head){search();menu();}else{printf("链表为空,请先建立链表!\n");menu();}break;case 4:if(head){Delete();menu();}else{printf("链表为空,请先建立链表!\n");menu();}break;case 5:if(head){insert();menu();}else{printf("链表为空,请先建立链表!\n");menu();}break;default:break;}}system("pause");return 0; }程序说明:加入已经加入了4个学生信息head->liuwei->zhanghua->lina->liuxiang,链表的长度为4,插入的时候,输入4,将会在liuxiang的后面插入一个学生信息;输入1,将会在liuwei的后面插入一个学生信息;
总结
以上是生活随笔为你收集整理的数据结构课程设计---学生信息管理系统的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 数据结构课程设计---最长公共子串
- 下一篇: XP或Win7系统下grub4dos安装