#include
#include
#define len sizeof(struct student)
struct student
{int num;
float score;
struct student *next;
};
struct student *creat(void) // 建立
{struct student *p1,*p2,*head;
int n=0;
p1=p2=(struct student *)malloc(len);
scanf("%d %f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(len);
scanf("%d %f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void print(struct student *head) // 输出
{struct student *p;
p=head;
if(head!=NULL)
for(;p!=NULL;)
{printf("%d %f\n",p->num,p->score);
p=p->next;
}}
struct student *del(struct student *head,int num) // 删除
{struct student *p1,*p2=NULL;
if(head=NULL)
printf("空链表,无法操作");
else
{p1=head;
while((num!=p1->num)&&(p1->next!=NULL))
{p2=p1;
p1=p1->next;
}
if(num==p1->num)
{if(p1==head)
head=p1->next;
else
p2->next=p1->next;
free(p1);
printf("此链表已删除");
}
else
printf("无此数据,错误");
}
return head;
}
main()
{struct student *head,*p;
int num;
head=creat();
printf("输入要查找的数据并删除:\n");
scanf("%d",&num);
p=del(head,num);
printf("删除后数据为:");
print(p);
}