C - 合并两个链接列表

时间:2016-09-14 21:20:00

标签: c linked-list

问题

我正在尝试合并两个链接列表。它们每个都有包含开始和结束节点的标题。这是功能。它返回带有新合并链表的标题;它将两个将被合并的链接列表作为参数。

我找不到我做错了什么。当返回合并的头并使用函数从其头中打印链接列表时,它将永远打印在循环中的两个节点。代码中使用的所有函数都运行良好;已在其他软件中测试过。

合并功能

header * merge_lists (header * h1, header * h2) {
  header * h3 = init_header();
  node * curr1 = h1->first;
  node * curr2 = h2->first;
  node * result;

  while (1) {
    if (curr1 == NULL && curr2 == NULL) break;
    if (curr1 == NULL){
      result = curr2;
      curr2 = curr2->next;
    }
    else if (curr2 == NULL){
      result = curr1;
      curr1 = curr1->next;
    }
    else if (curr1->name[0] > curr2->name[0]){
      result = curr1;
      curr1 = curr1->next;
    }
    else if (curr1->name[0] <= curr2->name[0]) {
      result = curr2;
      curr2 = curr2->next;
    }
    insert_beginning(h3, result);
  }
  return h3;
}

打印功能

void print_list (header * head) {
  node * current = head->first;
  while (current != NULL){
    printf("%s %d\n", current->name, current->age);
    current = current->next;
  }
}

在初始函数处插入

void insert_beginning (header * head, node * new_node) {
  if (head->quantity == 0){
    head->first = new_node;
    head->last = new_node;
    head->quantity++;
    return;
  }

  new_node->next = head->first;
  head->first = new_node;
  new_node->next->prev = new_node;
  head->quantity++;
}

节点结构

typedef struct node_struct {
  char name[30];
  int age;
  struct node_struct * prev;
  struct node_struct * next;
} node;

1 个答案:

答案 0 :(得分:1)

您的插入代码似乎至少有一个错误:

void insert_beginning (header * head, node * new_node) {
  if (head->quantity == 0){
    head->first = new_node;
    head->last = new_node;
    head->quantity++;
    return;
  }

  // NOTE/BUG: this links new_node to itself
#if 0
  head->first = new_node;
  new_node->next = head->first;
  // NOTE/FIX: this is the correct way
#else
  new_node->next = head->first;
  head->first = new_node;
#endif

  // NOTE/BUG?: what about end of list?
#if 0
  new_node->next->prev = new_node;
#else
  if (new_node->next != NULL)
    new_node->next->prev = new_node;
#endif

  head->quantity++;
}

<强>更新

  

我刚刚在帖子中复制错误的第一个错误,对不起。

好的,足够公平。

  

但第二个问题:此时new_node->next无法NULL,因为它始终会在开头插入,并且下一个节点总是会有。{/ p>

我认为这是问题的间接部分。

我已经让你的代码工作并制作了一个测试程序。我不得不创建一些缺少的函数和结构。

我编写了一个不同的合并函数:merge_lists2。其中一个最大的区别是,它不仅仅是(例如)curr1 = curr1->next,而是调用一个新函数来移除第一个元素[如果需要],删除所有旧的next/prev链接。

这是在新功能list_shift中完成的。特别是,请参阅该函数中的NOTE/BUG注释。

无论如何,这是更新和正常工作的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node_struct {
    char name[30];
    int age;
    struct node_struct *prev;
    struct node_struct *next;
} node;

typedef struct header_struct {
    node *first;
    node *last;
    int quantity;
} header;

void
print_list(header * head, const char *sym)
{
    node *current = head->first;

    printf("%s [fwd]:\n", sym);

    while (current != NULL) {
        printf("  %s %d\n", current->name, current->age);
        current = current->next;
    }
}

void
print_rlist(header * head, const char *sym)
{
    node *current = head->last;

    printf("%s [rev]:\n", sym);

    while (current != NULL) {
        printf("  %s %d\n", current->name, current->age);
        current = current->prev;
    }
}

void
insert_beginning(header * head, node * new_node)
{

#if 1
    new_node->prev = NULL;
#endif

    if (head->quantity == 0) {
        head->first = new_node;
        head->last = new_node;
        head->quantity++;
        return;
    }

    // NOTE/BUG: this links new_node to itself
#if 0
    head->first = new_node;
    new_node->next = head->first;
    // NOTE/FIX: this is the correct way
#else
    new_node->next = head->first;
    head->first = new_node;
#endif

    // NOTE/BUG?: what about end of list?
#if 0
    new_node->next->prev = new_node;
#else
    if (new_node->next != NULL)
        new_node->next->prev = new_node;
#endif

    head->quantity++;
}

void
insert_string(header * h, const char *str)
{
    node *ptr = calloc(1, sizeof(*ptr));

    strcpy(ptr->name, str);

    insert_beginning(h, ptr);
}

header *
init_header(void)
{
    header *h = calloc(1, sizeof(*h));

    return h;
}

header *
merge_lists1(header * h1, header * h2)
{
    header *h3 = init_header();
    node *curr1 = h1->first;
    node *curr2 = h2->first;
    node *result;

    while (1) {
        if (curr1 == NULL && curr2 == NULL)
            break;
        if (curr1 == NULL) {
            result = curr2;
            curr2 = curr2->next;
        }
        else if (curr2 == NULL) {
            result = curr1;
            curr1 = curr1->next;
        }
        else if (curr1->name[0] > curr2->name[0]) {
            result = curr1;
            curr1 = curr1->next;
        }
        else if (curr1->name[0] <= curr2->name[0]) {
            result = curr2;
            curr2 = curr2->next;
        }
        insert_beginning(h3, result);
    }
    return h3;
}

node *
list_shift(header * h, node * cur)
{

    do {
        // bug out if we do _not_ yet need to dequeue an element from this list
        if (cur != NULL)
            break;

        // bug out if we're at the end of the list
        cur = h->first;
        if (cur == NULL)
            break;

        // fix the head chain pointer
        h->first = cur->next;

        // fix the tail chain pointer
        if (cur == h->last)
            h->last = NULL;

        cur->prev = NULL;

        // NOTE/BUG: the smoking gun -- adding this fixed things
        cur->next = NULL;
    } while (0);

    return cur;
}

header *
merge_lists2(header * h1, header * h2)
{
    header *h3 = init_header();
    node *curr1 = NULL;
    node *curr2 = NULL;
    node *result;

    while (1) {
        curr1 = list_shift(h1, curr1);
        curr2 = list_shift(h2, curr2);

        if ((curr1 == NULL) && (curr2 == NULL))
            break;

        if (curr1 == NULL) {
            result = curr2;
            curr2 = NULL;
        }
        else if (curr2 == NULL) {
            result = curr1;
            curr1 = NULL;
        }
        else if (curr1->name[0] > curr2->name[0]) {
            result = curr1;
            curr1 = NULL;
        }
        else {
            result = curr2;
            curr2 = NULL;
        }

        insert_beginning(h3, result);
    }

    return h3;
}

// main -- main program
int
main(int argc, char **argv)
{
    char *cp;

    --argc;
    ++argv;

    for (; argc > 0; --argc, ++argv) {
        cp = *argv;
        if (*cp != '-')
            break;

        switch (cp[1]) {
        default:
            break;
        }
    }

    header *h1 = init_header();

    insert_string(h1, "jkl");
    insert_string(h1, "def");
    print_list(h1, "h1");
    print_rlist(h1, "h1");

#if 1
    header *h2 = init_header();

    insert_string(h2, "ttt");
    insert_string(h2, "ghi");
    insert_string(h2, "abc");
    print_list(h2, "h2");
    print_rlist(h2, "h2");
#endif

#if 0
    header *h3 = merge_lists1(h1, h2);

    print_list(h3, "h3");
    print_rlist(h3, "h3");
#endif

#if 1
    header *h3 = merge_lists2(h1, h2);

    print_list(h3, "h3");
    print_rlist(h3, "h3");
#endif

    return 0;
}

旁注:您只是比较字符串的第一个字符,因此您可能希望使用strcmp