直接上代码
NODE combine(NODE L1,NODE L2)
{
NODE r;
NODE p1 = L1->next;
NODE p2 = L2->next;
L1->next = NULL;
while( p1!=NULL && p2!=NULL)
{
if(p1->data < p2->data)
{
r = p1;
p1 = p1->next;
}
else if(p1->data > p2->data)
{
r = p2;
p2 = p2->next;
}
r->next = L1->next;
L1->next = r;
L1 = r;
if(p2) L1->next = p2;
if(p1) L1->next = p1;
}
完整代码
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}lnode,*NODE;
NODE createlist()
{
NODE headnode = (NODE)malloc(sizeof(lnode));
headnode->next = NULL;
return headnode;
}
void backinsert(NODE L)
{
NODE p = L;
int x;
printf("请输入数据 0截至\n");
scanf("%d",&x);
while(x != 0)
{
p->next = (NODE)malloc(sizeof(lnode));
p = p->next;
p->data = x;
scanf("%d",&x);
}
p->next = NULL;
}
NODE combine(NODE L1,NODE L2)
void print(NODE L1)
{
NODE pmove = L1->next;
while(pmove != NULL)
{
printf("%d\n",pmove->data);
pmove = pmove->next;
}
}
int main()
{
NODE L1 = createlist();
NODE L2 = createlist();
NODE L3 = createlist();
backinsert(L1);
backinsert(L2);
printf("即将进行递增合并\n");
combine(L1,L2);
print(L1);
}