Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 创建交错列表链接列表?_C_Arrays_Pointers_Heap_Dynamic Memory Allocation - Fatal编程技术网

C 创建交错列表链接列表?

C 创建交错列表链接列表?,c,arrays,pointers,heap,dynamic-memory-allocation,C,Arrays,Pointers,Heap,Dynamic Memory Allocation,我正在尝试创建一个名为IntNode*interleave\u列表的链表函数(IntNode*head\u 1,IntNode*head\u 2)获取两个链表,组合它们并返回指向头部的指针 示例:假设head_1指向一个包含三个整数的链表:1、3、5和head_2 指向包含5个整数的链表:10、11、12、13、14。新的链表将包含8个 整数,按如下顺序:1,10,3,11,5,12,13,14 我有以下结构来帮助创建链表: struct intnode { int value; s

我正在尝试创建一个名为
IntNode*interleave\u列表的链表函数(IntNode*head\u 1,IntNode*head\u 2)获取两个链表,组合它们并返回指向头部的指针

示例:假设head_1指向一个包含三个整数的链表:1、3、5和head_2 指向包含5个整数的链表:10、11、12、13、14。新的链表将包含8个 整数,按如下顺序:1,10,3,11,5,12,13,14

我有以下结构来帮助创建链表:

struct intnode {
   int value;
   struct intnode *next;
};
typedef struct intnode IntNode;

IntNode *intnode_construct(int value, IntNode *next)
{
   IntNode *p = malloc(sizeof(IntNode));
   assert (p != NULL);
   p->value = value;
   p->next = next;
   return p;
}
void print_linked_list(IntNode *p) //for printing linked list
{
    if (p == NULL) {
        printf("empty list");
        return; 
    }

    /* Print every node in the linked list except the last one,
       using the format: value1 -> value2 -> value3 -> ...
     */
    for (; p->next != NULL; p = p->next) {
        printf("%d -> ", p->value);
    }

    /* Print the last node. */
    printf("%d", p->value);
}
现在尝试创建我拥有的函数:

IntNode *interleave_lists(IntNode *head_1, IntNode *head_2)
{
    int count = 1;
    IntNode *p1=head_1, *p2=head_2,*head=NULL;
    for(; (p1->next != NULL) || (p2->next != NULL); p1 = p1->next, p2 = p2->next){
        if(p1 != NULL && count == 1){
            head = intnode_construct(p1->value,head);
            count = 0;
        } else if(p2 != NULL && count == 0){
            head = intnode_construct(p2->value,head);
        }
    }
    return head;
}
但每次我运行它时,控制台都会显示CRT:unhandled exception(main)——termining。 此函数的测试用例:

int main(void)
{
    IntNode *list1 = NULL,*list2 = NULL; // An empty linked list.
    list1 = intnode_construct(5, list1);
    list1 = intnode_construct(3, list1);
    list1 = intnode_construct(1, list1);

    list2 = intnode_construct(14, list2);
    list2 = intnode_construct(13, list2);
    list2 = intnode_construct(12, list2);
    list2 = intnode_construct(11, list2);
    list2 = intnode_construct(10, list2);


    IntNode *head = NULL;

    head = interleave_lists(list1, list2);
    print_linked_list(head);
}
1)您的
intnode\u构造应该如下所示

IntNode *intnode_construct(int value, IntNode *next)
{
   IntNode *p = malloc(sizeof(IntNode));
   if (p != NULL)
   {
       p->value = value;
       p->next = NULL;
   }
   return p;
}
对于您的代码,(;(p1->next!=NULL);(p2->next!=NULL)
在函数
交错列表中的循环
永远不会结束,因为
->next
永远不会是
==NULL

2)您的
交错列表必须如下所示:

IntNode *interleave_lists(IntNode *head_1, IntNode *head_2)
{
    int count = 1;
    IntNode *p1=head_1, *p2=head_2,*head=NULL;

    while ((p1 != NULL) || (p2 != NULL))
    {
        if(p1 != NULL && count == 1)
        {
            head = intnode_construct(p1->value,head);
            count = 0;
            p1 = p1->next;
        }
        else if(p2 != NULL && count == 0)
        {
            head = intnode_construct(p2->value,head);
            p2 = p2->next;
        }
    }

    return head;
}
代码总是将两个指针移动到每个循环中的下一个,而不检查指针是否有效或
NULL

3)顺便说一句,您必须更正代码以获得每个链接列表的标题。您的代码更新传递到
interleave\u列出了
最后一个元素…

多个问题

  • 你检查
    p->next!=空值
    而不是
    p!=NULL
    ,因此您将丢失列表的最后一个元素
  • 您的循环检查
    p1 | | p2
    ,只要其中一个指针尚未到达终点,就循环。因此,如果列表的长度不同,则短列表的指针将从末尾运行并崩溃
  • 您的
    intnode\u构造
    函数从右到左(从尾到头)构造列表,但您从头到尾迭代,因此在交错列表时,您正在反转列表
反转列表意味着您需要从尾部进行交错,使您的
交错列表
函数更易于递归实现:

IntNode *copy_list(IntNode *head)
{
    if (!head) return head;
    return intnode_construct(head->value, copy_list(head->next));
}

IntNode *interleave_lists(IntNode *head_1, IntNode *head_2)
{
    if (!head_1) return copy_list(head_2);
    return intnode_construct(head_1->value, interleave_lists(head_2, head_1->next));
}
或者,您可以执行“破坏性”交叉,重用输入列表的节点:

IntNode *interleave_lists(IntNode *head_1, IntNode *head_2)
{
    if (!head_1) return head_2;
    head_1->next = interleave_lists(head_2, head_1->next);
    return head_1;
}

碰撞发生在哪一行?如果你不知道,用调试器来找到它应该是
p->next=NULL作为旁白,如果需要,您可以使interleave函数在现有列表上运行,以避免复制所有节点。取决于以后是否要保留原始列表。