归纳编程学习的感悟,
记录奋斗路上的点滴,
希望能帮到一样刻苦的你!
如有不足欢迎指正!
共同学习交流!
🌎欢迎各位→点赞 👍+ 收藏⭐ + 留言📝
但行前路,不负韶华!
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include"LinkList.h"
#define DataType int
LNode* MergeList(LinkList A, LinkList B);
int main()
{
LinkList A, B, C;
LNode* p;
InitList(&A);
InitList(&B);
DataType a[] = { 12,16,21,33,35,87,102 };
DataType b[] = { 3,5,21,23,35,99,123 };
int n;
n = sizeof(a) / sizeof(a[0]);
for (int i = 1; i <=n; i++)
{
InsertElem(A, i, a[i - 1]);
}
n = sizeof(b) / sizeof(b[0]);
for (int i = 1; i <= n; i++)
{
InsertElem(B, i, b[i - 1]);
}
printf("链表A中共有%d个元素:\n", ListLength(A));
p = A->next;
for (int i = 1; i <= ListLength(A); i++)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
printf("链表B中共有%d个元素:\n", ListLength(B));
p = B->next;
for (int i = 1; i <= ListLength(B); i++)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
C = MergeList(A, B);
printf("合并的链表C中共有%d个元素:\n", ListLength(C));
p = C->next;
for (int i = 1; i <= ListLength(C); i++)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return 0;
}
LNode* MergeList(LinkList A, LinkList B)
{
LinkList C;
InitList(&C);
LNode* temp;
A = A->next;
B = B->next;
while (A && B)
{
if (A->data < B->data)
{
InsertElemF(C, A->data);
A = A->next;
}
else if(A->data==B->data)
{
temp = B;
B = B->next;
free(temp);
}
else
{
InsertElemF(C, B->data);
B = B->next;
}
}
while (A)
{
InsertElemF(C, A->data);
A = A->next;
}
while (B)
{
InsertElemF(C, B->data);
B = B->next;
}
return C;
}