在后面添加一个链表
Adding to the back a a linked List
我正在尝试将元素添加到链表的后面。我尝试通过迭代列表来做到这一点,直到我遇到一个指向 0 的指针。然后我创建一个指向这个指针的新指针,然后我尝试让它指向我列表中的一个新节点。编译器没有注释,但是当我尝试写下我的列表时,它不包括我尝试添加的元素。
void add_back(Node * s, int x) {
Node * new_node = malloc(sizeof(Node));
Node * start = s;
new_node->value = x;
new_node->next = 0;
while(start != 0) {
start = start->next;
}
Node ** plaats = &start;
*plaats = new_node;
}
使用的结构:
struct Node {
int value;
struct Node * next;
};
typedef struct Node Node;
您完成了最困难的部分 - 这就是您在最后几行中需要做的全部。
void add_back(Node * s, int x) {
if(s == NULL) // handle empty list
return;
Node * new_node = malloc(sizeof(Node));
new_node->value = x;
Node * start = s;
while(start->next != NULL) { //reach the last node - don't traverse further
start = start->next;
}
new_node->next = NULL;
start->next = newnode;
/* not required
Node ** plaats = &start;
*plaats = new_node;
*/
}
这个:
while(start->next != 0) { //reach the last node - don't traverse further
start = start->next;
}
让你到达这里:
+----+-----+ +-----+-----+ +-----+------+
| | +-->| | +-->| | NULL |
+----+-----+ +-----+-----+ +-----+------+
/
LastNode
这两行:
new_node->next = 0;
start->next = newnode;
这样做:
+----+-----+ +-----+-----+ +-----+------+ +-----+------+
| | +-->| | +-->| | ------------->| | NULL |
+----+-----+ +-----+-----+ +-----+------+ +-----+------+
/
New Node
问题是寻找最后一个节点的循环,当循环结束时start
不是最后一个节点,而是NULL
。然后你得到 start
的地址,它会给你一个指向 local 变量 start
的指针,并将新节点分配给那个地方。
而是具体检查列表是否为空,然后将节点添加为第一个节点。如果列表不为空,循环while start->next
is not NULL
,并使start->next
指向新节点。
此外,我建议增强函数 add_back() 以获取指向节点列表头的指针,并检查头是否为空并生成新头,否则执行建议的解决方案
Node *add(Node **s, int x)
{
Node *n = malloc(sizeof(Node));
... fill n;
n->next = NULL;
/* no head parameter */
if (s == NULL) {
return n;
}
/* empty head */
if (*s == NULL) {
*s = n;
} else {
/* append to end */
... code from other solution with (*s) instead of s
}
return *s;
}
...
Node *head = NULL;
add(&head, 1);
...
通过返回可能是新的头部,您可以将 add() 用作函数并立即使用结果
head = NULL;
printnodelist(add(&head, 1));
iterateovernodelist(head);
void add_back(Node * s, int x) {
一开始就错了。使用该原型,您无法添加到空列表中。
该函数可以在列表为空时改变列表的头部。所以你必须通过引用传递头部。
因此该函数将如下所示
void add_back( Node **head, int x )
{
Node **tail = head;
Node *new_node;
while ( *tail != NULL ) tail = &( *tail )->next;
new_node = malloc( sizeof( Node ) );
new_node->value = x;
new_node->next = NULL;
*tail = new_node;
}
因此,如果您将列表定义为
Node *head = NULL;
那么函数就这样调用
int i = 0;
for ( ; i < 10; i++ ) add_back( &head, i );
我正在尝试将元素添加到链表的后面。我尝试通过迭代列表来做到这一点,直到我遇到一个指向 0 的指针。然后我创建一个指向这个指针的新指针,然后我尝试让它指向我列表中的一个新节点。编译器没有注释,但是当我尝试写下我的列表时,它不包括我尝试添加的元素。
void add_back(Node * s, int x) {
Node * new_node = malloc(sizeof(Node));
Node * start = s;
new_node->value = x;
new_node->next = 0;
while(start != 0) {
start = start->next;
}
Node ** plaats = &start;
*plaats = new_node;
}
使用的结构:
struct Node {
int value;
struct Node * next;
};
typedef struct Node Node;
您完成了最困难的部分 - 这就是您在最后几行中需要做的全部。
void add_back(Node * s, int x) {
if(s == NULL) // handle empty list
return;
Node * new_node = malloc(sizeof(Node));
new_node->value = x;
Node * start = s;
while(start->next != NULL) { //reach the last node - don't traverse further
start = start->next;
}
new_node->next = NULL;
start->next = newnode;
/* not required
Node ** plaats = &start;
*plaats = new_node;
*/
}
这个:
while(start->next != 0) { //reach the last node - don't traverse further
start = start->next;
}
让你到达这里:
+----+-----+ +-----+-----+ +-----+------+
| | +-->| | +-->| | NULL |
+----+-----+ +-----+-----+ +-----+------+
/
LastNode
这两行:
new_node->next = 0;
start->next = newnode;
这样做:
+----+-----+ +-----+-----+ +-----+------+ +-----+------+
| | +-->| | +-->| | ------------->| | NULL |
+----+-----+ +-----+-----+ +-----+------+ +-----+------+
/
New Node
问题是寻找最后一个节点的循环,当循环结束时start
不是最后一个节点,而是NULL
。然后你得到 start
的地址,它会给你一个指向 local 变量 start
的指针,并将新节点分配给那个地方。
而是具体检查列表是否为空,然后将节点添加为第一个节点。如果列表不为空,循环while start->next
is not NULL
,并使start->next
指向新节点。
此外,我建议增强函数 add_back() 以获取指向节点列表头的指针,并检查头是否为空并生成新头,否则执行建议的解决方案
Node *add(Node **s, int x)
{
Node *n = malloc(sizeof(Node));
... fill n;
n->next = NULL;
/* no head parameter */
if (s == NULL) {
return n;
}
/* empty head */
if (*s == NULL) {
*s = n;
} else {
/* append to end */
... code from other solution with (*s) instead of s
}
return *s;
}
...
Node *head = NULL;
add(&head, 1);
...
通过返回可能是新的头部,您可以将 add() 用作函数并立即使用结果
head = NULL;
printnodelist(add(&head, 1));
iterateovernodelist(head);
void add_back(Node * s, int x) {
一开始就错了。使用该原型,您无法添加到空列表中。
该函数可以在列表为空时改变列表的头部。所以你必须通过引用传递头部。
因此该函数将如下所示
void add_back( Node **head, int x )
{
Node **tail = head;
Node *new_node;
while ( *tail != NULL ) tail = &( *tail )->next;
new_node = malloc( sizeof( Node ) );
new_node->value = x;
new_node->next = NULL;
*tail = new_node;
}
因此,如果您将列表定义为
Node *head = NULL;
那么函数就这样调用
int i = 0;
for ( ; i < 10; i++ ) add_back( &head, i );