请帮我看看哪错了?(急)

来源:百度知道 编辑:UC知道 时间:2024/06/24 18:09:07
我想先做一个动态链表,然后进行插入.
不知为什么,只能插入第一条,以后在插入就显示以存在了.输出的结果中新插入的那条号码也不对.
这里是源代码:
http://hi.baidu.com/%C7%E0%C9%BD%C1%F7%CB%AE/blog/item/1476cb808832ecd29123d97d.html

改了,你要每次都为new_node分配空间

/*插入函数,就是这个函数有问题*/
student * insert(student *head)
{student *p1,*p2,*new_node;
int j;

for(;;)
{
new_node=(student *)malloc(LEN);

if(new_node==NULL)
{printf("Database is full,can't add more record.\n");
return NULL;
}
printf("Please enter the number you want to insert(enter 0 to end):\n");
scanf("%ld",&new_node->num);
if(new_node->num==0) {break;}
for(p1=head,p2=NULL;
p1!=NULL&&(new_node->num)>(p1->num);
p2=p1,p1=p1->next)
;
if(p1!=NULL&&(p1->num)==(new_node->num))
{char c;
printf("This number already exists:\n");
print_title();
print(p1);
printf("Cover or not?(y/n)?");
for(;;)
{
c=getchar();
if(c=='n'||c=='N')