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. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    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. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,260
    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. #3
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    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. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    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:
        Node* head;
     public:
     List()
     {
      head=NULL;
     }
     ~List();
     void Inserthead(int x);
     int search(int x);
     void display();
    };
    
    void List::Inserthead(int x)
    {
    
       Node* newnode = new Node;
    
       newnode->data = x;
    
       if (head == NULL)
       {
    
         head=newnode;
    
         newnode->next = NULL;
    
        }
        else
        {
    	 newnode->next = head;
    
    	 head = newnode;
    
         }
    
     }
    
    
    void List::display()
    {
     Node* currnode=head;
     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* currnode=head;
     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)
        Inserthead(x);
     }
     void Union(Set& a,Set& b)
     {
       Node* p;
       for(p=a.head;p!=NULL;p=p->next)
       {
        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;
    }
    Last edited by csharp100; 04-05-2011 at 12:49 PM.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    I found what my mistake was. Can anyone help with a difference of the set?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    Help how? It's the set of elements in either but not both sets.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 07:09 PM
  2. Major Problems with GetSaveFileName() and GetOpenFileName()
    By CodeHacker in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2004, 11:05 AM
  3. creating new sets
    By axon in forum C++ Programming
    Replies: 7
    Last Post: 12-03-2003, 05:37 PM
  4. Problem with My Sets ADT
    By jawwadalam in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 06:36 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 09:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21