简单的链表程序无法正常工作

Simple linked list program just isn't working

我只是触及节点和链表的表面,所以我一直在尝试创建一个链表来打印 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;
}

head->next = content; //head node points to content 行没有任何意义。 head 不指向任何内容(您将其指定为 null),内容也不指向任何内容,因此此时说 head->next = content 毫无意义。您需要在开始循环之前将内存分配给头节点,或者在将内存分配给内容之后在循环中添加一个条件,例如 if(head == NULL) head = content 。并检查以确保您的内存分配成功。

好的,让我们分解一下

逐节

#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*)

完成的

你做错了什么

  1. head->next = content; 是错误的。 head 在您的代码中执行此语句时是 NULL。它没有可以指向任何东西的 next 指针。
  2. tmp->next = content;同上
  3. content = malloc(sizeof(node)); 最适合我上面概述的 typecast
  4. 正如其他人所指出的,malloc 可能因多种原因而失败。它 returns 一个 NULL 你应该检查一下。
  5. 你从来没有真正让 head 成为你列表的头部