//#include<bits/stdc++.h>
#include<stdio.h>
#include<stdlib.h>
struct Link{ //创建结构体
int a;
struct Link* next;
};
struct Link* creat(){ //创建链表
struct Link* head;
struct Link* p1,*p2;
p1=p2=(struct Link*)malloc(sizeof(struct Link));
int i=0;
head=p1;
for(i=0;i<15;i++){
p2=(struct Link*)malloc(sizeof(struct Link));
p1->next=p2;
p1=p2;
scanf("%d",&p2->a);
}
p2->next=NULL;
return head->next;
}
struct Link* Mysort(struct Link*L1,struct Link*L2){ //进行链表合并
struct Link*head;
struct Link*p1,*p2;
int flag=0; //flag为标记链表head,做准备
struct Link*p=(struct Link*)malloc(sizeof(struct Link));//为p分配空间,使p能存下地址,不分配地址会报错
p1=L1;
p2=L2;
while(p1!=NULL&&p2!=NULL){
if((p1->a)>(p2->a)){
p->next=p2;
p2=p2->next;
p=p->next;
}else{
p->next=p1;
p1=p1->next;
p=p->next;
}
if(cnt==0){
head=p;
}
flag=1;
}
if(p1==NULL){ //让p->next指向未比较完的链表
p->next=p2;
}else{
p->next=p1;
}
return head;
}
void Myprint(struct Link* L){ //打印链表
struct Link*head;
struct Link*p1;
head=p1=L;
while(p1!=NULL){
printf("%d ",p1->a);
p1=p1->next;
}
}
int main(){
struct Link*L1,*L2,*head;
L1=creat();
L2=creat();
head=Mysort(L1,L2);
Myprint(head);
return 0;
}
输入样例:
2 5 18 21 27 34 41 61 62 69 78 81 91 92 95
0 4 16 24 27 36 42 45 53 58 64 67 82 91 95