创建链表会出现分段错误(核心转储)错误

数字

以下代码用于创建单向链表并使用两个函数create_ll()display()显示它

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

struct Node
        {
            int data;
            struct Node* next;

        };

struct Node* create_ll(struct Node*);
struct Node* display(struct Node*);
int main(int argc, char *argv[])
{
    struct Node* start=NULL;
    int option=0;
    do 
    {
        printf("\t\t\t*******MAIN MENU********");
        printf("\n\t1.Create a list");
        printf("\n\t2.Display the list");
        printf("\n\t3.Exit");
        scanf("%d",&option);
        switch(option)
        {
            case 1: start=create_ll(start);
                printf("\nLinked list created");
                break;
            case 2: start=display(start);
                break;
        };
    }while(option!=3);

    return 0;

}
struct Node* create_ll(struct Node* start)
{
    struct Node *new_node,*ptr;
    int num;
    printf("Enter -1 to end");
    printf("\nEnter data");
    scanf("%d",&num);
    while(num!=-1)
    {
        printf("Creating Node....");
        new_node=(struct Node*)malloc(sizeof(struct Node));

        if(new_node!=NULL)
        {
            new_node->data=num;
            if (start==NULL)
            {
                new_node->next=NULL;
                start=new_node;
            }
            else
            {
                ptr=start;
                while(ptr->next!=NULL)
                    ptr=ptr->next;
                ptr->next=new_node;
                new_node->next=NULL;
            }
        }
        else
        {
            printf("\nOut of Memory!");
        }
        printf("\nEnter data");
        scanf("%d",&num);
    }
    return start;
}
struct Node* display(struct Node* start)
{
    struct Node *ptr;
    ptr=start;
    while(ptr->next!=NULL)
    {
        printf("%d\t",ptr->data);
        ptr=ptr->next;
    }
    return start;
}

它在 Ubuntu 上的 gcc 编译器上成功编译,没有任何错误。但是,运行后会出现分段错误(核心转储)错误。

gdb 显示故障在第 59 行:-

$ gdb -q LS.out
Reading symbols from LS.out...done.
(gdb) run
Starting program: /home/arpit/Desktop/Ds/LS.out 
            *******MAIN MENU********
    1.Create a list
    2.Display the list1
Enter -1 to end
Enter data 4

Program received signal SIGSEGV, Segmentation fault.
0x00000000004007de in create_ll (start=0x0) at LS.c:59
59        while(ptr->next!=NULL)
狒狒

我想你有一个错字。更改if (start=NULL)if (start == NULL)在 C 中,=是赋值运算符,==用于比较。

当你这样做时if (start=NULL),变量start被赋值NULL并且if块不会被执行,因为赋值表达式返回右侧的任何内容。之后,您ptr将变为NULL,然后您ptr导致分段错误else块 ( while(ptr->next!=NULL)) 中取消引用

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

创建线程时出现分段错误(核心转储)错误

为什么在评论 malloc() 调用时会出现分段错误(核心转储)?

核心转储分段错误

执行代码时出现分段错误(核心转储)错误

strcmp 出现分段错误(核心转储)错误

使用 apt 升级时出现分段错误(核心转储)

如何避免出现分段核心转储错误?

运行代码时出现核心转储/分段错误

信号:分段错误(核心转储)错误

尝试将浮点数读入 C++ 中的链表时出现分段错误(核心转储)

fclose() 以“分段错误(核心转储)”结尾

OSMNx,分段错误(核心转储)

C - 获取分段错误(核心转储)

RPC:分段错误(核心转储)

C ++:分段错误(核心转储)问题

分段错误(核心转储)需要建议

分段错误(核心转储)C ++-指针

分段错误(核心转储)C++

C 中的分段错误(核心转储)

分段错误(核心转储)C

多线程分段错误(核心转储)

如何摆脱分段错误(核心转储)

带有链表基本实现的 C++ 中的分段错误(核心转储)

为什么会出现此错误?-分段错误(核心已转储)

运行已编译的a.out后出现分段错误(核心转储)错误

声明int变量时出现分段错误(核心转储)错误

尝试从数组复制时出现分段错误(核心转储)错误

将cin字符串转换成node-> name时,为什么会出现分段错误(核心转储)?

如何修复C中的分段错误(核心转储)错误