这是我的代码。它不起作用:为什么我的排序代码不能在c中工作?
void insertioon (int d) // this part, insert and sort list
{
struct node *np, *temp, *prev;
int found;
np=malloc(sizeof(struct node));
np->data = d;
np->nextPtr = NULL;
temp=firstPtr;
found=0;
while ((temp != NULL) && !found)
{
if (temp->data <d)
{
prev = temp;
temp = temp->nextPtr;
}
else
{
found=1;
}
if (prev == NULL)
{
np->nextPtr=firstPtr;
firstPtr=np;
}
else
{
prev->nextPtr = np;
np->nextPtr = temp;
}
}
}
我的错误是什么?在insertioon中,我想对这个列表进行排序。
while循环中的插入操作。和'prev'未初始化。 – BLUEPIXY
@BLUEPIXY但我写prev = temp;我初始化为 – user3142663
在进入循环之前,您必须初始化为NULL。 – BLUEPIXY