繁体   English   中英

将节点插入到c中的链表

[英]inserting node to linked list in c

有些请解释

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;
}

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

这是将简单节点添加到C中链接列表的开头的方式。代码仅保留对开头的引用,并且当添加新节点时,它将添加到开头,并且新插入的节点被视为新节点。头。

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

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

请在下面的链接中查看有关上述逻辑的全面图示说明

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM