我只是在用节点和链表来触及表面,所以我一直在尝试创建一个链表来打印出 1-10 的节点。但是,它充满了问题。该程序给了我运行时错误和分段错误,我在运行 valgrind 时也有错误。
评论更适合我,表明我(希望)知道每个命令在做什么
#include <stdio.h>
#include <stdlib.h>
int main(void) {
typedef struct node {
int value;
struct node* next;
}
node;
//creates nodes for head, tmp, content
node* head = NULL;
node* tmp = NULL;
node* content = NULL;
head->next = content; //head node points to content
for (int i = 1; i <= 10; i++) {
content = malloc(sizeof(node)); //creates new node
content->value = i; //node data becomes i
tmp->next = content; //tmp node points to content node
tmp = tmp->next; //tmp node becomes next content node
content->next = NULL; //content node points to null
printf("%i ", content->value); //see node value
}
while (head != NULL) {
node* temp = head;
head = head->next;
free(temp);
}
return 0;
}
好的,让我们分解一下
逐节
#include <stdio.h>
#include <stdlib.h>
int main(void) {
// Data section
typedef struct node {
int value;
struct node* next;
}
node;
node* head = NULL; // a pointer to what will be the head of the list
node* content; // a utility pointer
node* temp; // a utility pointer
int i; // iteration var
// Code Section
for (i = 1; i <= 10; i++) {
if (head == NULL) {
// The list is empty when this condition triigers
// we initialize the first node and assign head to point to it
head = (node*)malloc(sizeof(node)); // allocate memory to the pointer. Also make sure the memory pointed to is of a node type
// This is important because it will allow us use -> operator to assign values to the pointed
// at memory
head -> value = i; // assign the value i to the value field of the pointed to memory
head -> next = NULL; // assign the next pointer of the pointed to memory to NULL
} else {
// iterate over the list till we reach the end.
// Once we do, assign more memory at the end, assign the memory a value and make it's next pointer be NULL
content = head;
while (content -> next != NULL) {
content = content -> next;
}
content -> next = (node*)malloc(sizeof(node));
content -> next -> value = i;
}
}
while (head != NULL) {
temp = head;
printf("Node data: %dn", temp -> value);
head = head->next;
free(temp);
}
return 0;
}
数据部分
// Data section
typedef struct node {
int value;
struct node* next;
}
node;
node* head = NULL; // a pointer to what will be the head of the list
node* content; // a utility pointer
node* temp; // a utility pointer
int i; // iteration var
当我学会用 C 编程时,如果你没有在函数顶部预先声明你的函数使用的变量,编译器会抱怨。出于某种原因,它现在可以工作了。
当您将某物声明为指针时,您只是在声明它。在将内存分配给指针之前,无法为其赋值。
代码部分(我不知道还能怎么称呼它)
// Code Section
for (i = 1; i <= 10; i++) {
if (head == NULL) {
// The list is empty when this condition triigers
// we initialize the first node and assign head to point to it
head = (node*)malloc(sizeof(node)); // allocate memory to the pointer. Also make sure the memory pointed to is of a node type
// This is important because it will allow us use -> operator to assign values to the pointed
// at memory
head -> value = i; // assign the value i to the value field of the pointed to memory
head -> next = NULL; // assign the next pointer of the pointed to memory to NULL
} else {
// iterate over the list till we reach the end.
// Once we do, assign more memory at the end, assign the memory a value and make it's next pointer be NULL
content = head;
while (content -> next != NULL) {
content = content -> next;
}
content -> next = (node*)malloc(sizeof(node));
content -> next -> value = i;
}
}
分配内存并确保分配内存属于特定类型的正确方法是使用malloc
并将其强制转换为适当的类型。正如您在某些行中看到的那样,例如content -> next = (node*)malloc(sizeof(node));
.确保分配的内存类型是使用类型转换(node*)
完成
的你做错了什么
head->next = content;
错了。head
在代码中出现此语句时NULL
。它没有可以指向任何内容的next
指针。tmp->next = content;
同上content = malloc(sizeof(node));
最适合我上面概述的类型转换- 正如其他人指出的那样,
malloc
可能由于各种原因而失败。它返回一个您应该检查的NULL
。 - 你从来没有真正让
head
成为你名单的头
head->next = content; //head node points to content
行没有任何意义。head
不指向任何内容(您为其分配了 null),内容也不指向,因此在这一点上说head->next = content
毫无意义。您需要在开始循环之前为头节点分配内存,或者在循环中添加条件,例如在为内容分配内存后if(head == NULL) head = content
。并检查以确保您的内存分配成功。