我正在进入无限 while 循环(链表 c)
I'm getting into infinite while loop (linked lists c)
我陷入了 while 循环(打印“no”的循环):
#include<stdlib.h>
#include<stdio.h>
typedef struct bill {
int value;
struct bill *next;
} list;
void printlist(list *head) {
list *temp = head;
while (temp != NULL)
printf("%d ",temp->value), temp = temp->next;
}
void insert_at_end(list *head, int value) {
list *current, *first;
first = malloc(sizeof(list));
if (head == NULL)
head = first;
else {
current = head;
while (current->next != NULL) {
current = current->next;
printf("no");
}
}
}
void main() {
list *head = NULL;
head = malloc(sizeof(list));
for (int i = 0; i < 10; i++)
insert_at_end(head, i);
printlist(head);
}
我不确定为什么,但是 current
永远不会变为 NULL。我看了很多视频,它们都一样:
while (current != NULL)
current = current->next
...应该在某一时刻变为 NULL,但在我的情况下不是。
存在这些问题:
当你做head = malloc(sizeof(list))
时,那个节点的成员仍然是未定义的,包括它的next
成员。因此,当您进入 insert_at_end
函数中的 else
块时,循环将访问此未定义的 next
指针并导致未定义的行为。
与前面的问题类似,first
也没有用 value
和 next
成员初始化。
在 insert_at_end
函数的 else
块中,没有将新节点附加到列表的代码。
在insert_at_end
函数的if
块中,head
的值被改变了。但这只是改变了局部变量的值——调用者不会看到这种变化。为此,您应该更改函数参数,使其成为指向 head
指针的 指针 。
在主程序中创建 head
节点似乎没有根据——您应该从一个空列表开始,即 head
等于 NULL
.
void main
不正确。应该是int main
,它应该返回合适的值。
这里是对有问题的两个函数的更正:
// This function accepts a pointer to a head-pointer, so the head-pointer
// can be changed and the caller will receive that change.
void insert_at_end(list **head, int value){
list *current, *first;
first = malloc(sizeof(list));
first->value = value; // This was missing
first->next = NULL; // This was missing
if (*head == NULL) {
*head = first;
} else {
current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = first; // This was missing
}
}
// The main method has an int return type
int main() {
list *head = NULL;
// Do not create a node here.
for (int i = 0; i < 10; i++) {
insert_at_end(&head, i); // pass pointer to head-pointer
}
printlist(head);
return 0;
}
我陷入了 while 循环(打印“no”的循环):
#include<stdlib.h>
#include<stdio.h>
typedef struct bill {
int value;
struct bill *next;
} list;
void printlist(list *head) {
list *temp = head;
while (temp != NULL)
printf("%d ",temp->value), temp = temp->next;
}
void insert_at_end(list *head, int value) {
list *current, *first;
first = malloc(sizeof(list));
if (head == NULL)
head = first;
else {
current = head;
while (current->next != NULL) {
current = current->next;
printf("no");
}
}
}
void main() {
list *head = NULL;
head = malloc(sizeof(list));
for (int i = 0; i < 10; i++)
insert_at_end(head, i);
printlist(head);
}
我不确定为什么,但是 current
永远不会变为 NULL。我看了很多视频,它们都一样:
while (current != NULL)
current = current->next
...应该在某一时刻变为 NULL,但在我的情况下不是。
存在这些问题:
当你做
head = malloc(sizeof(list))
时,那个节点的成员仍然是未定义的,包括它的next
成员。因此,当您进入insert_at_end
函数中的else
块时,循环将访问此未定义的next
指针并导致未定义的行为。与前面的问题类似,
first
也没有用value
和next
成员初始化。在
insert_at_end
函数的else
块中,没有将新节点附加到列表的代码。在
insert_at_end
函数的if
块中,head
的值被改变了。但这只是改变了局部变量的值——调用者不会看到这种变化。为此,您应该更改函数参数,使其成为指向head
指针的 指针 。在主程序中创建
head
节点似乎没有根据——您应该从一个空列表开始,即head
等于NULL
.void main
不正确。应该是int main
,它应该返回合适的值。
这里是对有问题的两个函数的更正:
// This function accepts a pointer to a head-pointer, so the head-pointer
// can be changed and the caller will receive that change.
void insert_at_end(list **head, int value){
list *current, *first;
first = malloc(sizeof(list));
first->value = value; // This was missing
first->next = NULL; // This was missing
if (*head == NULL) {
*head = first;
} else {
current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = first; // This was missing
}
}
// The main method has an int return type
int main() {
list *head = NULL;
// Do not create a node here.
for (int i = 0; i < 10; i++) {
insert_at_end(&head, i); // pass pointer to head-pointer
}
printlist(head);
return 0;
}