在c中将节点插入链表

时间:2013-10-05 07:37:50

标签: c linked-list

有些请解释

new_node->next = (*head_ref);
(*head_ref) = new_node;

这在下面的代码中

/* Utility function to insert a node at the beginning */
void push(struct node **head_ref, int new_data)
{
    struct node *new_node = (struct node *) malloc(sizeof(struct node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

2 个答案:

答案 0 :(得分:0)

评论说的正是如此。 在开头插入新节点,并更新head_ref以指向新的开始。

答案 1 :(得分:0)

这是一个简单节点如何添加到C中链表的开头。代码只保存对头的引用,当添加新节点时,它被添加到开头,并且新插入的节点被认为是作为新的头脑。

在您的代码中,第一行在开头添加新节点。这是通过将当前列表(由标题指向)附加到新节点。

第二行将新节点标记为列表头。

请查看以下链接,了解上述逻辑的全面图示说明

http://www.thelearningpoint.net/computer-science/data-structures-singly-linked-list-with-c-program-source-code