c - 使用链表获取段错误

标签 c linked-list segmentation-fault

关闭。这个问题需要debugging details .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

4年前关闭。




Improve this question




老实说,当谈到链表时,我不知道自己在做什么。这里的某些东西导致了段错误错误,但是我对链表的了解还不够,无法知道它可能在哪里,更不用说它们令人困惑。我有用于调试的代码,但在发布之前将其取出。代码似乎到达了 main 中的 for 语句,我得到了错误。用户输入他们想要打印的节点数量,其中许多节点用随机数打印,每行打印十个。

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

struct node_def
{
    int data;
    struct node_def *next;
};
typedef struct node_def node;

node *makeNode (int val);
node *insertFront(node *head,node *new);
void printList(node *head);

int numNodes = 0;

int main()
{
    srand(time(0));

    int i = 0;

    node *head = NULL;
    node *new = NULL;

    printf("How many nodes? ", numNodes);
    scanf("%d", &numNodes);
    printf("\n");

    head = insertFront(head, new);
    for(i = 0; i < numNodes; ++i)
    {
        makeNode(numNodes);
    /*  printList(head);*/
        /*insertFront(head, new);*/
    }
    printList(head);

    printf("\n");
    return 0;
}
node *makeNode (int val)
{

    node *head = NULL;

    node *new = malloc(sizeof(node));
    new->data = rand() % 10000;
    new->next = NULL;
    if (head == NULL)
        head = new;



    return new;
}
node *insertFront(node *head, node *new)
{

    new->next = head;

    return head;
}
void printList(node *head)
{
    int j = 0;
    for(j = 0; j < numNodes; ++j)
    {
        while (head != NULL)
        {
            printf(" %4d", head->data);
            head = head->next;
        }
        if(j % 10 == 0)
            printf("\n");
    }
    return;
}

最佳答案

该程序在这里有多个问题。基本上你写的代码根本没有实现链表。

  • 此函数 makenode 不正确。
    node *makeNode (int val)
    {
    
        node *head = NULL;
    
        node *new = malloc(sizeof(node));
        new->data = rand() % 10000;
        new->next = NULL;
        if (head == NULL)
            head = new;
    
        return new;
    }
    

    在这段代码中,head 被初始化为 NULL。所以 if (head == NULL) 将永远是真的。为什么不使用时需要 val param to makeNode?您可以将其更新为以下代码:

    我的建议:
    node *makeNode ()
    {
        node *new = malloc(sizeof(node));
        new->data = rand() % 10000;
        new->next = NULL;
        return new;
    }
    

    您不必在这里检查 head = NULL 。
  • 您正在 main 中执行 NULL 指针取消引用(这就是您遇到段错误的原因)。
    node *head = NULL;
    node *new = NULL;
    
    head = insertFront(head, new);
    

    这里 head 和 new 都是 NULL。现在在 insertFront , new->next会段错误。
    insertFront的参数, , 尚未初始化。你不认为在插入之前应该先为头部分配内存吗?

    我的建议:(在主要功能中)
    node *head = makeNode();
    for(i = 0; i < numNodes; ++i)
    {
         node *new = makeNode();
         head = insertFront(head, new);
    }
    
  • printList 函数也是不正确的。我建议你看一次并调试它。我不想逐行复习。

    但这是你真正应该做的:
    void printList(node *head)
    {
        if (head == NULL) return;
    
        node *temp = head;
    
        while (temp != NULL)
        {
            printf(" %d\n", temp->data);
            temp = temp->next;
        }
    }
    

    就是这么简单。我建议您仔细查看代码并尝试逐步调试它。

  • 还要确保在列表中插入节点之前,为节点分配内存。走笔和纸的方式并跟踪您的代码步骤和步骤。这就是我使用链表的方式。

    关于c - 使用链表获取段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43302202/

    相关文章:

    c - 是否有 gcc 编译器选项可帮助捕获形式参数和实际参数类型不匹配?

    c - C 内联汇编中的 RDTSC 导致段错误!

    c++ - Qt - QDrag exec 上的段错误

    c - 使用 DDD 调试器 c 时出错,没有 printf.c 的源代码

    c - C中的全局结构体

    c - 为单链表编写适当的push() 函数。

    arrays - 高效的数据结构,用于快速随机访问、搜索、插入和删除

    c - 如何从C中的链表中正确释放内存?

    c++ - 创建 shared_ptr 似乎会导致段错误

    java - 如何测量C/C++/Java程序在执行过程中使用的内存?