This Is The Code To Find The Union From Two Sets A & B.
Code:
#include <iostream> using namespace std; template<class T> 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<<p->info<<" --> "; p = p -> next; } cout << "NULL\n"; } void display (OLL <T> Tset) { while (Tset.list != NULL) { cout<<Tset.list -> info<<" --> "; Tset.list = Tset.list -> next; } cout<<"NULL\n"; } void FindAUB(OLL setA, OLL setB, OLL &U) { 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 <int> 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; }