2->3->4->5->6- 而pos_list有整数数据如:4->0->5-那么这个函数应该返回一个新列表,它包含列表 A 中出现在 pos_list 中给定位置的值-6ren">
gpt4 book ai didi

c - 此函数返回一个列表,其中包含出现在列表 "A"中 "pos_list"给定位置的值

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:54:15 26 4
gpt4 key购买 nike

-如果A列表有整数数据如:1->2->3->4->5->6- 而pos_list有整数数据如:4->0->5-那么这个函数应该返回一个新列表,它包含列表 A 中出现在 pos_list 中给定位置的值这样 New List= 5->1->6

我正在实现深度复制以制作新列表。我正在尝试使用根据 pos_list 的数据进行迭代的循环。在这个循环中,A 的节点将移动到 pos_list 数据的位置。这次我将复制新列表中的节点 A 以制作另一个列表。对于第一种情况,pos_list 有数据 4,因此循环将运行 4 次,直到列表 A 的节点指向其第四个位置。在这个循环中,我将在一个新循环中复制列表 A 的数据。我需要指导来解决这个问题。

struct node * sublist(struct node * A, struct node * pos_list) {
struct node* newList=NULL;
struct node * curr;
int i=0;

for (i = 0, curr = pos_list->next; (curr != NULL); curr = curr->next) { //pos_list->data has a dummy node so loop until the end of pos_list->data.
struct node* newList = (struct node *) malloc(sizeof (struct node));

for(int i=0;i<=pos_list->data;i++){ //counter for pos_list as it will be (3 then 0,6 and 4)
if(i==pos_list->data){ //At the time when i == pos_list->data(3 or 0 or 6..)
newList->data = A->data; //Putting value of list A data in new list.
newList = newList->next; //Linking
printf("%d\t", newList->data); //Just for log
}
A=A->next; //Going to next position on A
}
pos_list=pos_list->next; //Going to next position on B
}
return newList ;
}

如果列表是:1->2->3->4->5->6而pos_list是:4->0->5

我希望输出是新列表 5->1->6

最佳答案

您的代码有几个问题:

  • 您应该从 pos_list 开始遍历,而不是 pos_list->next。头指针指向的节点是列表的一部分。此外,如果 pos_list == NULLpos_list->next 将导致未定义的行为。
  • int i 的外部定义没有用。删除它。
  • 不要通过位置遍历 A。如果该位置无效,您将越过列表末尾,获取空指针并调用未定义的行为。列表应该由从先前节点的 next 指针访问的列表节点迭代。 (当然,提供有效位置是调用者的责任,但您的程序应该优雅地处理无效输入。)
  • 仅在找到有效位置后才创建新节点。否则,您将创建一个永远不会插入的节点,从而导致内存泄漏。
  • 此处:newList = newList->nextnewList->next 未初始化。请记住,malloc 为您提供了一大块未初始化的数据。
  • 您尝试使 newList 指向新创建列表的末尾,以便快速添加新节点。这是个好主意,但如果您返回该指针,您将得到一个仅包含一个元素的列表。 (您也将没有登录者能够访问该列表中任何先前创建的节点。)

这是一个应该有效的实现:

struct node *sublist(struct node *A, struct node *pos_list)
{
struct node *newHead = NULL;
struct node *newTail = NULL;
struct node *pos = pos_list;

while (pos) {
struct node *a = A;
int i = 0;

while (a) {
if (i == pos->data) {
struct node *node = malloc(sizeof(*node));

if (newHead == NULL) newHead = node;
if (newTail) newTail->next = node;
node->data = a->data;
node->next = NULL;
newTail = node;

break;
}

a = a->next;
i++;
}

pos = pos->next;
}

return newHead;
}

关于c - 此函数返回一个列表,其中包含出现在列表 "A"中 "pos_list"给定位置的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58232063/

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