#include"stdio.h"
#include"math.h"
typedef struct linknode
{
int data;
struct linknode *next;
}node;
node *create(A)
node *A;
{
node *r,*s,*p;
A=s=(node *)malloc(sizeof(node));
scanf("%d",&s->data);
r=s;
while(s->data!=0)
{
r->next=s;
r=s;
s=(node *)malloc(sizeof(node));
scanf("%d",&s->data);
}
p=A;
A=A->next;
r->next=NULL;
free p;
do{
printf("%d->",A->data);
A=A->next;
}while(A!=0);
return A;
}
node *create(B)
node *B;
{
node *r,*s,*p;
B=s=(node *)malloc(sizeof(node));
scanf("%d",&s->data);
r=s;
while(s->data!=0)
{
r->next=s;
r=s;
s=(node *)malloc(sizeof(node));
scanf("%d",&s->data);
}
p=B;
B=B->next;
r->next=NULL;
free p;
do{
printf("%d->",B->data);
B=B->next;
}while(B!=0);
return B;
}
node *sum(a,b)
node *a,*b;
{
node *r,*p,*s;
c=(node *)malloc(sizeof(node));
r=c;
while(a!=0)
{
r->data=a->data;
a=a->next;
p=(node *)malloc(sizeof(node));
r->next=p;
r=p;
}
while(b!=0)
{
p=(node *)malloc(sizeof(node));
r->next=p;
r=p;
r->data=b->data;
b=b->next;
}
r->next=NULL;
free r;
free p;
return c;
}
main()
{
node *k,*i,*j,*A,*B;
i=node *create(A);
j=node *create(B);
k=node *sum(i,j);
do{
printf"%d->",k->data);
k=k->next;
}while(k!=0);
}
目的是先建立两个链表,然后把它们合并在一起,相同的不删除