typedef struct student{
int flag;
int score;
student * next;
} student;
student * create(){//初始化单链表 flag=0 结束 不存储此数据
student * head,*p1,*p2;
p1=(student*)malloc(LEN);
head=p1;
p2=(student*)malloc(LEN);
while (1){
scanf("%d%d",&p1->flag,&p1->score);
if (p1->flag==0) break;
else{
p2->next=p1;
p2=p1;
p1=(student*)malloc(LEN);
}
}
p2->next = NULL;
return head;
}
student * insert(student* head,int score){//从小到大插入
student * p = (student *)malloc(LEN);
p->score=score,p->flag=2;
student *p1=head,*p2;
while ((p1->score<score) && (p1->next)){
p2=p1;
p1=p1->next;
}
if (p1->score>=score){
if (p1==head){
head=p;
}else{
p2->next=p;
}
p->next=p1;
}else{
p1->next=p;
p->next=NULL;
}
return head;
}
student * del(student * head,int score){
student * p = head,*p1;
if (p->score == score){
head = p->next;
}else{
p1 = p;
p = p->next;
while (p){
if (p->score == score){
p1->next = p->next;
break;
}
p1 = p;
p = p->next;
}
}
!p?printf("%d was not found!\n",score):printf("%d was deleted!\n",score);
return head;
}
student * alter(student * head,int count,int score){
student * p = head;
int n=0,pre_num;
while (p){
++n;
if (count==n){
pre_num = p->score;
p->score=score;
break;
}
p = p->next;
}
!p?printf("%d: %d alter fails!\n",count,score):printf("%d ---> %d was finished!\n",pre_num,score);
return head;
}
void display(student * head){
student * p=head;
int n=0;
while (p){
printf("%d: %d\n",++n,p->score);
p=p->next;
}
}
int main(void) {
student * head = create();
head=insert(head,500);
head=insert(head,720);
head=insert(head,900);
display(head);
head = del(head,23);
display(head);
head = alter(head,3,9993);
display(head);
return 0;
}