#include <iostream>
using namespace std;
struct listpoint
{
listpoint * next;
int data;
};
listpoint* creat_normal_list(int n)
{
listpoint * end, *head;
end = (listpoint*)malloc(sizeof(listpoint));
head = end;
for (int i = 0; i < n; i++)
{
listpoint * normal = (listpoint*)malloc(sizeof(listpoint));
cin >> normal->data;
end->next = normal;
end = normal;
}
end->next = NULL;
return head;
}
listpoint* mergeList(listpoint* list1, listpoint* list2)
{
if (list1 == NULL)
return list2;
else if (list2 == NULL)
return list1;
listpoint * pMergeHead = NULL;
if (list1->data < list2->data)
{
pMergeHead = list1;
pMergeHead->next = mergeList(list1->next, list2);
}
else
{
pMergeHead = list2;
pMergeHead->next = mergeList(list1, list2->next);
}
return pMergeHead;
}
void printf(listpoint* list, int n)
{
listpoint* p = list;
for (int i = 0; i < n; i++)
{
if (p->next != NULL)
{
cout << p->data << " ";
p = p->next;
}
}
cout << p->data << " ";
cout << endl;
}
int main()
{
listpoint * list1 = creat_normal_list(4);
listpoint * list2 = creat_normal_list(4);
listpoint * list3 = mergeList(list1->next, list2->next);
printf(list3, 8);
system("pause");
return 0;
}
顺序链表合并
最新推荐文章于 2023-03-08 00:35:49 发布