// WARNING: THIS CODE HAS NOT BEEN TESTED. // IT WILL TAKE 1-2 MONTHS TO PERFECT THE CODE. // IF YOU FACE ANY ERRORS, UNEXPECTED BEHAVIOUR OR HAVE ANY SUGGESTIONS, // LET US KNOW BY CREATING AN ISSUE ON OUR KSKA GIT REPOSITORY. /* Problem Statement: To create ADT that implement the "set" concept. a. Add (newElement) -Place a value into the set b. Remove (element) Remove the value c. Contains (element) Return true if element is in collection d. Size () Return number of values in collection Iterator () Return an iterator used to loop over collection e. Intersection of two sets f. Union of two sets g. Difference between two sets h. Subset Code from Data Structures and Algorithms (SPPU - Second Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-se-comp-content/DataStructuresAndAlgorithms/ */ // BEGINNING OF CODE #include using namespace std; templateclass Set; templateclass Iterator; template class Node { T data; Node *next; public: Node() { next=NULL; } Node(T x) { data=x; next=NULL; } friend class Set; friend class Iterator; }; template class Iterator { Node *ptr; friend class Set; public: Iterator() { ptr=NULL; } void set(Node *&t) { ptr=t; } void operator++(int) { ptr=ptr->next; } void next() { ptr=ptr->next; } bool operator!=(Iterator a) { if(a.ptr!=NULL&&ptr!=NULL) { if((a.ptr)->data==(ptr)->data) return false; return true; } if(a.ptr==NULL&&ptr==NULL) return false; return true; } void del() { delete ptr; } }; template class Set { Node *head; public: Set() { head=NULL; } Iterator begin() { Iterator a; a.ptr=head; return a; } Iterator end() { Iterator a; a.ptr=NULL; return a; } int size(); void display(); void insert(T); bool remove(T); bool contain(T); bool subSet(Set&); Set unionOf(Set&); Set intersectionOf(Set&); Set differenceOf(Set&); }; template int Set::size() { Iterator it; it.set(head); int cnt=0; while(it!=end()) { cnt++; it++; } return cnt; } template void Set::insert(T x) { Node *curr=head,*prev=NULL; while(curr!=NULL&&curr->datanext; } if(curr!=NULL&&curr->data==x) { cout<(x); head->next=curr; } else { prev->next=new Node(x); prev->next->next=curr; } } template void Set::display() { Iterator it; it.set(head); while(it!=end()) { cout<data<<" "; it.next(); } cout<<"\n"; } template bool Set::contain(T x) { Iterator it; it.set(head); while(it!=end()) { if(it.ptr->data==x) return true; it++; } return false; } template bool Set::remove(T x) { if(contain(x)) { Iterator curr,prev; curr=begin(); while(curr.ptr->data!=x) { prev=curr; curr.next(); } if(prev.ptr==NULL) head=head->next; else prev.ptr->next=curr.ptr->next; curr.del(); return true; } else return false; } template bool Set::subSet(Set &S2) { if(head==NULL) return true; if(S2.head==NULL) return false; Iterator p,q; p=begin(); q=S2.begin(); int cnt=0; while(q!=S2.end()&&p!=end()) { if(p.ptr->data==q.ptr->data) { cnt++; p++; } q.next(); } if(cnt==size()) return true; return false; } template Set Set::unionOf(Set &S2) { Set U; Iterator p=begin(),q=S2.begin(); Node *r=NULL; if(p!=end()&&q!=S2.end()) { if(p.ptr->datadata) { U.head=new Node(p.ptr->data); p++; } else { U.head=new Node(q.ptr->data); if(p.ptr->data==q.ptr->data) p++; q++; } } r=U.head; while(p!=end()&&q!=S2.end()) { if(p.ptr->datadata) { r->next=new Node(p.ptr->data); p++; } else { r->next=new Node(q.ptr->data); if(p.ptr->data==q.ptr->data) p++; q++; } r=r->next; } if(r==NULL) { if(p.ptr==NULL) { U.head=new Node(q.ptr->data); q++; } else { U.head=new Node(p.ptr->data); p++; } r=U.head; } while(p!=end()) { r->next=new Node(p.ptr->data); r=r->next; p++; } while(q!=S2.end()) { r->next=new Node(q.ptr->data); r=r->next; q++; } return U; } template Set Set::intersectionOf(Set &S2) { Set I; Iterator p=begin(),q=S2.begin(); Node *r=NULL; while(p!=end()&&q!=S2.end()) { if(p.ptr->data==q.ptr->data) { if(I.head==NULL) { I.head=new Node(p.ptr->data); r=I.head; } else r->next=new Node(p.ptr->data); q++; p++; } else if(p.ptr->data>q.ptr->data) q++; else p++; } return I; } template Set Set::differenceOf(Set &S2) { Node *r; Iterator p,q; if(head==NULL) { Set D; return D; } Set D; p=begin(); q=S2.begin(); while(p!=end()&&q!=S2.end()) { if(p.ptr->data==q.ptr->data) { p.next(); q.next(); } else { if(D.head==NULL) { D.head=new Node(p.ptr->data); r=D.head; } else { r->next=new Node(p.ptr->data); r=r->next; } p++; } } if(D.head==NULL&&p!=end()) { D.head=new Node(p.ptr->data); p.next(); r=D.head; } while(p!=end()) { r->next=new Node(p.ptr->data); p.next(); r=r->next; } return D; } template void operations(T var) { Set S1,S2,Res; T ele; int work,choice; do { cout<<"Enter\n1 To Work with Set 1\n2 To Work with Set 2\n0 To EXIT\n"; cin>>work; switch(work) { case 1: do { cout<<"Enter\n1 To Insert\n2 To Remove\n3 To Search\n4 To Print Size\n5 To Check if Subset\n"; cout<<"6 To Find Union\n7 To Find Intersection\n8 To Find Difference\n0 To Previous Menu\n"; cin>>choice; switch(choice) { case 1: cout<<"Enter the Element to Insert\n"; cin>>ele; S1.insert(ele); S1.display(); break; case 2: cout<<"Enter the Element to Remove\n"; cin>>ele; if(S1.remove(ele)) cout<<"Element Removed\n"; else cout<<"Element Not Present\n"; S1.display(); break; case 3: cout<<"Enter the Element to search\n"; cin>>ele; if(S1.contain(ele)) cout<<"Element Present\n"; else cout<<"Element Not Present\n"; break; case 4: cout<<"Set Size is "<>choice; switch(choice) { case 1: cout<<"Enter the Element to Insert\n"; cin>>ele; S2.insert(ele); S2.display(); break; case 2: cout<<"Enter the Element to Remove\n"; cin>>ele; if(S2.remove(ele)) cout<<"Element Removed\n"; else cout<<"Element Not Present\n"; S2.display(); break; case 3: cout<<"Enter the Element to search\n"; cin>>ele; if(S2.contain(ele)) cout<<"Element Present\n"; else cout<<"Element Not Present\n"; break; case 4: cout<<"Set Size is "<>ch; if(ch==1) operations((int)1); else if(ch==2) operations((float)1.0f); else if(ch==3) operations('a'); else cout<<"Invalid Choice\n"; return 0; } // END OF CODE // EXPERIMENTAL CODE