C语言 在链表交换期间未传递指针



这很可能看起来我错过了一些明显的东西,但是当我尝试将链表指针传递给我的选择排序时,我遇到了一个空指针问题。在我的 C 代码中,我将其作为我的链表:

typedef struct iorb
{
int base_pri;
struct iorb *link;
char filler[100];
} IORB;

然后我在创建新链表后将它传递到这个函数中:

void swapNodes(POINTER *head, POINTER CurrentHead, POINTER CurrentMinimum, POINTER TempSwap);
POINTER SortList(POINTER *head, char *SortMethod[])
{
POINTER TempHead = *head;
//Only one node, no need to sort.
if (TempHead->link == NULL)
{
return head;
}
//Store node with the new minimum value.
POINTER TempMin = *head;
//Store curent node for swapping
POINTER TempSwap = *head;
//Transverse the list.
POINTER TempCurrent;
for (TempCurrent = *head; TempCurrent->link != NULL; TempCurrent = TempCurrent->link)
{
//Check if this node has a lower priority than the current minimum and if so, swap them.
if (TempCurrent->link->base_pri < TempMin->base_pri)
{
TempMin = TempCurrent->link;
TempSwap = TempCurrent;
}
}
//Swap nodes if the head is not the same as the minimum.
if (TempMin != TempHead)
{
swapNodes(&TempHead, TempHead, TempMin, TempSwap);
}   
//Recursively sort the rest of the list.
//FOR SOME REASON THE NODE POINTER IS NOT BEING PASSED HERE (EMPTY)
TempHead->link = SortList(TempHead->link, *SortMethod);
return head;
}
void swapNodes(POINTER *head, POINTER CurrentHead, POINTER CurrentMinimum, POINTER TempSwap)
{
//Set new head as the minimum.
*head = CurrentMinimum;
//Link the current temp swap to the head.
TempSwap->link = CurrentHead;
//Swap pointers.
POINTER temp = CurrentMinimum->link;
CurrentMinimum->link = CurrentHead->link;
CurrentHead->link = temp;
}

我不确定为什么它没有被传回同一个函数,当我调试链表时似乎没问题。我怀疑我在交换节点函数中缺少一些东西,但我不太明白这是什么。有人可以提供有关此代码应该如何交换节点的一些见解吗?

如果您需要更多信息,请告诉我。

SortList(TempHead->link, *SortMethod(;需要列为 SortList(&TempHead, *SortMethod(;

正确传递指针。

最新更新