gpt4 book ai didi

c - 多头链表

转载 作者:行者123 更新时间:2023-11-30 17:49:30 25 4
gpt4 key购买 nike

我正在使用c中的链表,但我的程序实际上同时需要大量链表,因此当我想添加新节点时,我必须确定应该添加到哪个列表,我有一个函数它返回要添加的所需列表的头部,但我有点困惑应该如何编写“add_node”函数,因为头节点每次都不相同,而且我不想使用“switch case”,因为会很长...提前致谢

void add_it(int *array)
{
head=which_head(array);
curr = malloc(sizeof (node));
memcpy(curr->nconn, array, sizeof (curr->nconn));
curr->next = ?????????;
???????=curr
}

最佳答案

struct Node
{
Node *next;
int myData[80];
};

Node **which_head(some params);

void Add(some data)
{
Node **head_location = whitch_head(...);
Node *new_element = (Node*)malloc(...);

new_element->next = *head_location;
*head_location = new_element;
}

诀窍是您的 which_head() 函数应该返回头部的位置,而不是指向列表第一个元素(如果有)的指针。

关于c - 多头链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17800093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com