如何将 2 个链表合并或合并在一起以创建新列表

How do I combine or merge 2 linked lists together to create a new list?

本文关键字:合并 新列表 创建 列表 链表 在一起      更新时间:2023-10-16

我正在尝试获取两个链表"list_1"和"list_2",然后将它们组合并放入"list_3"中。我创建了两个列表,但似乎无法弄清楚如何组合它们。我添加的代码是我创建列表的方式。指针和链表很新,所以任何帮助将不胜感激,谢谢!

struct node
{
    int data;
    node *next;
};
class List
{
    public:
        node *head, *tail;
    List()
    {
        head = NULL;
        tail = NULL;
    }
    void add_node(int n)
    {
        for(int i = 1; i <= 1; i++)
        {
            node *temp = new node;
            temp -> data = n;
            temp -> next = NULL;
            if(head == NULL)
            {
                head = temp;
                tail = temp;
            }
            else{
                tail -> next = temp;
                tail = tail -> next;
            }
        }
    }

你必须"重新连接"它们。 列表 B 的head应重新连接到列表 A 的tail,以便可以删除列表 B 的List对象,但不会删除其成员。引入新的方法merge(List* list)参数,并将this->tail重新连接到list->head,并将this->tail更新为list->tail

如何将 2 个链表合并或合并在一起以创建新列表

只需使用add_node迭代两个列表即可组合

这里有一个组合修改当前列表的建议,如果您喜欢这些方式,很容易添加新的构造函数在参数或静态方法组合等中获得两个列表

我添加了一些"经典"方法来帮助并能够在没有内存泄漏的情况下使用 valgrind,并且还将列表属性设为私有,因为将它们公开不是一个好方法

#include <iostream>
struct node
{
  int data;
  node * next;
  node(int v) : data(v), next(nullptr) {}
};
class List
{
  private:
    node *head, *tail;
  public:
    List() : head(nullptr), tail(nullptr) {}
    ~List() { clear(); }
    List & operator=(const List & l) {            
      clear();
      const node * n = l.head;
      while (n != nullptr) {
        add_node(n->data);
        n = n->next;
      }
      return *this;
    }
    // + copy constructor, move etc
    void clear() {
       while (head != nullptr) {
         tail = head->next;
         delete head;
         head = tail;
       }
       head = tail = nullptr;
    }
    void add_node(int n)
    {
      node * temp = new node(n);
      if(head == NULL)
      {
        head = temp;
        tail = temp;
      }
      else
      {
        tail -> next = temp;
        tail = tail -> next;
      }
    }
    void combine(const List & l1, const List & l2) {
      *this = l1;
      node * n = l2.head;
      while (n != nullptr) {
        add_node(n->data);
        n = n->next;
      }
    }
    void pr() const {
      const node * n = head;
      while (n != nullptr) {
        std::cout << n->data << ' ';
        n = n->next;
      }
      std::cout << std::endl;
    }
};
int main()
{
  List l1, l2, l3;
  l1.add_node(1);
  l1.add_node(2);
  l1.add_node(3);
  l2.add_node(4);
  l2.add_node(5);
  l3.add_node(33);
  l3.pr();
  l3.combine(l1, l2);
  l3.pr();
}

编译和执行:

/tmp % g++ -pedantic -Wextra -Wall c.cc
/tmp % ./a.out
33 
1 2 3 4 5 

瓦尔格林德的处决

/tmp % valgrind ./a.out
==8413== Memcheck, a memory error detector
==8413== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==8413== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==8413== Command: ./a.out
==8413== 
33 
1 2 3 4 5 
==8413== 
==8413== HEAP SUMMARY:
==8413==     in use at exit: 0 bytes in 0 blocks
==8413==   total heap usage: 11 allocs, 11 frees, 176 bytes allocated
==8413== 
==8413== All heap blocks were freed -- no leaks are possible
==8413== 
==8413== For counts of detected and suppressed errors, rerun with: -v
==8413== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)