여기 내 코드입니다. 작동하지 않습니다.내 정렬 된 코드가 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;
}
}
}
제 실수는 무엇입니까? 삽입시이 목록을 정렬하고 싶습니다.
while 루프의 삽입 작업. 'prev'는 초기화를하지 않습니다. – BLUEPIXY
@BLUEPIXY 그러나 나는 prev = temp라고 쓴다. 그리고 나는 초기화한다 – user3142663
루프에 들어가기 전에 NULL로 초기화되어야한다. – BLUEPIXY