将值设置为双结构指针

时间:2017-10-16 05:36:58

标签: c

所以,我正在实现一个基于链表的队列,我将节点信息放在一个结构中,然后将队列的头部和尾部放在一个结构中。当我尝试添加节点时,我必须使用 - >两次,我最终得到了分段错误错误。

现在我的代码是一个包含用户输入和多个选项和内容的大型程序的一部分,但我简化了它的简化。

wedgeNumber

当我使用valgrind时,它告诉我分段错误在此代码段中

endTracking

更具体地说,在队列 - > head-> next = node

我似乎无法弄清楚我做错了什么。

3 个答案:

答案 0 :(得分:2)

当你的头是NULL时,你如何设置queue-> head-> next = node;。首先设置你的头值,然后你可以更新你的头。请参阅以下代码

void addToList(Queue* queue, Node* node){   
    printf("Address of parameter: %p", node);
    if (queue->head == NULL){
        queue->head = node; \\this is where the error occurs 
        queue->tail = node; 

    }else{
        queue->tail->next = node; 
        queue->tail = node; 
    }
}

答案 1 :(得分:1)

是的,因为queue->headNULL

void addToList(Queue* queue, Node* node){   
    printf("Address of parameter: %p", node);
    /* vvvv HERE */
    if (queue->head == NULL){
        queue->head->next = node; \\this is where the error occurs 
        queue->tail->next = node; 
    }else{
        queue->tail->next = node; 
        queue->tail = node; 
    }
} 

答案 2 :(得分:1)

显然,addToList功能在以下部分中存在错误。

if (queue->head == NULL)
{
    queue->head->next = node; \\this is where the error occurs 
    queue->tail->next = node; 
}

作为阻止的前提条件,queue->headnull,但在下一行queue->head中使用queue->head->next取消引用,这不起作用。< / p>

相关问题