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

时间:2019-03-27 08:03:40

标签: c++ linked-list singly-linked-list

我正在尝试获取两个链接列表“ 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;
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

您必须“重新布线”它们。列表B的10 = TEXT 10 { wrap = | field = tx_mask_cnt_news_item_date // field in tt_content, is '2018-08-31' strtotime = 1 strftime = %e %B %Y } 应该重新连接到列表A的head,以便您可以删除列表B的tail对象,但不会删除其成员。引入新方法List参数,然后将merge(List* list)重新连接到this->tail,并将list->head更新为this->tail

答案 1 :(得分:0)

  

如何将2个链接列表合并或合并以创建新列表

只需在两个列表上使用add_node进行迭代即可

这是一个建议,其中 combine 修改当前列表,很容易添加一个新的构造函数以在参数中包含两个列表或将 static 方法合并等,如果您喜欢这些方式

我添加了一些“经典”方法来帮助并能够使用 valgrind 而不会出现内存泄漏,并且还添加了 List 属性 private 因为让他们 public 不是一个好方法

#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 

valgrind

下执行
/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)