尝试在链接列表中获得2组的差异
例:
输入:
设置A:1 - 2 - 3 - 5
设置B:2 - 4 - 5
输出:
联盟:5 - 4 - 2 - 5 - 3 - 2 - 1
交叉口:5 - 2
差异:3 - 4 - 1(问题在这里)矿产量差异:5 - 3 - 2 - 1
此代码位于“LinkedList.h”
下#include "Node.h"
#include <iostream>
using namespace std;
class LinkedList
{
public:
Node *head;
LinkedList()
{
head = NULL;
}
bool find(Node *value)
{
int cnt(0);
Node *iterator = head;
while(iterator != NULL)
{
if(value->data == iterator->data) return 1;
iterator = iterator->next;
}
return cnt;
}
void insertBeginning(int value)
{
Node *newNode = new Node;
newNode->data = value;
if(find(newNode) != 1)
{
Node *temp = head;
head = newNode;
newNode->next = temp;
}
}
void deleteBeginning()
{
if(head != NULL)
{
Node *temp = head->next;
head = temp;
}
}
void display()
{
Node *iterator = head;
while (iterator != NULL)
{
cout<<iterator->data<<" ->";
iterator = iterator->next;
}
cout<<" end";
}
bool isEmpty()
{
int c;
head == NULL ? c = 1: c = 0;
return c;
}
};
class Set:public LinkedList
{
public:
void Union(Set& myListA,Set& myListB)
{
for(Node* iterator1 = myListA.head; iterator1 != NULL; iterator1 = iterator1->next)
{
LinkedList::insertBeginning(iterator1->data);
}
for(Node* iterator2 = myListB.head; iterator2 != NULL; iterator2 = iterator2->next)
{
LinkedList::insertBeginning(iterator2->data);
}
}
void Intersection(Set& myListA, Set& myListB)
{
for(Node* iterator3 = myListA.head; iterator3 != NULL; iterator3 = iterator3->next)
{
//if(LinkedList::find(iterator3) != 1)
for(Node* iterator4 = myListB.head; iterator4 != NULL; iterator4 = iterator4->next)
{
//if(LinkedList::find(iterator4) != 1)
if( iterator3->data == iterator4->data )
{
LinkedList::insertBeginning(iterator4->data);
}
}
}
}
void Difference(Set& myListA, Set& myListB)
{
for(Node* iterator5 = myListA.head; iterator5 != NULL; iterator5 = iterator5->next)
{
for(Node* iterator6 = myListB.head; iterator6 != NULL; iterator6 = iterator6->next)
{
if (iterator6->data != iterator5->data && LinkedList::find(iterator5) != 1 && LinkedList::find(iterator6) != 1) {
LinkedList::insertBeginning(iterator5->data);
}
else {
continue;
}
}
}
}
};
,这是在“Node.h”
下class Node
{
public:
int data;
Node *next;
};
答案 0 :(得分:2)
Difference
的实现过于复杂:由于您的列表已经过排序,您需要做的就是找到不匹配的元素。这需要一个循环,在每次迭代中你移动
Intersection
的实现同样过于复杂,并且只需要一个循环:它只会在没有为Difference()
存储元素的情况下存储公共值。最后,Union()
再次过于复杂:它会在每次迭代中存储一个元素,无论是公共的还是跳过的,取决于采用哪个分支。这也会产生正确的结果。
显然,你真正想要使用的是
std::set_intersection(s0.begin(), s0.end(), s1.begin(), s1.end(),
std::back_inserter(result_intersection));
std::set_union(s0.begin(), s0.end(), s1.begin(), s1.end(),
std::back_inserter(result_union));
std::set_symmetric_difference(s0.begin(), s0.end(), s1.begin(), s1.end(),
std::back_inserter(result_difference));
假设您已经为列表和迭代器提供了标准接口。
答案 1 :(得分:0)
7年后,这是您的答案
void Difference(Set& ListA, Set& ListB)
{
for (member* i5 = ListA.head; i5 != NULL; i5 = i5->next)
{
for (member* i6 = ListB.head; i6 != NULL; i6 = i6->next)
{
if (i6->data != i5->data && ListA.find(i6) != 1 && ListB.find(i5) != 1) {
List::insert(i5->data);
List::insert(i6->data);
}
else {
continue;
}
}
}
}