#include <bits/stdc++.h>
using namespace std;
typedef struct Link_list
{
int data;
struct Link_list *next;
}List;
void print(List *L) {
if(L==NULL) cout<<"NULL";
else{
cout<<L->data;
L = L -> next;
while(L) {
cout<<" "<<L->data;
L = L -> next;
}
}
}
List *create(List *L) {
L = (List *)malloc(sizeof(List));
L->next = NULL;
List *r = L;
int b;
cin>>b;
while(b!=-1){
List *p = (List *)malloc(sizeof(List));
p->data = b;
r->next = p;
p->next = NULL;
r = p;
cin>>b;
}
return L;
}
List *merge_list(List *L1, List *L2) {
List p;
p.data = 0;
p.next = NULL;
List *result = &p;
while (L1 && L2) {
if (L1->data < L2->data) {
result->next = L1;
L1 = L1->next;
} else {
result->next = L2;
L2 = L2 ->next;
}
result = result -> next;
}
if (L1 == NULL) {
result ->next = L2;
}
if (L2 == NULL) {
result ->next = L1;
}
return p.next;
}
int main(){
List *L1,*L2;
L1 = create(L1);
L2 = create(L2);
List *result = merge_list(L1->next,L2->next);
print(result);
return 0;
}
7-1 两个有序链表序列的合并
最新推荐文章于 2021-10-09 22:29:56 发布