指针C中的N链表

时间:2021-05-30 07:19:29

I wrote to following code which does not work. The application crashes on the print method. Any idea where it goes wrong?

我写了下面的代码不起作用。应用程序在打印方法上崩溃。知道哪里出错了吗?

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

typedef struct
{
    int item;
    struct LinkedList *Next;
} LinkedList;

int main()
{
    LinkedList vaz;
    vaz.item = 14123;
    vaz.Next = 0;
    add(&vaz,123);
    add(&vaz,9223);
    Print(&vaz);
    return 0;
}
void Print(LinkedList* Val)
{
    if(Val != 0){
        printf("%i\n",(*Val).item);
        Print((*Val).Next);
    }
}
void add(LinkedList *Head, int value)
{
    if((*Head).Next == 0 )
    {
        LinkedList sab;
        sab.item = value;
        sab.Next = 0;
        (*Head).Next = &sab;
    }
    else{
       add((*Head).Next,value);
    }
}

4 个解决方案

#1


2  

Your add method should allocate memory dynamically, you are using automatic allocation (local variables), which is freed when you 'leave' the function. This will cause an undefined behavior when you try to access this memory later on.

你的add方法应该动态分配内存,你正在使用自动分配(局部变量),当你“离开”函数时它会被释放。当您稍后尝试访问此内存时,这将导致未定义的行为。

void add(LinkedList *Head, int value)
{
    if((*Head).Next == 0 )
    {
        LinkedList sab; <-- need to be allocated dynamically
        sab.item = value;
        sab.Next = 0;
        (*Head).Next = &sab;
    }
    else{
       add((*Head).Next,value);
    }
}

#2


1  

You did not allocate heap memory to the Linked list you create. I think you should allocate memory on the heap when you add elements to the linked list. The linked list variable 'sab' is local to the method and so the memory get deallocated as soon as the function goes out of scope.

您没有将堆内存分配给您创建的链接列表。我认为在向链表添加元素时应该在堆上分配内存。链表变量'sab'是方法的本地变量,因此一旦函数超出范围,内存就会被释放。

To add an element, you need to do as follows:

要添加元素,您需要执行以下操作:

LinkedList *sab = (struct LinkedList*) malloc (sizeof(struct LinkedList));
if( sab != NULL )
{
    sab->item = value;
    sab->next = NULL;
}

For more details, you can refer: http://www.cprogramming.com/snippets/source-code/singly-linked-list-insert-remove-add-count

有关详细信息,请参阅:http://www.cprogramming.com/snippets/source-code/singly-linked-list-insert-remove-add-count

#3


1  

add should dynamically allocate memory as follows:

add应该动态分配内存,如下所示:

void add(LinkedList *Head, int value)
{
    if (Head == NULL)
        return;

    if((*Head).Next == NULL )
    {
        LinkedList *sab = malloc(sizeof(LinkedList));

        if ( sab == NULL ) {
            // Error: ran out of memory (really shouldn't happen, but could)
            return;
        }

        (*sab).item = value;
        (*sab).Next = NULL;
        (*Head).Next = sab;
    }
    else{
       add((*Head).Next,value);
    }
}

Also, the recursive call is "overkill" in this case. A simple loop would suffice:

此外,在这种情况下,递归调用是“过度杀伤”。一个简单的循环就足够了:

void add(LinkedList *Head, int value)
{
    if (Head == NULL)
        return;

    while ((*Head).Next != NULL)
        Head = (*Head).Next;

    LinkedList *sab = malloc(sizeof(LinkedList));

    if ( sab == NULL ) {
        // Error: ran out of memory (really shouldn't happen, but could)
        return;
    }

    (*sab).item = value;
    (*sab).Next = NULL;
    (*Head).Next = sab;
}

I'd likewise avoid the recursive call for printing:

我同样避免了打印的递归调用:

void Print(LinkedList* Val)
{
    while (Val != NULL) {
        printf("%i\n", (*Val).item);
        Val = (*Val).Next;
    }
}

If you have a function which removes items from the list, you need to ensure that you do a free on the pointer to that item that's removed.

如果您有一个从列表中删除项目的函数,则需要确保对指向该项目的指针执行*删除操作。

#4


1  

Your LinkedList *Head doesn't have any memory First assign memory to it as given below-

你的LinkedList * Head没有任何内存首先为它分配内存,如下所示 -

LinkedList *Head = (struct LinkedList*)malloc(sizeof(struct LinkedList));

#1


2  

Your add method should allocate memory dynamically, you are using automatic allocation (local variables), which is freed when you 'leave' the function. This will cause an undefined behavior when you try to access this memory later on.

你的add方法应该动态分配内存,你正在使用自动分配(局部变量),当你“离开”函数时它会被释放。当您稍后尝试访问此内存时,这将导致未定义的行为。

void add(LinkedList *Head, int value)
{
    if((*Head).Next == 0 )
    {
        LinkedList sab; <-- need to be allocated dynamically
        sab.item = value;
        sab.Next = 0;
        (*Head).Next = &sab;
    }
    else{
       add((*Head).Next,value);
    }
}

#2


1  

You did not allocate heap memory to the Linked list you create. I think you should allocate memory on the heap when you add elements to the linked list. The linked list variable 'sab' is local to the method and so the memory get deallocated as soon as the function goes out of scope.

您没有将堆内存分配给您创建的链接列表。我认为在向链表添加元素时应该在堆上分配内存。链表变量'sab'是方法的本地变量,因此一旦函数超出范围,内存就会被释放。

To add an element, you need to do as follows:

要添加元素,您需要执行以下操作:

LinkedList *sab = (struct LinkedList*) malloc (sizeof(struct LinkedList));
if( sab != NULL )
{
    sab->item = value;
    sab->next = NULL;
}

For more details, you can refer: http://www.cprogramming.com/snippets/source-code/singly-linked-list-insert-remove-add-count

有关详细信息,请参阅:http://www.cprogramming.com/snippets/source-code/singly-linked-list-insert-remove-add-count

#3


1  

add should dynamically allocate memory as follows:

add应该动态分配内存,如下所示:

void add(LinkedList *Head, int value)
{
    if (Head == NULL)
        return;

    if((*Head).Next == NULL )
    {
        LinkedList *sab = malloc(sizeof(LinkedList));

        if ( sab == NULL ) {
            // Error: ran out of memory (really shouldn't happen, but could)
            return;
        }

        (*sab).item = value;
        (*sab).Next = NULL;
        (*Head).Next = sab;
    }
    else{
       add((*Head).Next,value);
    }
}

Also, the recursive call is "overkill" in this case. A simple loop would suffice:

此外,在这种情况下,递归调用是“过度杀伤”。一个简单的循环就足够了:

void add(LinkedList *Head, int value)
{
    if (Head == NULL)
        return;

    while ((*Head).Next != NULL)
        Head = (*Head).Next;

    LinkedList *sab = malloc(sizeof(LinkedList));

    if ( sab == NULL ) {
        // Error: ran out of memory (really shouldn't happen, but could)
        return;
    }

    (*sab).item = value;
    (*sab).Next = NULL;
    (*Head).Next = sab;
}

I'd likewise avoid the recursive call for printing:

我同样避免了打印的递归调用:

void Print(LinkedList* Val)
{
    while (Val != NULL) {
        printf("%i\n", (*Val).item);
        Val = (*Val).Next;
    }
}

If you have a function which removes items from the list, you need to ensure that you do a free on the pointer to that item that's removed.

如果您有一个从列表中删除项目的函数,则需要确保对指向该项目的指针执行*删除操作。

#4


1  

Your LinkedList *Head doesn't have any memory First assign memory to it as given below-

你的LinkedList * Head没有任何内存首先为它分配内存,如下所示 -

LinkedList *Head = (struct LinkedList*)malloc(sizeof(struct LinkedList));