合并链表的问题
合并两个链表,为什么第二次输入链表后输出的是空表?是不是第二次调用ininlise后有那个内存释放问题?新手不懂,各位大哥来个提示吧。。呵呵,谢谢。。。#include<stdio.h>
#include<malloc.h>
#define len sizeof(struct list)
#define null 0
struct list
{
int num;
struct list *next;
};
int n=0;
struct list *initlist(void)
{
struct list *p1,*p2,*head;
p1=p2=(struct list*)malloc(len);
head=null;
printf(" qing shu ru:");
scanf("%d",&p1->num);
while(p1->num!=0)
{
n++;
if (n==1)
head=p1;
else
{
p2->next=p1;
p2=p1;
p1=(struct list*)malloc(len);
printf("qing shu ru:");
scanf("%d",&p1->num);
}
p2->next=null;
}
return head;
}
void print(struct list *head)
{
struct list *p;
p=head;
if (p==null)
printf("空表");
else
while(p!=0)
{
printf("num is :%d",p->num);
p=p->next;
}
}
struct list *L(struct list *l1,struct list *l2)
{
struct list *p1,*p2,*p3,*head;
p1=l1,p2=l2;
p3=l1;
head=p3;
while(p1!=0&&p2!=0)
{
if(p1->num<p2->num)
{
p3->next=p1;
p1=p1->next;
}
else
{
p3->next=p2;
p2=p2->next;
}
}
if(p1!=0)
{
p3->next=p2;
}
else
{
p3->next=p1;
}
}
void main()
{
struct list *l1,*l2,*l3;
l1=initlist();
print(l1);
l2=initlist();
print(l2);
l3=L(l1,l2);
print(l3);
}