#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
struct Node*next;
}Node,*Linkedlist;
Linkedlist LinkedlistInit(){
Node*L;
L=(Node*)malloc(sizeof(Node));
if(L==NULL){
exit(0);
}
L->next=NULL;
return L;
}
Linkedlist LinkedlistCreatT(){
Node*L;
L=(Node*)malloc(sizeof(Node));
L->next=NULL;
Node*r;
r=L;
int x;
while(scanf("%d",&x)!=EOF){
Node*p;
p=(Node*)malloc(sizeof(Node));
p->data=x;
r->next=p;
r=p;
}
r->next=NULL;
return L;
}
Linkedlist LinkedlistCreatT1(){
Node*L1;
L1=(Node*)malloc(sizeof(Node));
L1->next=NULL;
Node*r1;
r1=L1;
int x1;
while(scanf("%d",&x1)!=EOF){
Node*p1;
p1=(Node*)malloc(sizeof(Node));
p1->data=x1;
r1->next=p1;
r1=p1;
}
r1->next=NULL;
return L1;
}
Linkedlist LinkedlistMerge(Linkedlist L1,Linkedlist L2){
Node*p1=L1->next;
Node*p2=L2->next;
Node*q;
q=L1;
while(p1&&p2)
{
if(p1->data<=p2->data)
{
q->next=p1;
p1=p1->next;
}
else
{
q->next=p2;
p2=p2->next;
}
q=q->next;
}
if(p1!=NULL)
{
q->next=p1;
}
else{
q->next=p2;
}
free(L2);
return L1;
}
int main()
{
Linkedlist L1,L2,L3;
L1=LinkedlistCreatT();
L2=LinkedlistCreatT1();
L3=LinkedlistMerge(L1,L2);
Node *p=L3->next;
{
while(p)
{
printf("%d\n",p->data);
p=p->next;
}
}
return 0;
}
合并两个链表(简单合并元素不处理)
最新推荐文章于 2024-08-02 12:48:49 发布