我只是抓住了节点和链表的表面,所以我一直在尝试创建一个打印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;
}
答案 0 :(得分:0)
行head->next = content; //head node points to content
没有任何意义。 head
没有指向任何内容(你将其指定为null),内容也没有,所以说head->next = content
在这一点上毫无意义。您需要在开始循环之前为头节点分配内存,或者在为内容分配内存后在循环中添加类似if(head == NULL) head = content
的条件。并检查以确保您的内存分配成功。
答案 1 :(得分: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: %d\n", 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
成为你名单的负责人