简体   繁体   English

调试 c 中的链表数组

[英]debug an array of linked lists in c

I'm having a trouble in my project, at first I made one linked list and it worked properly but then I had to edit it and make it an array of linked list, but it just stores in a wrong way I guess, here is my code, what's wrong with it?我在我的项目中遇到了麻烦,起初我制作了一个链表并且它工作正常,但后来我不得不编辑它并使其成为链表数组,但我猜它只是以错误的方式存储,这里是我的代码,有什么问题? . . Thanks is advanced.谢谢先进。

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

struct node
{
    int coeff;
    int power;
    struct node* Next;
};

  
void Insert(int X,int Y, struct node* L, struct node* P)
{
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
    temp->coeff = X;
    temp->power = Y;
    temp->Next = P->Next;
    P->Next = temp;
}

   
void PrintList(struct node* L)
{
    struct node* P = L;
            printf("%d %d ",P->coeff,P->power);
            printf("\n");
             P=P->Next;
}


int main()
{

    struct node* head[3] ;
    for(int q =0 ; q!= 3 ; q++)
    {

        head[q] = malloc(sizeof(struct node));

    }

   

        Insert(1,2,head[0],&head[0]);
        Insert(2,3,head[0],&head[0]);
        Insert(1,2,head[1],&head[1]);
        Insert(2,2,head[1],&head[1]);

      
    

for(int i=0 ;i!=3;i++){
        
PrintList(head[i]);

}



    return 0;
}

In a LinkedList there is only 1 head.在 LinkedList 中只有 1 个头。 You have allocated 3 different heads and are trying to add nodes to these heads.您已经分配了 3 个不同的磁头,并尝试将节点添加到这些磁头。 So the following answer assumes you intend to have 3 different LinkedLists with each list's head in an array.因此,以下答案假设您打算将 3 个不同的 LinkedLists 与每个列表的头部放在一个数组中。

The memory from malloc() in this line此行中来自malloc()的 memory

head[q] = malloc(sizeof(struct node));

was uninitialized.未初始化。 Using calloc() appeared more prudent here.在这里使用calloc()显得更加谨慎。 So,所以,

head[q] = calloc(sizeof(struct node), 1); //Initialize everything to 0

Next, one of the arguments in your Insert() was redundant and also didn't go with your calling code.接下来,您的Insert()中的 arguments 之一是多余的,并且您的调用代码也没有 go 。 It is of the signature它是签名的

void Insert(int ,int , struct node* , struct node*)

but you're invoking it with但你正在调用它

void Insert(int ,int , struct node* , struct node**)

Dropping the last argument should be fine, I guess, in this case since you aren't modifying the incoming struct node* pointer but its contents instead.我猜,在这种情况下,删除最后一个参数应该没问题,因为您没有修改传入的struct node*指针,而是修改了它的内容。 In case you were changing the struct node* to point to something else within the Insert() function (eg P = temp; etc.), then passing in a struct node** would have more meaning.如果您将struct node*更改为指向Insert() function 中的其他内容(例如P = temp;等),那么传入struct node**将具有更多意义。 So, changing your code to因此,将您的代码更改为

void Insert(int X,int Y, struct node* P)
{
    struct node* temp;
    temp = (struct node*)malloc(sizeof(struct node));
    temp->coeff = X;
    temp->power = Y;
    temp->Next = P->Next;
    P->Next = temp;
}

And the calling code to和调用代码

Insert(2,3,head[0]);
Insert(1,2,head[0]);
Insert(1,2,head[1]);
Insert(2,2,head[1]);

Next, changing your PrintList to actually traverse the LinkedList:接下来,更改您的PrintList以实际遍历 LinkedList:

void PrintList(struct node* L)
{
    struct node* P = L;
    while(P) {
        printf("%d %d\n",P->coeff,P->power);
        P=P->Next;
    }
}

Apart from that, I just added some logging to better clarify the LinkedList being printed.除此之外,我只是添加了一些日志记录以更好地阐明正在打印的 LinkedList。

for(int i=0 ;i!=3;i++){
    printf("Printing list %d:\n", i);
    PrintList(head[i]);
    printf("*****\n");
}

This gives the output:这给出了 output:

Printing list 0:
0 0
1 2
2 3
*****
Printing list 1:
0 0
2 2
1 2
*****
Printing list 2:
0 0
*****

Obviously, all the head of the LinkedLists are 0 initialized and hence show up as 0 0 .显然,LinkedLists 的所有head都是 0 初始化的,因此显示为0 0 They could be initialized as deemed fit, or could be dropped from the PrintList() function altogether, depending on your program's expectations.它们可以被初始化为合适的,或者可以完全从PrintList() function 中删除,具体取决于您的程序的期望。

When working on a C project you should always enable compiler warnings.在处理 C 项目时,您应该始终启用编译器警告。 If you do the compiler will issue a warning like如果你这样做,编译器会发出警告,如

warning: passing argument 4 of ‘Insert’ from incompatible pointer type [-Wincompatible-pointer-types]

This is just the first problem in your code.这只是代码中的第一个问题。 You also have uninitialized fields in the nodes you allocate in the main function.您在主 function 中分配的节点中也有未初始化的字段。

Instead of having a function which modifies the list, it is more flexible to define a function which adds a new node to a list and returns the new list.与修改列表的 function 不同,定义 function 更灵活,它将新节点添加到列表并返回新列表。 Below is an alternative approach which also uses convenience macro functions for allocating memory and calculating the length of an array.下面是另一种方法,它也使用方便的宏函数来分配 memory 并计算数组的长度。 The code also hides the pointer behind a type definition and defines a function which frees a list.该代码还将指针隐藏在类型定义后面,并定义了一个释放列表的 function。

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

#define LEN(array) ((int) (sizeof (array) / sizeof (array)[0]))

#define NEW_ARRAY(pointer, length) \
    { \
        (pointer) = malloc(((size_t) length) * sizeof (pointer)[0]); \
        if ((pointer) == NULL) { \
            fprintf(stderr, "Allocating memory with malloc failed: %s\n", strerror(errno)); \
            exit(EXIT_FAILURE); \
        } \
    }

#define NEW(pointer) NEW_ARRAY((pointer), 1)

typedef struct node *List;

struct node {
    int coeff;
    int power;
    struct node *next;
};
    
List NewList(int coeff, int power, List next)
{
    List node;

    NEW(node);
    node->coeff = coeff;
    node->power = power;
    node->next = next;
    return node;
}


void FreeList(List list)
{
    if (list != NULL) {
        FreeList(list->next);
        free(list);
    }
}


void PrintList(List list)
{
    while (list != NULL) {
        printf("%d %d\n", list->coeff, list->power);
        list = list->next;
    }
}


int main(void)
{
    List head[3] ;

    head[0] = NewList(1, 2, NewList(2, 3, NULL));
    head[1] = NewList(1, 2, NewList(2, 2, NULL));
    head[2] = NULL;

    for (int i = 0; i < LEN(head); i++) {
        PrintList(head[i]);
        FreeList(head[i]);
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM