为什么我不能在 C 中向链表添加节点?
Why can't I add a node to linked list in C?
我对 C 中的链接列表有疑问。我创建了一个函数,该函数使用一些信息 (char *description) 创建了列表的新节点并将其添加到其末尾。代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char *description;
struct node *next;
};
// The function to create a node and append it to the linked list of nodes
struct node* create_node(struct node *first, char *description) {
struct node *current = first;
// Iteration through the list until the end
while(current != NULL) {
node++;
current = current -> next;
}
// Now pointer current points at the end of the list, first -> next. How to assign the pointer new to first -> next through current?
struct node *new = malloc(sizeof(struct container));
new -> next = NULL;
new -> description = malloc(sizeof(description));
memcpy(new -> description, description, sizeof(description));
current = new;
return current;
}
int main() {
// Creating the first node
struct node *first = create_node(NULL, "First");
// Creating and appending the second node to the list
create_node(first, "Second");
printf("%d\n", first -> next == NULL); // Prints 1, the newly created node hasn't been appended
return 0;
}
我搜索了如何创建此类列表,发现了非常相似的方法。我知道这是基本的东西,很可能有一个简单的解决方案,但我找不到。
谢谢大家的回复。
大家好,我是这个社区的新人。让我们试着帮忙。
我认为您指向列表的最后一个节点并将其更改为第
行的新节点
current = new;
但是对于link新节点你应该把它保存在节点的下一个字段中,试试:
current->next=new;
希望对你有帮助,再见:)。
对于初学者来说,函数名称 create_node
令人困惑。至少将函数命名为 append_node
.
会好得多
第二个函数参数应该有限定符 const,因为传递的字符串在函数中没有改变。
在这些陈述中
new -> description = malloc(sizeof(description));
memcpy(new -> description, description, sizeof(description));
您正在根据 sizeof( char * )
的值分配大小等于 8 或 4 字节的内存,并相应地处理这个字节数。
你至少要写
new -> description = malloc( strlen(description));
memcpy(new -> description, description, strlen(description));
但是如果你复制整个字符串会更好。
该函数有一个错误。它不会将节点附加到列表,因为在函数内更改了未链接到列表的本地指针当前。
考虑到内存分配可能会失败。你应该处理这样的情况。
如果将指针传给头节点,函数会更安全更简单。
这是一个演示程序。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
char *description;
struct node *next;
};
// The function to create a node and append it to the linked list of nodes
int append_node( struct node **head, const char *description )
{
struct node *new_node = malloc( sizeof( struct node ) );
int success = new_node != NULL;
if ( success )
{
new_node->description = malloc( strlen( description ) + 1 );
success = new_node->description != NULL;
if ( success )
{
strcpy( new_node->description, description );
new_node->next = NULL;
while ( *head != NULL )
{
head = &( *head )->next;
}
*head = new_node;
}
else
{
free( new_node );
}
}
return success;
}
int main( void )
{
// Creating the first node
struct node *head = NULL;
if ( append_node( &head, "first" ) )
{
printf( "%s\n", head->description );
}
return 0;
}
程序输出为
first
我对 C 中的链接列表有疑问。我创建了一个函数,该函数使用一些信息 (char *description) 创建了列表的新节点并将其添加到其末尾。代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char *description;
struct node *next;
};
// The function to create a node and append it to the linked list of nodes
struct node* create_node(struct node *first, char *description) {
struct node *current = first;
// Iteration through the list until the end
while(current != NULL) {
node++;
current = current -> next;
}
// Now pointer current points at the end of the list, first -> next. How to assign the pointer new to first -> next through current?
struct node *new = malloc(sizeof(struct container));
new -> next = NULL;
new -> description = malloc(sizeof(description));
memcpy(new -> description, description, sizeof(description));
current = new;
return current;
}
int main() {
// Creating the first node
struct node *first = create_node(NULL, "First");
// Creating and appending the second node to the list
create_node(first, "Second");
printf("%d\n", first -> next == NULL); // Prints 1, the newly created node hasn't been appended
return 0;
}
我搜索了如何创建此类列表,发现了非常相似的方法。我知道这是基本的东西,很可能有一个简单的解决方案,但我找不到。 谢谢大家的回复。
大家好,我是这个社区的新人。让我们试着帮忙。
我认为您指向列表的最后一个节点并将其更改为第
行的新节点current = new;
但是对于link新节点你应该把它保存在节点的下一个字段中,试试:
current->next=new;
希望对你有帮助,再见:)。
对于初学者来说,函数名称 create_node
令人困惑。至少将函数命名为 append_node
.
第二个函数参数应该有限定符 const,因为传递的字符串在函数中没有改变。
在这些陈述中
new -> description = malloc(sizeof(description));
memcpy(new -> description, description, sizeof(description));
您正在根据 sizeof( char * )
的值分配大小等于 8 或 4 字节的内存,并相应地处理这个字节数。
你至少要写
new -> description = malloc( strlen(description));
memcpy(new -> description, description, strlen(description));
但是如果你复制整个字符串会更好。
该函数有一个错误。它不会将节点附加到列表,因为在函数内更改了未链接到列表的本地指针当前。
考虑到内存分配可能会失败。你应该处理这样的情况。
如果将指针传给头节点,函数会更安全更简单。
这是一个演示程序。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
char *description;
struct node *next;
};
// The function to create a node and append it to the linked list of nodes
int append_node( struct node **head, const char *description )
{
struct node *new_node = malloc( sizeof( struct node ) );
int success = new_node != NULL;
if ( success )
{
new_node->description = malloc( strlen( description ) + 1 );
success = new_node->description != NULL;
if ( success )
{
strcpy( new_node->description, description );
new_node->next = NULL;
while ( *head != NULL )
{
head = &( *head )->next;
}
*head = new_node;
}
else
{
free( new_node );
}
}
return success;
}
int main( void )
{
// Creating the first node
struct node *head = NULL;
if ( append_node( &head, "first" ) )
{
printf( "%s\n", head->description );
}
return 0;
}
程序输出为
first