数据结构课程设计---学生信息管理系统
生活随笔
收集整理的這篇文章主要介紹了
数据结构课程设计---学生信息管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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的后面插入一個學生信息;
總結
以上是生活随笔為你收集整理的数据结构课程设计---学生信息管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构课程设计---最长公共子串
- 下一篇: java信息管理系统总结_java实现科