能帮我看个程序嘛?L1和L2是给定的带头结点的单链表,其结点存储的数据是递增有序的;函数Merge要将L1和L2合并为一个非递减的整数序列。应直接使用原序列中的结点,返回归并后的带头结点的链表头指针。
List Merge(List L1, List L2)
{
List P, searchP1, searchP2, searchP3,followP;
P = (List)malloc(sizeof(List));
P->Next = NULL;
searchP1 = L1->Next;
searchP2 = L2->Next;
searchP3 = P;
while (searchP1 != NULL && searchP2 != NULL)
{
if (searchP1->Data <= searchP2->Data)
{
searchP3->Next = searchP1;
followP = searchP1;
searchP1 = searchP1->Next;
searchP3 = searchP3->Next;
free(followP);
}
else
{
searchP3->Next = searchP2;
followP = searchP2;
searchP2 = searchP2->Next;
searchP3 = searchP3->Next;
free(followP);
}
}
while (searchP1 != NULL)
{
searchP3->Next = searchP1;
followP = searchP1;
searchP1 = searchP1->Next;
searchP3 = searchP3->Next;
free(followP);
}
while (searchP2 != NULL)
{
searchP3->Next = searchP2;
followP = searchP2;
searchP2 = searchP2->Next;
searchP3 = searchP3->Next;
free(followP);
}
searchP3->Next = NULL;
return P;
}
好像报错了,但是不清楚哪里为问题。
举报
数据结构
解决了,谢谢大家。List Merge(List L1, List L2){ List P, searchP1, searchP2, searchP3; P = (List)malloc(sizeof(List)); P->Next = NULL; searchP1 = L1->Next; searchP2 = L2->Next; searchP3 = P; while (searchP1 != NULL && searchP2 != NULL) { if (searchP1->Data <= searchP2->Data) { searchP3->Next = searchP1; searchP1 = searchP1->Next; searchP3 = searchP3->Next; } else { searchP3->Next = searchP2; searchP2 = searchP2->Next; searchP3 = searchP3->Next; } } while (searchP1 != NULL) { searchP3->Next = searchP1; searchP1 = searchP1->Next; searchP3 = searchP3->Next; } while (searchP2 != NULL) { searchP3->Next = searchP2; searchP2 = searchP2->Next; searchP3 = searchP3->Next; } searchP3->Next = NULL; L1->Next = NULL; L2->Next = NULL; return P;} -
彩依
2020-12-13
举报
举报
阅。。