# intersection of two sets

This is a discussion on intersection of two sets within the C++ Programming forums, part of the General Programming Boards category; Evening everyone! Can someone find my mistake in my code? I am at a loss. Code: void Intersection(Set& a, Set& ...

1. ## intersection of two sets

Evening everyone! Can someone find my mistake in my code? I am at a loss.
Code:
```void Intersection(Set& a, Set& b) {
Node* p;
Node* q;
for( p = a.front; p != NULL; p = p->link)
{
for( q = b.front; p != NULL; p = p->link)
{
if (p->data == q->data)
{
Insert(p->data);
}
}
}
}```

2. O_o

Something seems very familiar about those looping constructs...

Also, `SetIntersection' should logically return a new set; it doesn't make any sense to mutate by insertion either starting set with the shared elements because they already must exist by definition.

Soma

3. What jumps out most to me is that this:

Code:
```for( p = a.front; p != NULL; p = p->link)
{
for( q = b.front; p != NULL; p = p->link)
{```
should probably be this:

Code:
```for( p = a.front; p != NULL; p = p->link)
{
for( q = b.front; q != NULL; q = q->link)
{```

4. I appreciate it everyone! I figured out the intersection last night but now my problem is I believe the call of the intersection. My program freezes up so I included the whole code. The union works with no problem when I run the whole program. Any help would be appreciated. Please do not mind the extra variables in int main().
Code:
```#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
class Node
{
public:
int data;
Node* next;
};
class List
{ protected:
public:
List()
{
}
~List();
int search(int x);
void display();
};

{

Node* newnode = new Node;

newnode->data = x;

{

newnode->next = NULL;

}
else
{

}

}

void List::display()
{
while(currnode!=NULL)
{
cout<<currnode->data<<" ";
currnode=currnode->next;
}
}

int List::search(int x)
{

Node* currnode;

int count =0 ;

for (currnode = head; currnode != NULL; currnode = currnode->next)
{

if (currnode->data == x)
{

return count;

}

count++;

}

return -1;

}

List::~List()
{
Node* nextnode=NULL;
while(currnode)
{
nextnode->next=currnode->next;
delete currnode;
currnode=nextnode;
}
}

class Set:public List
{
public:
void insnew(int x)
{ int copy;
copy=search(x);
if(copy==-1)
}
void Union(Set& a,Set& b)
{
Node* p;
{
insnew(p->data);
}
for( p= b.head;p!= NULL;p = p->next)
{
insnew(p->data);

}
}

void intersection(Set& a,Set& b)
{

Node* p;

Node* q;

for( p = a.head; p != NULL; p = p->next)

{

for( q = b.head; q != NULL; q = q->next)

{

if (p->data == q->data)

{

insnew(p->data);

}

}

}

}

};

int main()

{

Set setA, setB, setUnion, setIntersection;
int i,m,n,a,b;
cout<<"Enter no of elements in set A: ";
cin>>n;
cout<<"Enter elements of A: ";
for(i=0;i<n;i++)
{
cin>>a;
setA.insnew(a);
}
cout<<"Enter no of elements in set B: ";
cin>>m;
cout<<"Enter elements of B: ";
for(i=0;i<m;i++)
{
cin>>b;
setB.insnew(b);
}

setUnion.Union(setA, setB);

cout << "Contents of setA union setB: ";
setUnion.display();

setIntersection.intersection(setA, setB);
setIntersection.display();

cout << "Contents of setA intersection setB: ";

return 0;
}```

5. I found what my mistake was. Can anyone help with a difference of the set?

6. Help how? It's the set of elements in either but not both sets.