创建一个 interleave_lists 链表?

creating an interleave_lists linked list?

我正在尝试创建一个名为 IntNode *interleave_lists(IntNode *head_1, IntNode *head_2); 的链表函数,它采用两个链表,将它们组合起来,return 一个指向头部的指针。

示例:假设 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:未处理的异常(主要)-- 正在终止。 我对此功能的测试用例:

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_construct 应该如下所示

IntNode *intnode_construct(int value, IntNode *next)
{
   IntNode *p = malloc(sizeof(IntNode));
   if (p != NULL)
   {
       p->value = value;
       p->next = NULL;
   }
   return p;
}

使用您的代码,函数 interleave_lists 中的循环 for(; (p1->next != NULL) || (p2->next != NULL); 永远不会结束,因为 ->next 永远不会是 == NULL

2) 您的 interleave_lists 必须如下所示:

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_lists 最后一个元素...

多个问题

  • 您检查 p->next != NULL 而不是 p != NULL,因此您丢失了列表的最后一个元素
  • 您的循环检查 p1 || p2,只要任一指针尚未到达终点就循环。因此,如果列表的长度不同,则短列表的指针将 运行 偏离末尾并崩溃。
  • 您的 intnode_construct 函数从右到左(从尾到头)构造列表,但是您是从头到尾迭代的,因此您在交错排列时颠倒了列表。

反向列表意味着你需要从尾部交错,使你的 interleave_lists 函数更容易递归实现:

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

或者,您可以进行 'destructive' 交错,重复使用输入列表的节点:

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