C语言使用单链表实现学生信息管理系统

编辑: admin 分类: c#语言 发布时间: 2021-12-12 来源:互联网

本文实例为大家分享了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);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。

【文章原创作者:http://www.yidunidc.com/st.html转发请说明出处】