我不知道为什么我的链表指针不动
I don't know why my linked list pointer isnt moving
我正在尝试编写一段代码,将元素添加到列表中。
typedef struct things {
int value;
struct things *next;
} something;
int main()
{
int input = 0;
something *head = NULL;
something *current = NULL;
current = head; //current points to head address
while(input != -1)
{
scanf("%d", &input);
while(current != NULL) //loop until current node returns NULL
{
current = current->next; //go to next node
}
current = malloc(sizeof(something)); //allocate memory for new node assuming current is NULL
current->value = input;
current->next = NULL; //next points to NULL
}
current=head; //current points back to head
while(current != NULL)
{
printf("%d -> ", current->value);
current = current->next;
}
puts("NULL");
return 0;
}
然而,当我尝试打印列表时,我没有得到任何输出。因此,即使我输入 1 2 3 4..etc,打印功能也不会输出任何内容
while(current != NULL)
{
printf("%d -> ", current->value);
current = current->next;
}
puts("NULL");
我期待像 1 -> 2 -> 3 -> ... 9 -> NULL
这样的输出。我刚刚开始学习链表,所以欢迎任何建议。
您在任何时候都没有更新 head
的值。或者将列表中的最后一个节点指向新创建的节点。
首先检查是否设置了 head
,如果没有,则填充它。否则,找到列表的最后一个节点并将新节点添加到 "next" 中,如下所示。
if(head == NULL)
{
head = malloc(sizeof(something));
head->value = input;
head->next = NULL; //next points to NULL
}
else
{
current = head;
while(current->next != NULL) //loop until current node returns NULL
{
current = current->next; //go to next node
}
current->next = malloc(sizeof(something)); //allocate memory for new node assuming current is NULL
current->next->value = input;
current->next->next = NULL; //next points to NULL
}
您当前的方法不适合单指针。
将内存分配给 current
的地方不会将节点插入到列表中。
只需将 current
作为指向指针的指针,您的方法就会起作用。
int input = 0;
something *head = NULL;
something **current = NULL;
current = &head; //current points to head address
while(input != -1)
{
scanf("%d", &input);
while(*current != NULL) //loop until current node returns NULL
{
current = &(*current)->next; //go to next node
}
*current = malloc(sizeof(something)); //allocate memory for new node assuming current is NULL
(*current)->value = input;
(*current)->next = NULL; //next points to NULL
}
current=&head; //current points back to head
while(*current != NULL)
{
printf("%d -> ", (*current)->value);
current = &(*current)->next;
}
puts("NULL");
我正在尝试编写一段代码,将元素添加到列表中。
typedef struct things {
int value;
struct things *next;
} something;
int main()
{
int input = 0;
something *head = NULL;
something *current = NULL;
current = head; //current points to head address
while(input != -1)
{
scanf("%d", &input);
while(current != NULL) //loop until current node returns NULL
{
current = current->next; //go to next node
}
current = malloc(sizeof(something)); //allocate memory for new node assuming current is NULL
current->value = input;
current->next = NULL; //next points to NULL
}
current=head; //current points back to head
while(current != NULL)
{
printf("%d -> ", current->value);
current = current->next;
}
puts("NULL");
return 0;
}
然而,当我尝试打印列表时,我没有得到任何输出。因此,即使我输入 1 2 3 4..etc,打印功能也不会输出任何内容
while(current != NULL)
{
printf("%d -> ", current->value);
current = current->next;
}
puts("NULL");
我期待像 1 -> 2 -> 3 -> ... 9 -> NULL
这样的输出。我刚刚开始学习链表,所以欢迎任何建议。
您在任何时候都没有更新 head
的值。或者将列表中的最后一个节点指向新创建的节点。
首先检查是否设置了 head
,如果没有,则填充它。否则,找到列表的最后一个节点并将新节点添加到 "next" 中,如下所示。
if(head == NULL)
{
head = malloc(sizeof(something));
head->value = input;
head->next = NULL; //next points to NULL
}
else
{
current = head;
while(current->next != NULL) //loop until current node returns NULL
{
current = current->next; //go to next node
}
current->next = malloc(sizeof(something)); //allocate memory for new node assuming current is NULL
current->next->value = input;
current->next->next = NULL; //next points to NULL
}
您当前的方法不适合单指针。
将内存分配给 current
的地方不会将节点插入到列表中。
只需将 current
作为指向指针的指针,您的方法就会起作用。
int input = 0;
something *head = NULL;
something **current = NULL;
current = &head; //current points to head address
while(input != -1)
{
scanf("%d", &input);
while(*current != NULL) //loop until current node returns NULL
{
current = &(*current)->next; //go to next node
}
*current = malloc(sizeof(something)); //allocate memory for new node assuming current is NULL
(*current)->value = input;
(*current)->next = NULL; //next points to NULL
}
current=&head; //current points back to head
while(*current != NULL)
{
printf("%d -> ", (*current)->value);
current = &(*current)->next;
}
puts("NULL");