简体   繁体   English

C中的链表列表

[英]An array of linked lists in C

I have the following code: 我有以下代码:

typedef struct element{
    int value;
    int numberOfValues;
    struct element * next;
} Element;

static int setnum = 0; // this is a key that identifies the set
int s, i; // s is input by the user to identify a set | i is a loop counter
Element * prev, * curr;

Element * larray[MAXSETS];

And then, a function that allows the user to create a set of elements: 然后是一个允许用户创建一组元素的函数:

int create(void)
{
    if (setnum > MAXSETS)
        printf("Error. Maximum value of sets reached.\n");
    else
        {
            setnum++;
            larray[setnum] = (Element *) malloc(sizeof(Element));
            larray[setnum]->next = NULL;
            larray[setnum]->numberOfValues = 0;
            // larray[setnum] practically becomes the head
        }

    return setnum;

} }

And an add function: 还有一个添加功能:

void add(int s, int x)
{
static Element * hold = NULL;

printf("Hello!\n");

if (larray[s]->numberOfValues >= MAXELEMENTS) // the set must be finite
{
    printf("Error. Set is full.\n");
}
else
    if (larray[s]->numberOfValues == 0)
    {
        larray[s]->value = x;
        larray[s]->numberOfValues++;
        hold = larray[s];
    }
    else
    {
        prev = (Element *) malloc(sizeof(Element));
        prev = hold;
        prev->next = hold;
        hold->next = NULL;
        hold->value = x;
        printf("Done!\n");
        larray[s]->numberOfValues++;
    }
printf("Bye!\n");
}

And finally, a display function: 最后是显示功能:

void display(int s)
{
    curr = larray[s];
    for (i = 0; larray[s]->numberOfValues; i++)
    {
        printf("%d\n", larray[s]->value);
        larray[s] = larray[s]->next;
    }
}

However, when I pass these functions in a drive, only 1 number is displayed. 但是,当我在驱动器中传递这些功能时,仅显示1个数字。 Either the display function is wrong, or the add function is wrong, but I can't seem to get the problem? 显示功能错误或添加功能错误,但我似乎无法解决问题? Any ideas? 有任何想法吗?

Thanks. 谢谢。

The pointer assignments are incorrect. 指针分配不正确。

prev is assigned to a malloc of memory. prev被分配给内存的malloc。 Then prev is assigned to hold. 然后将prev指定为保留。 So the newly allocated memory is lost. 因此,新分配的内存将丢失。

prev = (Element *) malloc(sizeof(Element));
prev = hold;

Your code has more than one problem. 您的代码有多个问题。 First of all, note the create function: 首先,请注意create函数:

else
    {
        setnum++;
        larray[setnum] = (Element *) malloc(sizeof(Element));
        larray[setnum]->next = NULL;
        larray[setnum]->numberOfValues = 0;
        // larray[setnum] practically becomes the head
    }

I'd say you want to increment setnum only after assigning to larray[setnum] , otherwise, you will never use position larray[0] . 我想说的是,仅在分配给larray[setnum]后才想增加setnum ,否则,您将永远不会使用位置larray[0] Also, when setnum == MAXSETS , you will be accessing an out-of-bounds position (this is an off-by-one error). 同样,当setnum == MAXSETS ,您将访问一个越界位置(这是一个不正确的错误)。 Thus, the test should check for setnum >= MAXSETS . 因此,测试应检查setnum >= MAXSETS create should look like this instead: create应该看起来像这样:

int create(void)
{
    if (setnum >= MAXSETS)
        printf("Error. Maximum value of sets reached.\n");
    else
        {
            larray[setnum] = (Element *) malloc(sizeof(Element));
            larray[setnum]->next = NULL;
            larray[setnum]->numberOfValues = 0;
            setnum++;
        }

    return setnum;
}

The add function is wrong. add功能错误。 This piece of code is, for sure, not what you want: 这段代码肯定不是您想要的:

else
{
    prev = (Element *) malloc(sizeof(Element));
    prev = hold;
    prev->next = hold;
    hold->next = NULL;
    hold->value = x;
    printf("Done!\n");
    larray[s]->numberOfValues++;
}

At this point, hold will point to NULL , after the assignment prev = hold; 此时,在赋值prev = hold;之后, hold将指向NULL prev = hold; , you leak memory, because you lose reference to the memory you allocated immediately before, and, worse than that, you dereference a null pointer in the following statements. ,则会泄漏内存,因为您将丢失对紧接在其之前分配的内存的引用,更糟糕的是,在以下语句中取消引用了空指针。 I suggest rewriting this function from scratch. 我建议从头开始重写此功能。

The display function won't work the way you expect. 显示功能将无法按您期望的方式工作。 The for loop leaks memory in every iteration because of this assignment: 由于此分配, for循环在每次迭代中都会泄漏内存:

larray[s] = larray[s]->next;

Do you really want to destroy the set after displaying it? 您真的要在展示完之后销毁它吗? Also, the loop condition is wrong. 同样,循环条件是错误的。 When you add an element, you always increment numberOfValues in the first element on that array position, and you don't use numberOfValues in the other elements past the first. 添加元素时,始终在该数组位置的第一个元素中增加numberOfValues ,并且在第一个元素之后的其他元素中不使用numberOfValues However, the loop condition to stop is that larray[s]->numberOfValues == 0 , which, combined with the assignment on the previous iteration, makes your program behave completely arbitrarily, since larray[s]->numberOfValues was never initialized and will have some garbage value. 但是,要停止的循环条件是larray[s]->numberOfValues == 0 ,再加上上一次迭代的赋值,使您的程序可以完全任意地运行,因为larray[s]->numberOfValues从未被初始化,会有一些垃圾价值

this code has too much problem , I am not sure what you are suppose to do with this code,but I change part of your code, and test it with simple case, it works , hope it will be useful here 该代码有太多问题,我不确定您应该如何使用此代码,但是我更改了部分代码,并以简单的情况对其进行了测试,它可以工作,希望在这里有用

#define MAXSETS 5
#define MAXELEMENTS 3
#include <stdio.h>
#include <stdlib.h>
typedef struct element{
    int value;
    int numberOfValues;
    struct element * next;
} Element;


static int setnum = 0; // this is a key that identifies the set
int s, i; // s is input by the user to identify a set | i is a loop counter
Element * prev, * curr;

Element * larray[MAXSETS];

int create(void)
{
    while (setnum < MAXSETS)
        {

            larray[setnum] = (Element *) malloc(sizeof(Element));
            larray[setnum]->next = NULL;
            larray[setnum]->numberOfValues = 0;
            setnum++;
            // larray[setnum] practically becomes the head
        }
     printf("%d\n", setnum);
    return setnum;
}


void add(int s, int x)
{
static Element * hold = NULL;


if (larray[s]->numberOfValues >= MAXELEMENTS) // the set must be finite
{
    printf("Error. Set is full.\n");
}
else
    if (larray[s]->numberOfValues == 0)
    {
        larray[s]->value = x;
        larray[s]->numberOfValues++;
        printf("numberOfValues: %d\n", larray[s]->numberOfValues);
        hold = larray[s];
    }
    else
    {
        hold = larray[s];
        for(int i = 1; i < larray[s]->numberOfValues;i++)
        {
            hold = hold->next;
        }
        prev = (Element *) malloc(sizeof(Element));
        prev->next = hold;
        prev->value = x;
        larray[s]->numberOfValues++;
        printf("numberOfValues: %d\n", larray[s]->numberOfValues);
        hold->next = prev;
    }

}


void display(int s)
{
    curr = larray[s];
    for (i = 0; i < larray[s]->numberOfValues; i++)
    {
        printf("%d\n", curr->value);
        curr = curr->next;
    }
}

int main()
{
    create();
    add(0,0);
    add(0,1);
    add(0,2);
    display(0);

}

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

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