Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 双链表程序运行时错误_C - Fatal编程技术网

C 双链表程序运行时错误

C 双链表程序运行时错误,c,C,我没有太多的c编程经验。我不明白这段代码中的错误是什么。在将此代码放到网上之前,我已经尝试了5次。请帮忙。 我在这里实现了一个双链接列表,其中有两个函数用于向列表中添加节点,还有一个函数用于显示整个列表。成功编译后,如果我尝试添加一个节点,那么程序将意外结束 #include<stdio.h> #include<stdlib.h> #include<malloc.h> struct node { int data; struct node* n

我没有太多的c编程经验。我不明白这段代码中的错误是什么。在将此代码放到网上之前,我已经尝试了5次。请帮忙。 我在这里实现了一个双链接列表,其中有两个函数用于向列表中添加节点,还有一个函数用于显示整个列表。成功编译后,如果我尝试添加一个节点,那么程序将意外结束

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
    int data;
    struct node* next;
    struct node* prev;
};
void display_list(struct node* ptr);    
void add_node(struct node* ptr)
{
    if(ptr->next==NULL)
    {
        ptr=(struct node*)malloc(sizeof(struct node));
        (ptr->next)->next=NULL;
        (ptr->next)->prev=ptr;
    }
    else        
    {   //traverse the list
        while(ptr->next!=NULL)
        {
            ptr=ptr->next;
        }
        (ptr->next)=(struct node*)malloc(sizeof(struct node));
        (ptr->next)->next=NULL;
        (ptr->next)->prev=ptr;
    }
    printf("\nEnter data : ");
    scanf("%d",((ptr->next)->data));
    display_list(ptr);
}
void display_list(struct node* ptr)
{
    if(ptr->next==NULL)
    {
        printf("%d\n",ptr->data);
    }
    else
    {
        //traverse the list and display each node
        while(ptr->next!=NULL)
        {
            printf("%d--->>>---",ptr->data);
            ptr=ptr->next;
        }
            //display last node
        printf("%d",ptr->data);
    }
}
int main()
{
    int choice;
    struct node* start=NULL;
    again:
    printf("\n1) Add node");
    printf("\n2) Display list");
    scanf("%d",&choice);
    if(choice==1)
        add_node(start);
    else if(choice==2)
        display_list(start);
    else 
        goto again;
    return 0;
}
#包括
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
结构节点*prev;
};
无效显示列表(结构节点*ptr);
void add_节点(结构节点*ptr)
{
如果(ptr->next==NULL)
{
ptr=(结构节点*)malloc(sizeof(结构节点));
(ptr->next)->next=NULL;
(ptr->next)->prev=ptr;
}
其他的
{//遍历列表
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
(ptr->next)=(结构节点*)malloc(sizeof(结构节点));
(ptr->next)->next=NULL;
(ptr->next)->prev=ptr;
}
printf(“\n输入数据:”);
scanf(“%d”)((ptr->next)->数据);
显示列表(ptr);
}
无效显示列表(结构节点*ptr)
{
如果(ptr->next==NULL)
{
printf(“%d\n”,ptr->data);
}
其他的
{
//遍历列表并显示每个节点
while(ptr->next!=NULL)
{
printf(“%d-->-->--”,ptr->data);
ptr=ptr->next;
}
//显示最后一个节点
printf(“%d”,ptr->data);
}
}
int main()
{
智力选择;
结构节点*start=NULL;
再一次:
printf(“\n1)添加节点”);
printf(“\n2)显示列表”);
scanf(“%d”,选择(&C);
如果(选项==1)
添加_节点(开始);
else if(选项==2)
显示列表(开始);
其他的
再次转到;
返回0;
}

在您的
add_节点
函数中,您有一个
if
语句来检查
ptr->next
是否为
NULL
,但您从未真正检查
ptr
本身是否为
NULL

main
函数中,您可以看到,第一次调用
add_node
时,参数确实是
NULL
,因此在该函数中,
ptr
NULL
,一旦代码尝试检查
ptr->next
,您就会遇到问题


既然您在评论中提出了很好的要求,我将向您展示我对代码重组的意思

现在,您的
add_node
实现将
struct node*
作为参数。问题是当你有这样的东西时:

struct node* ptr = NULL;
add_node(ptr);
void add_node(struct node ** head) {
    struct node * ptr = *head;

    // use ptr like you had before in the old implementation

    *head = ptr; // updating head.
                 // If ptr has changed, this will update head
                 // If it hasn't, then no harm
}
即使修改
add_node
以正确处理
NULL
参数,
ptr
本身的值在
add_node
返回后也不会更改。一种方法是让
add_node
取而代之的是
struct node**
。大概是这样的:

struct node* ptr = NULL;
add_node(ptr);
void add_node(struct node ** head) {
    struct node * ptr = *head;

    // use ptr like you had before in the old implementation

    *head = ptr; // updating head.
                 // If ptr has changed, this will update head
                 // If it hasn't, then no harm
}
那样的话,如果你有

struct node *foo;
add_node(&foo);

add_node
末尾的
*head=ptr
行将用正确的值更新变量,这次
foo
将在
add_node
返回时更新。

add_node
函数中,您有一个
if
语句来检查
ptr->next
是否为
NULL
,但您从未真正检查
ptr
本身是否为
NULL

main
函数中,您可以看到,第一次调用
add_node
时,参数确实是
NULL
,因此在该函数中,
ptr
NULL
,一旦代码尝试检查
ptr->next
,您就会遇到问题


既然您在评论中提出了很好的要求,我将向您展示我对代码重组的意思

现在,您的
add_node
实现将
struct node*
作为参数。问题是当你有这样的东西时:

struct node* ptr = NULL;
add_node(ptr);
void add_node(struct node ** head) {
    struct node * ptr = *head;

    // use ptr like you had before in the old implementation

    *head = ptr; // updating head.
                 // If ptr has changed, this will update head
                 // If it hasn't, then no harm
}
即使修改
add_node
以正确处理
NULL
参数,
ptr
本身的值在
add_node
返回后也不会更改。一种方法是让
add_node
取而代之的是
struct node**
。大概是这样的:

struct node* ptr = NULL;
add_node(ptr);
void add_node(struct node ** head) {
    struct node * ptr = *head;

    // use ptr like you had before in the old implementation

    *head = ptr; // updating head.
                 // If ptr has changed, this will update head
                 // If it hasn't, then no harm
}
那样的话,如果你有

struct node *foo;
add_node(&foo);

add_node
末尾的
*head=ptr
行将用正确的值更新变量,这次
foo
将在
add_node
返回时更新。

add_node
函数中,您有一个
if
语句来检查
ptr->next
是否为
NULL
,但您从未真正检查
ptr
本身是否为
NULL

main
函数中,您可以看到,第一次调用
add_node
时,参数确实是
NULL
,因此在该函数中,
ptr
NULL
,一旦代码尝试检查
ptr->next
,您就会遇到问题


既然您在评论中提出了很好的要求,我将向您展示我对代码重组的意思

现在,您的
add_node
实现将
struct node*
作为参数。问题是当你有这样的东西时:

struct node* ptr = NULL;
add_node(ptr);
void add_node(struct node ** head) {
    struct node * ptr = *head;

    // use ptr like you had before in the old implementation

    *head = ptr; // updating head.
                 // If ptr has changed, this will update head
                 // If it hasn't, then no harm
}
即使修改
add_node
以正确处理
NULL
参数,
ptr
本身的值在
add_node
返回后也不会更改。一种方法是让
add_node
取而代之的是
struct node**
。大概是这样的:

struct node* ptr = NULL;
add_node(ptr);
void add_node(struct node ** head) {
    struct node * ptr = *head;

    // use ptr like you had before in the old implementation

    *head = ptr; // updating head.
                 // If ptr has changed, this will update head
                 // If it hasn't, then no harm
}
那样的话,如果你有

struct node *foo;
add_node(&foo);

add_节点
末尾的
*head=ptr
行将用正确的值更新变量,这次
foo
将在
add_节点
返回时更新。

add_节点中