나는 학습을 위해서 간단한 링크리스트의 구현을 쓰고 있어요 반복합니다. 내 연결된 목록은 값과 다음 노드에 대한 포인터를 포함하는 node
구조로 구성됩니다. 코드를 실행하면 NULL 포인터에 도달 할 때 종료해야하더라도 코드가 끝없이 반복됩니다. 내가 도대체 뭘 잘못하고있는 겁니까?링크 된 목록은 끝없이
#include <stdio.h>
struct node {
int value;
struct node *next_node;
};
struct node * add_node(struct node *parent, int value)
{
struct node child;
child.value = value;
child.next_node = NULL;
parent->next_node = &child;
return parent->next_node;
}
void print_all(struct node *root)
{
struct node *current = root;
while (current != NULL) {
printf("%d\n", current->value);
sleep(1);
current = current->next_node;
}
}
int main()
{
struct node root;
root.value = 3;
struct node *one;
one = add_node(&root, 5);
print_all(&root);
}
디버거에서 얼마나 걸음 밟았습니까? – kfsone