简体   繁体   English

使用结构将链表传递给 function 我做错了什么?

[英]Pass linked list to function with struct what I doing wrong?

I want to pass liked list to function and create new list.我想将喜欢的列表传递给 function 并创建新列表。 I need some help to understand what I'm doing wrong.我需要一些帮助来了解我做错了什么。 I create new pointer and copy the pointer to "curr" and than run on the list and built in. I try to find in which step I got it wrong.我创建了新指针并将指针复制到“curr”,然后在列表上运行并内置。我试图找出我在哪一步出错了。 I already try to debug the program.我已经尝试调试程序。

the struct is simple struct If i, j and the value of the matrix A are an arithmetic series they are added to the list.该结构是简单的结构如果 i、j 和矩阵 A 的值是一个算术级数,它们将被添加到列表中。

int createList(int A[][COLS], list** lst, int rows, int cols)
{
// your code:
*lst = (list*)calloc(1, sizeof(list));
list* curr = *lst;
int i, j;
int counter = 0;
int d; 
int check = 0;
int j_i;
int A_j;
four new_four;
for (i = 0; i < rows; i++) {
    for (j = 0; j < cols; j++) {
        j_i = j - i;
        A_j = A[i][j] - j;
        if (j_i == A_j) {
            d = j-i;
            new_four = createFour(i, j, d, A[i][j]);

            if (counter == 0) {
                curr = createElement(new_four);

            }
            counter++;
            curr->next = createElement(new_four);
            curr = curr->next;

        }
    }
}
curr->next = NULL;

return counter;
}

// create new object with all the parameters // 使用所有参数创建新的 object

    four createFour(int i, int j, int d, int value)
{
    four new_f;;
    new_f.d = d;
    new_f.i = i;
    new_f.j = j;
    new_f.value = value;
    return new_f;
}

// create new Element with the data to the list // 使用列表中的数据创建新元素

list* createElement(four data)
{
// your code:
list* new_Element = (list*)calloc(1, sizeof(list));
new_Element->data = data;
new_Element->next = (list*)calloc(1, sizeof(list));
return new_Element;
}

The problem is that you're not actually passing back the list via the lst parameter.问题是您实际上并没有通过lst参数传回列表。 You're allocating a value and storing it correctly, but then not using that value to add in all the data you want, because you never actually use it and instead create an entirely different list that is then causes a memory leak.您正在分配一个值并正确存储它,但随后没有使用该值添加您想要的所有数据,因为您从未实际使用它而是创建了一个完全不同的列表,然后导致 memory 泄漏。

The problem boils down to this part问题归结为这部分

        if (counter == 0) {
            curr = createElement(new_four);

        }
        counter++;
        curr->next = createElement(new_four);
        curr = curr->next;

When counter is 0, you create the start of a new list - this is the value you want to be storing into *lst to pass back to the calling code.counter为 0 时,您创建了一个新列表的开始 - 这是您要存储到*lst中以传递回调用代码的值。 You're also creating 2 nodes when I imagine you only want 1.当我想你只想要 1 个时,你也在创建 2 个节点。

If you make this slight tweak, you'll get what you want.如果你做这个轻微的调整,你会得到你想要的。

        if (counter == 0) {
            *lst = curr = createElement(new_four);

        } else {
            curr->next = createElement(new_four);
            curr = curr->next;
        }
        counter++;

you'll also need to remove this line at the start of the function as it isn't needed您还需要在 function 的开头删除这一行,因为它不需要

*lst = (list*)calloc(1, sizeof(list));

and you don't need to assign *lst to curr here either since it is initiased later on而且您也不需要在此处将*lst分配给curr因为它是稍后启动的

list* curr = *lst;

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

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