c - 使用 C 的链接列表中的段错误(核心转储)

标签 c data-structures linked-list segmentation-fault

我想要一个链表数组,显然每个链接应该有单独的头节点。首先,作为示例,我从一个数组元素开始。我将链表存储到 current[0] 中。但它给出了段错误。如果我使用 Node *current 它将创建一个列表并且工作正常。但是,我想将列表存储在数组中。代码有什么问题?

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

Node *current[20];

void insert_beg_of_list(Node *current[0], int data);

void print_list(Node *current[0]);

void insert_beg_of_list(Node *current[0], int data) {

    //keep track of first node
    Node *head = current[0];

    while(current[0]->next != head) {
        current[0] = current[0]->next;
    }
    current[0]->next = (Node*)malloc(sizeof(Node));
    current[0] = current[0]->next;
    current[0]->data = data;
    current[0]->next = head;
}

void print_list(Node *current[0]) {

    Node *head = current[0];
    current[0] = current[0]->next;
    while(current[0] != head){
        printf(" %d ", current[0]->data);
        current[0] = current[0]->next;
    }

}

int main() {

    Node *head = (Node *)malloc(sizeof(Node));
    head->next = head;  

    int data = 0 ;
    int usr_input = 0;
    int i;
    int m;
    int j;

        scanf("%d", &usr_input);

        for (i=0; i<usr_input; i++) {

            scanf("%d", &data);
            insert_beg_of_list(head, data);

        }

            printf("The list is ");
            print_list(head);
            printf("\n\n");

    return 0;
}

最佳答案

我认为你混合了全局数组 current的使用。将您的代码更改为:

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

void insert_beg_of_list(Node *current, int data);

void print_list(Node *current);

void insert_beg_of_list(Node *current, int data) {

    //keep track of first node
    Node *head = current;

    while(current->next != head) {
        current = current[0]->next;
    }
    current->next = malloc(sizeof(Node));
    if (current->next == NULL)
        return;
    current = current->next;
    current->data = data;
    current->next = head;
}

void print_list(Node *current) {

    Node *head = current;
    current = current->next;
    while(current != head){
        printf(" %d ", current->data);
        current = current->next;
    }

}

int main() {

    Node *current[20];
    Node *head = malloc(sizeof(Node));
    if (head == NULL)
        return;

    head->next = head;  

    int data = 0 ;
    int usr_input = 0;
    int i;
    int m;
    int j;

    scanf("%d", &usr_input);

    for (i = 0; i < usr_input; i++) {
        scanf("%d", &data);
        insert_beg_of_list(head, data);
    }

    //assign the newly created pointer to a place in the array
    current[0] = head;

    printf("The list is ");
    print_list(head);
    printf("\n\n");

    return 0;
}

请记住参数 current在你的函数的原型(prototype)和声明中是 not the same作为数组 current在您的main中创建功能。我只是保留了原来的名称。

注意:您应该使用 head->next 执行某些操作指针,初始化它。

<小时/>

另请阅读this link为什么不转换 malloc 的结果和 another one为什么您应该检查其结果。

关于c - 使用 C 的链接列表中的段错误(核心转储),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43782691/

相关文章:

c - 从 C 中的前缀表达式构建解析树

c - 使用命令行参数的结构链接列表

c - write() 没有在 C 语言的 Windows 上写入正确数量的字节

c - 如何有效地交错 8 个 __int16 数字中的位?

c++ - 如果编译器内联通过函数指针调用的函数会发生什么

java - 按值从高到低对 Hashmap 进行排序

c - 如何用C语言在windows和linux下清屏

c - 为什么数据不保存到结构中?

c - 拆分随机链表然后对它们进行排序

c++ - C++中的结构体是按顺序分配内存的吗?不知何故每次都能得到指针比较的正确答案