typedef struct node1
{
int data;
struct node1 *next;
}node;
int copy(node *head1, node *head2)
{
node *p1,*p2,*tmp;
p1 = head1->next; //p1指向链表1的首数据
p2 = head2;
int re=0;
while (p1!=NULL && p2!=NULL)
{
p2->data = p1->data; //逐个复制数据
p1=p1->next;
p2=p2->next;
++re;
}
if (p1==NULL) //head1的长度不长于head2,可能需要清除head2后面的数据
{
tmp=head2;
while(tmp->next !=p2) tmp=tmp->next;
tmp->next=NULL;
while(p2!=NULL)
{
tmp=p2;
p2=p2->next;
free(tmp);
}
}
else //head1的长度大于head2,head2需要追加空间
{
tmp=head2;
while(tmp->next !=p2) tmp=tmp->next;
p2=tmp;
while(p1!=NULL)
{
tmp=(node *)malloc(sizeof(node));
tmp->data=p1->data;
p2->next=tmp;
p2=tmp; //刚才漏了这一句
++re;
p1=p1->next;
}
p2->next=NULL;
}
return re;
};
|