链接列表的访问冲突错误

时间:2015-11-29 06:47:29

标签: c linked-list

我尝试运行我的代码,但是,我遇到了访问冲突错误。我搜索了一些资源,但似乎我的代码没有任何问题。 有人可以帮助我吗?

tableView:didSelectRowAtIndexPath:

while循环运行时发生错误。

1 个答案:

答案 0 :(得分:1)

此解决方案基于我的猜测

void addToEnd(PlayerListNode *newNode){

    if (newNode == NULL) {
        printf("ERROR");
        exit(1);

    }

    if(!listHead){
       printf("\n First node %d inserted... \n", newNode->player);
       listHead=newNode;
       return;
    }

    printf("\n Inserting %d ... \n", newNode->player);
    PlayerListNode *current = listHead;
    while (current -> next != NULL)
    {
        current = current->next;
    }
    current->next = newNode;
}

因为您要插入到树的末尾,所以可以通过将lastNode成员添加到PlayerListNode来优化它;

void addToEnd(PlayerListNode *newNode){

    if (newNode == NULL) {
        printf("ERROR");
        exit(1);

    }

    if(!listHead){
       printf("\n First node %d inserted... \n", newNode->player);
       listHead=newNode;
    }
    listHead->LastNode->next = newNode;
    listHead->LastNode=newNode;
    listHead->LastNode->next = NULL; //unsure it is NULL in case of newNode is listHead
}