gpt4 book ai didi

c - 为什么我的列表合并排序不起作用 -c

转载 作者:行者123 更新时间:2023-11-30 20:35:48 24 4
gpt4 key购买 nike

我正在发送到列表的合并排序函数头,不知何故它消失了最少的器官,我发送了列表中的 10 个公寓,它应该按最大价格合并,我注意到当它返回时,返回它从函数递归得到公寓号 9(公寓号从 6 到 10 的头部),但不知何故它得到了公寓号 6 作为第一个。我在学校的项目中使用了这个功能,我需要在下周之前提交它,所以我将非常感谢接下来几天的帮助,提前谢谢您。

void MergeSort(ApNode *head)
{

ApNode *front, *back;
if ((head == NULL) || (head->next == NULL))
{
return;
}
else {//lst not empty
front = (ApNode*)malloc(sizeof(ApNode));
back = (ApNode*)malloc(sizeof(ApNode));
/* Split head into 'front' and 'back' sublists */
FrontBackSplit(head, &front, &back);

/* Recursively sort the sublists */
MergeSort(front);
MergeSort(back);
head = merge(front, back);
}
return;
}

void FrontBackSplit(ApNode* source, ApNode** frontRef, ApNode** backRef)
{

ApNode* fast;
ApNode* slow;
if (source == NULL || source->next == NULL)
{
/* length < 2 cases */
*frontRef = source;
*backRef = NULL;
}
else
{
slow = source;
fast = source->next;

/* Advance 'fast' two nodes, and advance 'slow' one node */
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}

/* 'slow' is before the midpoint in the list, so split it in two
at that point. */
*frontRef = source;
*backRef = slow->next;
slow->next = NULL;
}
}

ApNode* merge(ApNode* head1, ApNode * head2)
{

ApNode * curr1 = head1;
ApNode * curr2 = head2;
ListAp lst3;
makeEmptyApList(&lst3);
while ((curr1 != NULL) && (curr2 != NULL))
{
if ((curr1->price)<(curr2->price))
{
addAp(&lst3, curr1);
curr1 = curr1->next;
}
else
{
addAp(&lst3, curr2);
curr2 = curr2->next;
}
}
while (curr1 != NULL)
{
addAp(&lst3, curr1);
curr1 = curr1->next;
}
while (curr2 != NULL)
{
addAp(&lst3, curr2);
curr2 = curr2->next;
}
return lst3.head;
}

最佳答案

我认为问题是你在调用之前没有原型(prototype)化FrontBackSplit(head, &front, &back);。比编译器无法识别该函数。尝试将 FrontBackSplit(ApNode* source, ApNode** frontRef, ApNode** backRef) 函数放在 void MergeSort(ApNode *head) 之前。

关于c - 为什么我的列表合并排序不起作用 -c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37819363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com