在链表的插入最后一个函数中交换两行时出错

error in swapping two lines in insert last function of linked list

我不明白单链接中插入最后一个函数的一件事list.this函数总是在最后一个节点之后插入新项目。

newNode->next=temp->next;
temp->next=newNode;

我收到一个错误。我不知道为什么?请帮忙!!

int insertLast(int item)
{

    struct listNode *newNode,*temp;
    newNode=(struct listNode*)malloc(sizeof(struct listNode));

    newNode->item=item;
    newNode->next=NULL;
    if(list==NULL)
    {
        list=newNode;
    }
    else
    {
        temp=list;
        while(temp->next!=NULL)temp=temp->next;
        newNode->next=temp->next;
        temp->next=newNode;
    }
    return SUCCESS_VALUE;
}
newNode->next=temp->next;

NewNode->next 应该保持 null 因为您要添加到列表的末尾。现在你的列表最后会有一个圆圈 (temp->newNode->temp->newNode...) 所以你将无法通过它。