简体   繁体   English

C中的链表问题

[英]Problems with a Linked List in C

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. 我正在用C创建一个(结构的)链表,但是我希望能够调用一个函数,并让它自己将4-5个结构添加到列表中。 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语言中,在函数中创建的所有变量都保留在堆栈/堆中,我不知道应该如何完成此操作。

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! 这比我想像的更快,更有用!

You have to allocate your "one", "two", "three" with malloc() instead of creating them on the stack. 您必须使用malloc()分配“一个”,“两个”,“三个”,而不是在堆栈上创建它们。 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. 处理完它们之后,您将不得不再次遍历该列表并在内存上调用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;
}

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

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 干杯

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

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