【发布时间】:2016-10-21 04:19:53
【问题描述】:
在链表实现中,向链表中插入一个新节点通常是这样写的:
void push(struct node** head_ref, int new_data)
/* 1. allocate node */
struct node* new_node = (struct node*) malloc(sizeof(struct node));
/* 2. put in the data */
new_node->data = new_data;
/* 3. Make next of new node as head */
new_node->next = (*head_ref);
/* 4. move the head to point to the new node */
(*head_ref) = new_node;
(我从http://quiz.geeksforgeeks.org/linked-list-set-2-inserting-a-node/获取此代码)
节点结构为:
结构节点 {
int 数据;
结构节点 *next;
};
我不明白的是插入部分的 3. 和 4.。所以你让new_node的next指针指向head,然后head指向new_node?所以这意味着下一个指针指向new_node?
这似乎是一个愚蠢的问题,但我无法理解它,所以我希望有人能给我解释一下。谢谢你。
【问题讨论】:
标签: linked-list