【问题标题】:linked list insertion missing链表插入丢失
【发布时间】:2020-08-22 01:02:26
【问题描述】:

嘿,我尝试构建一个链表,我构建了两个函数,一个用于插入,一个用于创建列表 但我在插入时有问题我认为是内存问题 我找不到 代码正在运行,但有时它会无缘无故地跳转到另一个函数,但我不知道为什么

感谢任何帮助/想法

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

typedef struct {

    int num;
    struct List* next;

}List;
// i think the problem is in here cant find it :(((
List* insert(List* head, int num)
{
    List* temp = (List*)malloc(sizeof(List));
    List* temp_head = head;
    temp->num = num;
    temp->next = NULL;
    if (head == NULL)
    {
        head = temp;
    }
    else
    {
        while (temp_head->next != NULL)
        {
            temp_head = temp_head->next;
        }
        temp_head->next = temp;
    }
    return head;
}
List* create_new_list(List* new_list)
{
    int number;
    printf("Enter numbers, to exit enter negative number\n");
    scanf_s("%d", &number);
    if (number <= 0)
    {
        return NULL;
    }
    new_list->num = number;
    new_list->next = NULL;
    scanf_s("%d", &number);
    while (number >= 0)
    {
        new_list=insert(new_list, number);
        scanf_s("%d", &number);
    }
    return new_list;
}

int main()
{
    List* head= (List*)malloc(sizeof(List));
    head=create_new_list(head);
}

【问题讨论】:

  • list* head = (list*)malloc(sizeof(list)); -> list *head = malloc (sizeof(List));(注意List中的'L',或者更好的list *head = malloc (sizeof *head);甚至更好,你不需要分配给list,例如@ 987654331@ 和 C 中,malloc 的返回不需要强制转换,没有必要。见:Do I cast the result of malloc?
  • 您还可以找到(按顺序)Singly Linked List (node only, no wrapper),然后使用包装器为 O(1) 插入提供 tail 指针 Singly Linked List of Integers (example)
  • 好的,我尝试制作 *head=NULL 但也没有帮助为什么 head=create_new_list(&head);?
  • 最大的问题在于(list*)malloc(sizeof(list)); 而不是(list*)malloc(sizeof(List));,当然还有list* head -> List* headlist* insert(list* head, int num) -> List* insert(List* head, int num)检查你的 L's.
  • 好的,我试过但没有帮助://

标签: c memory linked-list


【解决方案1】:

好的,你有很多问题,你的编译器应该告诉你它们在哪一行。对于初学者,您在List 的定义中缺少struct List,例如

typedef struct List {
    int num;
    struct List *next;
} List;

接下来,您需要修复使用 list 而不是 List 作为类型的每个位置,例如

List *insert (List *head, int num)

List *create_new_list (List *new_list)

    List *head = malloc(sizeof *head);

进行更改后,您将拥有(注意:我没有scanf_s,所以下面使用scanf):

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

typedef struct List {
    int num;
    struct List *next;
} List;

List *insert (List *head, int num)
{
    List *temp = malloc (sizeof *temp);
    List *temp_head = head;

    temp->num = num;
    temp->next = NULL;

    if (head == NULL)
        head = temp;
    else {
        while (temp_head->next != NULL)
            temp_head = temp_head->next;
        temp_head->next = temp;
    }

    return head;
}

List *create_new_list (List *new_list)
{
    int number;
    printf("Enter numbers, to exit enter negative number\n");
    scanf ("%d", &number);
    if (number <= 0)
    {
        return NULL;
    }
    new_list->num = number;
    new_list->next = NULL;
    scanf ("%d", &number);
    while (number >= 0)
    {
        new_list=insert(new_list, number);
        scanf("%d", &number);
    }
    return new_list;
}

void prnList (List *head)
{
    for (; head; head = head->next)
        printf (" %d", head->num);

    putchar ('\n');
}

int main()
{
    List *head = malloc(sizeof *head);
    head=create_new_list(head);
    prnList (head);
}

注意:我添加了prnList()函数)

使用/输出示例

$ ./bin/ll_scanf
Enter numbers, to exit enter negative number
1 2 3 4 5 -1
 1 2 3 4 5

替代方案 - 避免分配给 head

您可以简单地在main() 中使用head 自动存储持续时间而不是分配。例如,您可以这样做:

int main()
{
    List head = { .num = 0 };
    create_new_list(&head);
    prnList (&head);
}

这只是让您不必稍后再使用free(head),否则可以将其传递给任何需要它的函数,就像分配它一样。 注意:如果您采用这条路线,则不要分配来自create_new_list(&amp;head); 的返回值,因为上面的head 是结构的实例,而不是指向List 的指针。

检查一下,如果您有任何问题,请告诉我。

另请注意:'*' 带有变量名而不是类型(通常)。为什么?因为:

List* a, b, c;

没有声明三个指向List 的指针,而是声明一个指向List (a) 的指针和两个struct List (b, c) 的实例。使用:

List *a, b, c;

说清楚了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 2013-02-28
    相关资源
    最近更新 更多