資料結構課程設計—學生資訊管理系統

來源:互聯網
上載者:User

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的後面插入一個學生資訊;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.