简体   繁体   English

两个有序链表的交集 C++

[英]Intersection of two Sorted Linked Lists C++

I have some troubles with my intersection method.我的交集方法有一些问题。 It works properly, but after end of programm it throws mistakes.它工作正常,但在程序结束后它会抛出错误。 Here`s my code:这是我的代码:

void SortedLinkedList::intersection(SortedLinkedList finalList, SortedLinkedList list1, SortedLinkedList list2) {
        Node *pointer1 = list1.first;
        Node *pointer2 = list2.first;
        int counter = 0;
        while (pointer1 != NULL && pointer2 != NULL) {
            if (pointer1->data < pointer2->data) {
                pointer1 = pointer1->next;
            }
            else if (pointer2->data < pointer1->data){
                pointer2 = pointer2->next;
            }
            else if (pointer1->data == pointer2->data){
                finalList.addItem(pointer1->data);
                pointer1 = pointer1->next;
                pointer2 = pointer2->next;
            }
        }
        finalList.printList();
    }

I need to get intersection of two lists in a third one.我需要在第三个列表中获得两个列表的交集。

Main glaring mistake: SortedLinkedList finalist You have to change values in that class, it's your output data structure.主要明显错误: SortedLinkedList finalist您必须更改该类中的值,这是您的输出数据结构。 You have to pass it by reference, otherwise the actual list passed to the function never will be changed.你必须通过引用传递它,否则传递给函数的实际列表永远不会改变。

In some cases you would like other input data passed refs too, just if you do it with "const" declarator, you have to use only const methods from them.在某些情况下,您也希望其他输入数据也传递 refs,只要您使用“const”声明符来实现,您就必须只使用它们中的const方法。 As you actually do not use any methods andyour class is relatively simple, that doesn't matter much由于您实际上不使用任何方法并且您的类相对简单,因此无关紧要

void SortedLinkedList::intersection(SortedLinkedList& finalList, 
                  const SortedLinkedList& list1,
                  const SortedLinkedList& list2) 

Anything else without seeing whole design of your class would be guesswork.没有看到您的课程的整体设计的任何其他事情都是猜测。 problem might be in your AddItem method or in lack of class fields initialization.问题可能出在您的 AddItem 方法中或缺少类字段初始化。

The standard library offer a method for that std::set_intersection标准库为该std::set_intersection提供了一种方法

It is precisely used to merge to sorted containers.它恰好用于合并到已排序的容器。

This is the snippet taken from the link:这是从链接中截取的片段:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main()
{
  std::vector<int> v1{1,2,3,4,5,6,7,8};
  std::vector<int> v2{        5,  7,  9,10};
  std::sort(v1.begin(), v1.end());
  std::sort(v2.begin(), v2.end());

  std::vector<int> v_intersection;

  std::set_intersection(v1.begin(), v1.end(),
                        v2.begin(), v2.end(),
                        std::back_inserter(v_intersection));
  for(int n : v_intersection)
    std::cout << n << ' ';
}

In the link you will find a couple of possible implementations as well.在链接中,您还将找到几个可能的实现。 But, my suggestion is: "Don't reinvent the wheel, just realign it".但是,我的建议是:“不要重新发明轮子,只需重新调整它”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM