双链表程序运行时错误

时间:2013-10-13 06:35:01

标签: c

我对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;
}

4 个答案:

答案 0 :(得分:3)

add_node函数中,您有if语句来检查ptr->next是否为NULL,但您实际上从未检查过ptr < em>本身是NULL

main函数中,您可以看到第一次调用add_node时,参数确实是NULL,因此第一次在该函数中{{1} } ptr,一旦您的代码尝试检查NULL,就会出现问题。


由于您在评论中提出了很好的建议,我将向您展示我对代码重组的意义。

目前,ptr->next的实施需要add_node作为参数。问题在于你有这样的事情:

struct node *

即使您修改struct node* ptr = NULL; add_node(ptr); 以正确处理add_node参数,NULL本身的值也会在ptr返回后发生变化。一种方法是让add_node代替add_node。像这样:

struct node **

那样,如果你有像

这样的东西
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); 末尾的*head = ptr行将使用正确的值更新变量,这次add_node 更新{ {1}}返回。

答案 1 :(得分:0)

如果你在这里替换你的行:

struct node* start=NULL;

使用创建根节点的代码:

struct node* start = (struct node*)malloc(sizeof(struct node));

if (start)
{
   start->prev = NULL;
   start->next = NULL;
}

您将传递一个有效的节点,然后可以将其添加到'。

答案 2 :(得分:0)

最后得到了这个问题的答案,导致代码在这里。我在真正问题所在的区域添加了评论:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
/////////////////////////////////////////////////////////////////////////////////////
struct node
{
    int data;
    struct node* next;
    struct node* prev;
};
//////////////////////////////////////////////////////////////////////////////////////

***//now add node function also returns a pointer of type node***

struct node* insert_node(struct node* start,int item)
{
    struct node* new_node=(struct node*)malloc(sizeof(struct node));
    new_node->data=item;
    //no availabel heap
    if(new_node==NULL)
    {
        printf("\nNo availabel memory!");
        exit(0);
    }
    //if there is no node in the linked list
    if(start==NULL)
    {
        start=new_node;
        start->next=NULL;
        start->prev=NULL;
    }
    //if there is one node in the linked list
    else
    {
        new_node->next=start;
        new_node->prev=NULL;
        start=new_node;             //start now points to new node
    }
    return start;
}
void display_list(struct node* ptr)    //display function has also been modified
{
    struct node* temp=ptr;
    while(temp)
    {
        printf("%d-->>--",temp->data);
        temp=temp->next;
    }
}
////////////////////////////////////////////////////////////////////////////////////////
int main()
{
    int i;
    struct node* start=NULL;
    for(i=0;i<10;i++)
    {
        start=insert_node(start,i+1);   //function must return a pointer 
            //because the function uses a copy of the pointer and does not modify the
            //original pointer in the main function
    }
    display_list(start);
    return 0;
}

答案 3 :(得分:0)

您正在解决无效指针

    ptr=(struct node*)malloc(sizeof(struct node));
    (ptr->next)->next=NULL;
    (ptr->next)->prev=ptr;

你创建了带有垃圾的ptr,然后你访问了它的成员ptr->next->next

相关问题