居然忘了,为了调试上贴,我专门编了个。
#include
#include
struct student
{
int num;
float score;
struct student *next;
};
int n=0;
struct student del(struct student **head,int num)
{
struct student *p1,*p2,*temp;
int flag=0;
if(*head==NULL)
{
printf("\nThis list is null!\n");
return *p1;
}
p1=*head; //为什么这里是p1指向head而不是head指向p1
loop: while(p1->num!=num && p1->next!=NULL)
{
p2=p1; //
p1=p1->next; //
}
if(p1->num==num)
{
if(p1==*head)
{
*head=p1->next;
temp=p1;
p1=p1->next;
free(temp);
}
else
{
p2->next=p1->next;
temp=p1;
p1=p2->next;
free(temp);
}
printf("\nDeleat NO:%d succesed!\n",num);
n=n-1;
flag=1;
}
if(p1!=NULL)goto loop;
if(!flag)
{
printf("%d not been found!\n",num);
}
}
void Insert(struct student **head)
{
int num;
float score;
struct student *p1,*r;
printf("请输入学号,成绩\n");
scanf("%d%f",&num,&score);
r= (struct student *)malloc(sizeof(struct student));
if(*head==NULL)
p1=*head=r;
else
{
for(p1=*head;p1->next!=NULL;p1=p1->next);
p1->next=r;
}
r->num=num;
r->score=score;
r->next=NULL;
n++;
}
void show(struct student *head)
{
struct student *p1;
if(head==NULL)
return;
else
{
for(p1=head;p1!=NULL;p1=p1->next)
printf("%d %f\n",p1->num,p1->score);
}
}
main()
{
struct student *head;
int a,num;
head=NULL;
while(scanf("%d",&a))
{
printf("请输入选项:(1添加。2删除。3显示)");
if(a==1)
Insert(&head);
else if(a==2)
{
printf("输入学号\n");
scanf("%d",&num);
del(&head,num);
}
else if(a==3)
show(head);
}
}