谭浩强《C程序设计第二版》中11.7.8节(P286)中的链表综合操作怎么调都有错程序能运行但是结果是错误的,在此求救各位了马上就要交作业了先谢谢大家:)
运行的时候老是出现这个问题:"scanf:floating point formats not linked Abnormal program termination"
附程序如下:
#include<malloc.h>
#define null 0
#define len sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(len);
scanf("%ld,%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("%ld,%f",&p1->num,&p1->score);
}
p2->next=null;
return(head);
}
void print(struct student *head)
{struct student *p;
printf("\nnow,these %d records are:",n);
p=head;
if(head!=null)
do
{printf("%ld%5.lf\n",p->num,p->score);
p=p->next;
}while(p!=null);
}
struct student *insert(struct student *head,struct student *stud)
{struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==null)
{head=p0;p0->next=null;}
else
{while((p0->num>p1->num)&&(p1->next!=null))
{p2=p1;
p1=p1->next;}
if(p0->num<=p1->num)
{if(head==p1)head=p0;
else p2->next=p0;
p0->next=p1;}
else
{p1->next=p0;p0->next=null;}}
n=n+1;
return(head);
}
struct student *del(struct student *head,long num)
{struct student *p1,*p2;
if(head==null) {printf("\nlist null!\n");goto end;}
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;
printf("delete:%d\n",num);
n=n-1;
}
else printf("%ld not been found!\n",num);
end:return(head);
}
main()
{
struct student *head,*stu;
long del_num;
printf("input records:\n");
head=creat();
print(head);
printf("\ninput the delete number:");
scanf("%ld",&del_num);
while(del_num!=0)
{head=del(head,del_num);
print(head);
printf("input the delete number:");
scanf("%ld",&del_num);}
printf("\ninput the inserted record:");
stu=(struct student*)malloc(len);
scanf("%ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
{head=insert(head,stu);
print(head);
printf("input the inserted record:");
stu=(struct student*)malloc(len);
scanf("%ld,%f",&stu->num,&stu->score);
}
}
运行结果应该是这样的:
input records:
99101,99↓
99103,87↓
99105,77↓
0,0↓
Now,These 3 records are:
99101 99.0
99103 87.0
99105 77.0
input the deleted number:99103↓
delete:99103
Now,These 2 records are:
99101 99.0
99105 77.0
input the deleted number:99105↓
delete:99105
Now,These 1 records are :
99101 90.0
input the deleted number:0↓
input the inserted record:99104,87↓
Now,These 2 records are:
99101 99.0
99104 87.0
input the inserted record:99106,65↓
Now,These 3 records are:
99101, 99.0
99104,87.0
99106,65.0
input the inserted record:0 ↓
[此贴子已经被作者于2006-11-20 17:36:32编辑过]