#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct st
{
char name[20];
int english;
int math;
int chinese;
int average;
st *next;
};
struct st *pend=NULL;//初始链表的尾指针
struct st *pendorder=NULL;//顺序链表的尾指针
struct st *pheadorder=NULL;//顺序链表的头指针
struct st *makeorder(struct st *phead);//按分数从大到小排序 生产链表
struct st *addtolist(struct st *add);// 将平均分最大的添到另一个链表
struct st *createlist();//输入学生信息时生成的初始链表
struct st * deletestu(char *name,st *phead);//删除一个学员的信息
struct st *addstu(st *name,st *phead);//向顺序链表添加一个元素,插入的地方按平均成绩
void printinfo(st *phead);//按平均成绩打印出每个学员的名字
int main()
{
int select;
char deletename[20];
struct st *addstud=NULL;
struct st *phead=NULL;
phead=createlist();//输入时创建链表
pheadorder=makeorder(phead);//将链表排序
printf("input operation:1----deletestudent,2-----addstudent,3----output all student\n");
scanf("%d",&select);
while(select>0)//选择操作1为删除2为添加3为打印,其他的输入会跳出循环
{
switch(select)
{
case 1:
printf("please input the of the student to be deleted:\n");
scanf("%s",deletename);
pheadorder=deletestu(deletename,pheadorder);
printf("input operation:1----deletestudent,2-----addstudent,3----output all student\n");
scanf("%d",&select);
break;
case 2:
printf("please input the information of the student to be added:\n");
addstud=new st;
scanf("%s%d%d%d",addstud->name,&(addstud->english),&(addstud->math),&(addstud->chinese));
addstud->average=((addstud->english)+(addstud->math)+(addstud->chinese))/3;
while((addstud->english)<=0)
{
delete addstud;
printf("please input the information of the student to be added:\n");
addstud=new st;
scanf("%s%d%d%d",addstud->name,&(addstud->english),&(addstud->math),&(addstud->chinese));
addstud->average=((addstud->english)+(addstud->math)+(addstud->chinese))/3;
}
pheadorder=addstu(addstud,pheadorder);
printf("input operation:1----deletestudent,2-----addstudent,3----output all student\n");
scanf("%d",&select);
break;
case 3:
printinfo(pheadorder);
printf("input operation:1----deletestudent,2-----addstudent,3----output all student\n");
scanf("%d",&select);
break;
default:
goto laber;
}
}
laber:system("pause");
return 1;
}
struct st *createlist()//输入时创建初始链表
{
struct st *pfirst=NULL;
struct st *plast=NULL;
struct st *p=new st;
printf("please input the information of the students:\n");
scanf("%s%d%d%d",p->name,&(p->english),&(p->math),&(p->chinese));
p->average=((p->english)+(p->math)+(p->chinese))/3;
while((p->english)>0)
{
if(pfirst==NULL)
pfirst=plast=p;
else
plast->next=p;
plast=p;
printf("please input again:\n");
p=new st;
scanf("%s%d%d%d",p->name,&(p->english),&(p->math),&(p->chinese));
p->average=((p->english)+(p->math)+(p->chinese))/3;
}
plast->next=NULL;
printf("list create successful\n");
delete p;
return pfirst;
}
struct st *deletestu(char *name,st *phead)//删除一个学员
{
int flag=0;
st *p=NULL;
if(strcmp(phead->name,name)==0)
{
phead=phead->next;
flag=1;
}
else
for(p=phead;p;p=p->next)
{
if(strcmp(p->next->name,name)==0)
{
p->next=p->next->next;
flag=1;
break;
}
}
if(!flag)
printf("the student you delete is not in the list\n");
else printf("delete successful\n");
return phead;
}
struct st *addstu(st *name,st *phead)//按平均分增加一个学员
{
name->next=NULL;
struct st *p=NULL;
if((name->average)>(phead->average))
{
name->next=phead;
phead=name;
return phead;
}
else
{
for(p=phead;p->next;p=p->next)
{
if((name->average)>(p->next->average))
{
name->next=p->next;
p->next=name;
return phead;
}
}
}
p=p->next;
p->next=name;
return phead;
}
void printinfo(st *phead)//打印信息
{
st *p;
for(p=phead;p;p=p->next)
printf("%s\n",p->name);
}
struct st *addtolist(struct st *phead,struct st *add)//生成顺序链表时每回都添加一个平均成绩最高的学员信息
{
add->next=NULL;
if(phead==NULL)
pendorder=phead=add;
else
pendorder->next=add;
pendorder=add;
return phead;
}
struct st *makeorder(struct st *phead)//将初始链表变成顺序链表
{
if(phead!=NULL)
{
int max;
struct st *p=NULL;
struct st *index=NULL;
while(phead)
{
max=0;
for(p=phead;p;p=p->next)
{
if(p->average>max)
{
max=p->average;
index=p;
}
}
phead=deletestu(index->name,phead);
pheadorder=addtolist(pheadorder,index);
}
return pheadorder;
}
else printf("there is no list members to be ordered\n");
return pheadorder;
}