#include<stdio.h>
#include<stdlib.h>
#define len sizeof(struct student)
struct student{
long int num;
float score;
struct student *next;
};
int n;
int main()
{
struct student *creat();
struct student *delete(struct student * ,int );
struct student *add(struct student *,int );
struct student *p1,*p2,*p3;
int x1,x2;
printf("输入学生的学号和成绩(中间空格分开):\n");
p1=p2=p3=creat();
do{
printf("\nnum=%ld score=%5.1f\n",p1->num,p1->score);
p1=p1->next;
}while(p1!=NULL);
printf("输入要删除的位数:");
scanf("%d",&x1);
p2=delete(p2,x1);
do{
printf("\nnum=%ld score=%5.1f\n",p2->num,p2->score);
p2=p2->next;
}while(p2!=NULL);
printf("输入要增添的位数:");
scanf("%d",&x2);
p3=add(p3,x2);
do{
printf("\nnum=%ld score=%5.1f\n",p3->num,p3->score);
p3=p3->next;
}while(p3!=NULL);
return 0;
}
struct student *creat()
{
struct student *head,*p1,*p2;
head=NULL;
n=0;
p1=p2=malloc(len);
scanf("%ld %f",&p1->num,&p1->score);
while(p1->num!=0){
n++;
if(n==1){
head=p1;
}else{
p2->next=p1;
}
p2=p1;
p1=malloc(len);
scanf("%ld %f",&p1->num,&p1->score);
}
p2->next=NULL;
return (head);
}
struct student *delete(struct student *p1,int x)
{
struct student *p2,*head;
head=p2=p1;
n=0;
do{
n++;
p1=p1->next;
if(n>2){
p2=p2->next;
}
if(x==1){
head=p1;
break;
}else if(x==n){
p2->next=p1;
break;
}
}while(1);
return head;
}
struct student *add(struct student *p1,int x)
{
struct student *p2,*p3,*head;
head=p2=p1;
n=0;
p3=malloc(len);
printf("/n输入要插入的学号与成绩:\n");
scanf("%ld %f",&p3->num,&p3->score);
n=1;
do{
n++;
p1=p1->next;
if(n!=2){
p2=p2->next;
}
if(x==1){
head=p3;
p3->next=p1;
break;
}else if(x==n){
p2->next=p3;
p3->next=p1;
break;
}
}while(1);
return head;
}