### I Need Help With Implementing A Function To Find The Intersection Of Two Set Using Pointers.

This Is The Code I Made To Find The Union Of Both Set, But I'm Kinda Lost About Coding The Function To Find The Intersection Of A|B By Modifying The Function AUB. Could Somebody Hint Or Help Me Out Please? Pointer Is Always My Really Bad Subject Of All Time So, If Somebody Could Clearly Guide Me Through This Code To Find The Intersection.

Much Thanks In Advanced And I Deeply Appreciated For Your Time And Efforts.

Here Is The Code To Find The Union Of Both Set A & B (AUB)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126`` ``````#include using namespace std; template class OLL { private:struct NODE { T info; NODE *next; }; NODE *list; public: OLL() { list = NULL; } void insert (T x) { NODE *p = list, *q = list, *r; //create new node r = new (NODE); r -> info = x; r -> next = NULL; //find the insertion place while (p != NULL && p -> info < x) { q = p; p = p -> next; } if (p == list) // x is the first info { list = r; r -> next = p; } else if (p == NULL) // x is the last info { q -> next = r; } else //x is neither first nor last info { r -> next = p; q -> next = r; } } void display () { NODE *p = list; while (p != NULL) { cout<info<<" --> "; p = p -> next; } cout << "NULL\n"; } void display (OLL Tset) { while (Tset.list != NULL) { cout< info<<" --> "; Tset.list = Tset.list -> next; } cout<<"NULL\n"; } void FindAUB(OLL setA, OLL setB, OLL &U) //modify this function to make FindAIB "A Intersect B" { NODE *a = setA.list; NODE *b = setB.list; while (a != NULL && b != NULL) { if (a->info < b->info) { U.insert(a->info); a = a->next; } else if (b->info < a->info) { U.insert(b->info); b = b->next; } else { U.insert(a->info); a = a->next; b = b->next; } } while (a != NULL) { U.insert(a->info); a = a->next; } while (b != NULL) { U.insert(b->info); b = b->next; } } }; int main() { //create set of integers OLL setA, setB, setAUB; //insert set A int A[4] = {3, 9, 6, 8}; int B[7] = {17, 19, 2, 6, 4, 1, 3}; //set A for (int i=0; i<4; ++i) { setA.insert(A[i]); } setA.display(); //set B for (int i=0; i<7; ++i) { setB.insert(B[i]); } setB.display(); //set AUB setA.FindAUB(setA, setB, setAUB); setAUB.display(); //Terminate Program system("pause"); return 0; }``````
Last edited on
I Still Stuck On Making The A Intersect B Function. Could Somebody Help Please?
So, you're trying to find all the elements of A that are also in B, right?

Forget code. Lets say you have some numbers written on red bits of paper. That's set A. Also, you have some numbers written on blue bits of paper. That's set B. Figure out the steps you'd take to work out which numbers appear on both red paper and blue paper. Work out, in detail, what you'd have to do logically to achieve that.

That should then tell you what steps your AUB method will need to make to find the intersection.
Topic archived. No new replies allowed.