C中的一系列链表

时间:2014-01-12 12:36:29

标签: c arrays

我有以下代码:

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)
{
    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;

}

添加功能:

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");
}

最后,显示功能:

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;
    }
}

但是,当我在驱动器中传递这些功能时,只显示1个数字。显示功能是错误的,或添加功能是错误的,但我似乎无法得到问题?有任何想法吗?

感谢。

3 个答案:

答案 0 :(得分:1)

指针分配不正确。

prev被分配给内存的malloc。然后prev被分配保留。所以新分配的内存丢失了。

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

答案 1 :(得分:1)

您的代码有多个问题。首先,请注意create函数:

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

我想说,只有在分配到setnum后才想增加larray[setnum],否则,您永远不会使用位置larray[0]。此外,当setnum == MAXSETS时,您将访问越界位置(这是一个一个错误的错误)。因此,测试应检查setnum >= MAXSETScreate应该是这样的:

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;
}

add功能错误。当然,这段代码不是你想要的:

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

此时,hold将指向NULL,在分配prev = hold;后,您泄漏内存,因为您丢失了对之前分配的内存的引用,并且比那,你在以下语句中取消引用空指针。我建议从头开始重写这个功能。

显示功能无法按预期方式工作。由于这个赋值,for循环在每次迭代中都会泄漏内存:

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

你真的想在显示之后销毁它吗?此外,循环条件是错误的。添加元素时,总是在该数组位置的第一个元素中递增numberOfValues,并且在第一个元素之后的其他元素中不使用numberOfValues。但是,要停止的循环条件是larray[s]->numberOfValues == 0,它与前一次迭代的赋值相结合,使得程序完全任意行为,因为larray[s]->numberOfValues从未初始化并且会有一些垃圾值。< / p>

答案 2 :(得分:1)

这段代码有太多问题,我不确定你对这段代码的用处是什么,但是我改变了部分代码,并用简单的案例测试它,它有效,希望它在这里有用

#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);

}