**两条有序顺序单链表的有序合并**
#include <iostream>
#define MaxSize 100
using namespace std;
typedef struct List
{
int num[MaxSize];
int len;
}List, * plist;
void Initialize(plist l)
{
l->len = 0;
}
bool List_Merge(plist l1, plist l2, plist l3)
{
if ((l1 == NULL) || (l2 == NULL) || (l3 == NULL)) return false;
if (l1->len + l2->len > MaxSize)
{
cout << "l3不够长,无法完成合并!";
return false;
}
int m = 0, n = 0, k = 0;
while (m < l1->len && n < l2->len) l3->num[k++] = l1->num[m] < l2->num[n] ? l1->num[m++] : l2->num[n++];
while (m < l1->len)
{
l3->num[k++] = l1->num[m++];
}
while (n < l2->len)
{
l3->num[k++] = l2->num[n++];
}
l3->len = l1->len + l2->len;
return true;
}
void Show(plist l)
{
for (int i = 0; i < l->len; ++i) cout << l->num[i] << " ";
}
int main()
{
List l1, l2, l3;
int len_l1 = 0, len_l2 = 0;
cout << "请输入l1的有效长度:\n";
cin >> len_l1;
cout << "请输入l2的有效长度:\n";
cin >> len_l2;
Initialize(&l1);
Initialize(&l2);
Initialize(&l3);
cout << "请输入l1的值: \n";
for (int i = 0; i < len_l1; ++i) {
cin >> l1.num[i];
l1.len++;
}
cout << "请输入l2的值: \n";
for (int i = 0; i < len_l2; ++i) {
cin >> l2.num[i];
l2.len++;
}
List_Merge(&l1, &l2, &l3);
Show(&l3);
return 0;
}