C中链接列表的问题

时间:2021-07-30 07:22:09

I'm making a linked list (of structs) in C, but I want to be able to call a function and have it add 4-5 stucts to the list by itself. The problem is since in C all the variables created in functions are left on the stack/heap I have no clue how I am supposed to accomplish this.

我在C中创建一个链接列表(结构),但我想能够调用一个函数并让它自己添加4-5个stuct到列表中。问题是因为在C中,所有在函数中创建的变量都留在了堆栈/堆上,我不知道我应该如何实现这一点。

Here is a code example:

这是一个代码示例:

struct listItem
{
   int value;
   listItem *left;
   listItem *right;
}

void addItems(listItem *l)
{
   listItem one, two, three;
   l->left = &one;
   one.left = &two;
   two.left = &three;
}

int main (char *a [])
{
   listItem l;
   addItems(l);
}

Obviously this will not work. How might I accomplish this? Is it even possible. Thanks

显然这不起作用。我怎么能做到这一点?它甚至可能吗?谢谢

EDIT: Wow thank you everyone for the help. That was faster and more helpful than I could have imagined!

编辑:哇谢谢大家的帮助。这比我想象的更快,更有帮助!

3 个解决方案

#1


You have to allocate your "one", "two", "three" with malloc() instead of creating them on the stack. After you're done with them, you'll have to walk through the list again and call free () on the memory so that your program doesn't leak.

您必须使用malloc()分配“one”,“two”,“three”,而不是在堆栈上创建它们。在完成它们之后,你将不得不再次浏览列表并在内存上调用free(),这样你的程序就不会泄漏。

Try this addItem instead...

试试这个addItem而不是......

void addItem(listItem *l, int value)
{
   listItem* item = malloc (sizeof (listItem));
   item->value = value;
   item->next = 0;
   item->prev = l; // Probably not what you want, but you were only singly linking in the example

   l->next = item;
}

#2


In this code:

在这段代码中:

void addItems(listItem *l)
{
   listItem one, two, three;
   l->left = &one;
   one.left = &two;
   two.left = &three;
}

All of the variables are left on the stack, not the heap. Probably you want to allocate them on the heap, so that you can refer a pointer to them that will not be invalid once the stack frame is left:

所有变量都留在堆栈上,而不是堆。您可能希望在堆上分配它们,以便您可以引用指向它们的指针,这些指针在离开堆栈帧后不会无效:

void addItems(listItem *l)
{
   listItem *one=calloc(1, sizeof(*one)), 
     two=calloc(1, sizeof(*two)),
     three=calloc(1, sizeof(*three));
   l->left = one;
   one.left = two;
   two.left = three;
}

#3


addItems() has to allocate memory:

addItems()必须分配内存:

void addItems(listItem *l)
{
   listItem* one = (listItem*)malloc(sizeof(listItem));
   listItem* two = (listItem*)malloc(sizeof(listItem));
   listItem* three = (listItem*)malloc(sizeof(listItem));
   l->left = 0;
   l->right = one;
   one->left = l;
   one->right = two;
   two->left = one;
   two->right = three;
   three->left = two;
   three->right = 0;
}

int main ()
{
   listItem l;
   addItems(&l);
}

I'm assuming you are out to create a double linked list, so I liberty in setting the left/right pointers accordingly. If I'm wrong in my assumption, please adjust so it suits your needs.

我假设你要创建一个双链表,所以我可以相应地设置左/右指针。如果我的假设错了,请调整以适合您的需要。

Cheers

#1


You have to allocate your "one", "two", "three" with malloc() instead of creating them on the stack. After you're done with them, you'll have to walk through the list again and call free () on the memory so that your program doesn't leak.

您必须使用malloc()分配“one”,“two”,“three”,而不是在堆栈上创建它们。在完成它们之后,你将不得不再次浏览列表并在内存上调用free(),这样你的程序就不会泄漏。

Try this addItem instead...

试试这个addItem而不是......

void addItem(listItem *l, int value)
{
   listItem* item = malloc (sizeof (listItem));
   item->value = value;
   item->next = 0;
   item->prev = l; // Probably not what you want, but you were only singly linking in the example

   l->next = item;
}

#2


In this code:

在这段代码中:

void addItems(listItem *l)
{
   listItem one, two, three;
   l->left = &one;
   one.left = &two;
   two.left = &three;
}

All of the variables are left on the stack, not the heap. Probably you want to allocate them on the heap, so that you can refer a pointer to them that will not be invalid once the stack frame is left:

所有变量都留在堆栈上,而不是堆。您可能希望在堆上分配它们,以便您可以引用指向它们的指针,这些指针在离开堆栈帧后不会无效:

void addItems(listItem *l)
{
   listItem *one=calloc(1, sizeof(*one)), 
     two=calloc(1, sizeof(*two)),
     three=calloc(1, sizeof(*three));
   l->left = one;
   one.left = two;
   two.left = three;
}

#3


addItems() has to allocate memory:

addItems()必须分配内存:

void addItems(listItem *l)
{
   listItem* one = (listItem*)malloc(sizeof(listItem));
   listItem* two = (listItem*)malloc(sizeof(listItem));
   listItem* three = (listItem*)malloc(sizeof(listItem));
   l->left = 0;
   l->right = one;
   one->left = l;
   one->right = two;
   two->left = one;
   two->right = three;
   three->left = two;
   three->right = 0;
}

int main ()
{
   listItem l;
   addItems(&l);
}

I'm assuming you are out to create a double linked list, so I liberty in setting the left/right pointers accordingly. If I'm wrong in my assumption, please adjust so it suits your needs.

我假设你要创建一个双链表,所以我可以相应地设置左/右指针。如果我的假设错了,请调整以适合您的需要。

Cheers