#include<stdio.h>
#include<stdlib.h>
#include"zzy.h"
int main(int argc, const char *argv[])
{
int m;
zzy *l=create();
if(NULL==l)
{
return -2;
}
printf("创建成功\n");
while(1)
{
printf("-----学生管理系统-----");
printf("1.输入学生信息");
printf("2.按成绩查找学生");
printf("3.删除学生");
printf("4.按学生成绩排序");
printf("5.退出");
printf("6.");
scanf("%d",&m);
datatype1 name1[20];
datatype score1;
int n,i;
switch(m)
{
case 1:
printf("输入几位学生信息:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("请输入学生信息:");
scanf("%s%d",name1,&score1);
put(l,score1,name1);
}
bian(l);
break;
case 2:
printf("请输入查找的学生成绩:");
scanf("%d",&score1);
chazhao(l,score1);
break;
case 3:
printf("请输入想删除的学生的学生成绩:");
scanf("%d",&score1);
shang(l,score1);
bian(l);
break;
case 4:
paixu(l);
bian(l);
break;
case 5:exit(0);
}
}
return 0;
}
#ifndef __ZZY_H__
#define __ZZY_H__
typedef int datatype;
typedef char datatype1;
typedef struct node
{
datatype1 name[20];
datatype score;
int len;
struct node *s;
}zzy;
zzy *create();
int empty(zzy *l);
void bian(zzy *l);
int put(zzy *l,datatype score,datatype1 name[20]);
int chazhao(zzy *l,datatype score);
int shang(zzy *l,datatype score);
void paixu(zzy *l);
#endif
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"zzy.h"
zzy *create()
{
zzy *l=(zzy*)malloc(sizeof(zzy));
if(NULL==l)
{
printf("申请失败");
return NULL;
}
l->s=NULL;
l->len=0;
l->score=0;
printf("申请成功");
return l;
}
int empty(zzy *l)
{
if(NULL==l)
{
printf("表不合法");
return -1;
}
return l->s==NULL ? 1:0;
}
void bian(zzy *l)
{
if(NULL==l || empty(l))
{
printf("表空,遍历失败");
return;
}
printf("表中的元素为:");
zzy *q=l;
while(q->s != NULL)
{
printf("%s%d\t",q->s->name,q->s->score);
q=q->s;
}
printf("\n");
}
int put(zzy *l,datatype score1,datatype1 name1[20])
{
if(NULL==l)
{
printf("链表不合法\n");
return -3;
}
zzy *p=(zzy*)malloc(sizeof(zzy));
strcpy(p->name,name1);
p->score=score1;
zzy *q=l;
while(q->s != NULL)
{
q=q->s;
}
q->s=p;
l->len++;
printf("添加成功\n");
return 0;
}
int chazhao(zzy *l,datatype score1)
{
if(NULL==l)
{
printf("链表为空");
return -4;
}
zzy *q=l->s;
if(q!=NULL)
{
if(q->score==score1)
{
printf("%s%d",q->name,q->score);
}
q=q->s;
}
}
int shang(zzy *l,datatype score1)
{
if(NULL==l || empty(l))
{
printf("链表为空");
return -5;
}
zzy *q=l;
while(q->s!=NULL)
{
if(q->s->score==score1)
{
zzy *p=q->s;
q->s=p->s;
free(p);
p=NULL;
l->len--;
}
q=q->s;
}
return 0;
}
void paixu(zzy *l)
{
if(NULL==l || empty(l))
{
printf("链表为空");
return;
}
datatype temp;
datatype1 name2[20];
zzy *q=l->s;
for(int i=1;i<l->len;i++)
{
zzy *p=l->s->s;
for(int a=1;a<i;a++)
{
p=p->s;
}
for(int k=1;k<=l->len-i;k++)
{
if(q->score<p->score)
{
temp=q->score;
q->score=p->score;
p->score=temp;
strcpy(name2,q->name);
strcpy(q->name,p->name);
strcpy(p->name,name2);
}
p=p->s;
}
q=q->s;
free(p);
p=NULL;
}
printf("排序成功");
}