gpt4 book ai didi

创建原始列表的子列表?

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

我正在尝试创建一个函数,该函数需要从列表和原始列表中获取两个参数位置,然后将索引数字复制到列表中。我还包括了列表和头部的结构。我收到 EXC_BAD_ACCESS 我评论该行的错误。

代码:

struct node_struct {
Item item;
link next;
};

struct list_struct {
link first;
int length;
};

list sublist(list A, list pos_list) {

int index = 0;
link tempForindex = malloc(sizeof *tempForindex);
link temp2 = malloc(sizeof *temp2);
list finale = malloc(sizeof *finale);
link temp3 = malloc(sizeof *temp3);
tempForindex = pos_list->first;
temp2 = A->first;
temp3 = finale->first;
int counter = 0;
while(tempForindex->next != NULL)
{
index = tempForindex->item;//EXC_BAD_ACCESS code1
counter = 0;
while(temp2->next != NULL)
{
if (counter == index)
{
temp3->item = temp2->item;
temp2 = A->first;
temp3 = temp3->next;
break;
}
temp2 = temp2->next;
counter++;
}
tempForindex = tempForindex->next;
}

return finale;

}

最佳答案

这一切都存在无可救药的缺陷。

您需要获得与链接列表配合的模式。结构很好,但代码在指示的位置崩溃的事实表明,pos_list 尚未正确设置。

使用此模式迭代列表

list mylist;
link ptr;
for(ptr = mylist->first; ptr != NULL; ptr = ptr->next)
{
/* loop body */
}

现在编写一个测试函数来打印两个参数并确定它们是否有效。

关于创建原始列表的子列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39812752/

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