C語言使用單鏈表實現學生信息管理系統

本文實例為大傢分享瞭C語言使用單鏈表實現學生信息管理系統,供大傢參考,具體內容如下

初學數據結構,記錄一下學習過程。

運行結果如圖:

1.運行界面

2.錄入學生信息

3.按照總分進行排序

代碼如下:

#define ERROR 0
#define OK 1
#define OVERFLOW -1;
typedef int ElemType;
typedef int Status;
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<iostream>
typedef struct{
 char name[20];
 char snumber[20];
 char sex[10];
 int math;
 int chinese;
 int english;
}student;
typedef struct LNode{
 student data;
 struct LNode *next;
}LNode,*LinkList;
//單鏈表初始化
Status InitList(LinkList &L){
 L = new LNode;
 L->next = NULL;
 return OK;
}
//創建單鏈表
void CreateList(LinkList &L,int n){
 LinkList r;
 LinkList p;
 L = new LNode;
 L->next = NULL;
 r = L;
 int i;
 for(i = 0;i<n;i++){
  p = new LNode;
  scanf("%s%s%s%d%d%d",&p->data.name,&p->data.snumber,&p->data.sex,&p->data.chinese,&p->data.math,&p->data.english);
  //scanf("%d%d%d",&p->data.chinese,&p->data.math,&p->data.english);
  p->next=NULL;
  r->next=p;
  r = p;
 }
}


//總分統計
int GradeSum(LinkList L){
 int sum;
 sum = (L->data.chinese)+(L->data.english)+(L->data.math);
 return sum;
}
//統計所有學生信息
void BianLi(LinkList L){
 LinkList p;
 p = L->next;
 printf("姓名\t學號\t性別\t語文\t數學\t英語\t總分\n");
 while(p){
  printf("%s\t%s\t%s\t",p->data.name,p->data.snumber,p->data.sex);
  printf("%d\t%d\t%d\t%d",p->data.chinese,p->data.math,p->data.english,GradeSum(p));
  p = p->next;
  printf("\n");
 }
}
//按照總分排序
void OrderSum(LinkList &L){
 LinkList p,q,tail;

 tail = NULL;

 while((L->next->next) != tail)
 {
  p = L;
  q = L->next;
  while(q->next != tail)
  {
   if( GradeSum(q) <  GradeSum(q->next))
   {
    p->next = q->next;
    q->next = q->next->next;
    p->next->next = q;
    q = p->next;
   }
   q = q->next;
   p = p->next;
  }
  tail = q;
 }
 printf("排序完畢!\n");

}
//根據語文進行排序
void OrderChinese(LinkList &L){
 LinkList p,q,tail;

 tail = NULL;

 while((L->next->next) != tail)
 {
  p = L;
  q = L->next;
  while(q->next != tail)
  {
   if((q->data.chinese) < (q->next->data.chinese))
   {
    p->next = q->next;
    q->next = q->next->next;
    p->next->next = q;
    q = p->next;
   }
   q = q->next;
   p = p->next;
  }
  tail = q;
 }
 printf("排序完畢!\n");

}
//根據英語進行排序
void OrderEnglish(LinkList &L){
 LinkList p,q,tail;

 tail = NULL;

 while((L->next->next) != tail)
 {
  p = L;
  q = L->next;
  while(q->next != tail)
  {
   if((q->data.english) < (q->next->data.english))
   {
    p->next = q->next;
    q->next = q->next->next;
    p->next->next = q;
    q = p->next;
   }
   q = q->next;
   p = p->next;
  }
  tail = q;
 }
 printf("排序完畢!\n");

}
//根據數學進行排序
void OrderMath(LinkList &L){
 LinkList p,q,tail;

 tail = NULL;

 while((L->next->next) != tail)
 {
  p = L;
  q = L->next;
  while(q->next != tail)
  {
   if((q->data.math) < (q->next->data.math))
   {
    p->next = q->next;
    q->next = q->next->next;
    p->next->next = q;
    q = p->next;
   }
   q = q->next;
   p = p->next;
  }
  tail = q;
 }
 printf("排序完畢!\n");

}
//刪除學生信息
Status ListDelete(LinkList &L,int i){
 LinkList p;
 LinkList q;
 int j;
 p = L;j = 0;
 while((p->next)&&(j<i-1)){
  p = p->next;
  ++j;
 }
 if(!(p->next)||(j>i-1)) return ERROR;
 q = p->next;
 p->next = q->next;
 delete q;
 return OK;
}
void DeleteStudent(LinkList &L){
 int i;
 printf("請輸入需要刪除的學生序號\n");
 scanf("%d",&i);
 ListDelete(L,i);
 printf("刪除完畢!\n");

}
//插入學生信息
Status ListInsert(LinkList &L,int i,char name[],char snumber[],char sex[],int chinese,int math,int english){
 LinkList p = L;
 LinkList s;
 int j = 0;
 while(p && (j<i-1)){
  p = p->next;
  ++j;
 }
 if(!p||j>i-1) return ERROR;
 s = new LNode;
 strcpy(s->data.name,name);
 strcpy(s->data.snumber,snumber);
 strcpy(s->data.sex,sex);
 s->data.chinese = chinese;
 s->data.math = math;
 s->data.english = english;
 s->next = p->next;
 p->next = s;
 return OK;
}
void InsertStudent(LinkList &L){
 int n;
 char name[20],snumber[20],sex[10];
 int chinese,math,english;
 printf("請輸入插入的位置N:");
 scanf("%d",&n);
 printf("請依次輸入學生的姓名、學號、性別、語文成績、數學成績、英語成績、總分\n");
 scanf("%s%s%s%d%d%d",&name,&snumber,&sex,&chinese,&math,&english);
  ListInsert(L,n,name,snumber,sex,chinese,math,english);

}
void menu(){
 printf("\t\t\t  ======================================================\n\n");
 printf("\t\t\t ‖                學生信息管理系統                    ‖ \n\n");
 printf("\t\t\t  ======================================================\n\n"); 
 printf("\t\t\t  ===================系統功能菜單=======================\n");
 printf("\t\t\t ‖  ------------------------------------------------  ‖\n");
 printf("\t\t\t ‖  ------------------------------------------------  ‖ \n");
 printf("\t\t\t  ======================================================  \n");
 printf("\t\t\t ‖    0.錄入學生信息      ‖ ‖  1.統計學生信息       ‖     \n");
 printf("\t\t\t  ======================================================\n"); 
 printf("\t\t\t ‖    2.按照總分排序      ‖ ‖  3.按照語文成績排序   ‖     \n");
 printf("\t\t\t  ====================================================== \n");
 printf("\t\t\t ‖    4.按照數學成績排序  ‖ ‖  5.按英語成績排序     ‖    \n");
 printf("\t\t\t  ======================================================\n");
 printf("\t\t\t ‖    6.刪除學生信息      ‖ ‖  7.插入學生信息       ‖    \n");
 printf("\t\t\t  ====================================================== \n");
 printf("\t\t\t ‖    8.退出系統          ‖         \n");
 printf("\t\t\t  ==========================     \n");
 printf("\t\t\t     -----------------------------------------------  \n");
 printf("\n\n\n");
}
void luRu(LinkList &L){
 int n;
 printf("請輸入學生數目N:");
 scanf("%d",&n);
 printf("請依次輸入學生的姓名、學號、性別、語文成績、數學成績、英語成績、總分\n");
 CreateList(L,n);
 printf("學生信息已錄入完畢!\n");
}
void main(int args,char argv[]){
 LinkList p;
 InitList(p);
 int i;
 do{
  menu();
  scanf("%d",&i);
  getchar();
  switch(i){
  case 0:luRu(p);break;
  case 1:BianLi(p);break;
  case 2:OrderSum(p);BianLi(p);break;
  case 3:OrderChinese(p);BianLi(p);break;
  case 4:OrderMath(p);BianLi(p);break;
  case 5:OrderEnglish(p);BianLi(p);break;
  case 6: DeleteStudent(p);break;
  case 7:InsertStudent(p);break;
  case 8:exit(0);
  }
 }while(i != -1);
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: