// File: Set_ADT.cpp // Implementations for set of objects ADT #include "Set_ADT.h" // Initialize Set to Empty template SetADT::SetADT() {Elts=0; ListOfElts=NULL;} // Add Item to Set // Pre: None // Post: If Elt already in set, size of set unchanged, otherwise // set has cardinality increase by 1 // Returns: 0=> Elt already in Set 1=> Elt added to Set template int SetADT::addElt(eltType Elt) {if (orderedSearch(ListOfElts,Elts,Elt)>=0) return(0); if (!Elts) ListOfElts=new eltType[1]; else ListOfElts=(eltType *)realloc(ListOfElts,(Elts+1)*sizeof(eltType)); ListOfElts[Elts++]=Elt; selSort(ListOfElts,Elts); return(1); } // Remove Item From Set // Pre: None // Post: If Elt not in set, size of set unchanged, otherwise // set has cardinality decrease by 1 // Returns: 0=> Elt not in Set 1=> Elt Removed From Set template int SetADT::removeElt(eltType Elt) {int Index; if ((Index=orderedSearch(ListOfElts,Elts,Elt))<0) return(0); ListOfElts[Index]=ListOfElts[Elts-1]; if (!(--Elts)) { delete ListOfElts; ListOfElts=NULL; } else ListOfElts=(eltType *)realloc(ListOfElts,Elts*sizeof(eltType)); selSort(ListOfElts,Elts); return(1); } // Return # Elts in Set // Pre: None // Post: Set Unchanged // Returns: Cardinality of Set template int SetADT::cardinality() const {return Elts;} // Determine if an element is a member of the set // Pre: None // Post: Set Unchanged. Result dependent on argument membership // Returns: 0=> Elt not in Set 1=> Elt in Set template int SetADT::operator >(eltType Elt) {if (orderedSearch(ListOfElts,Elts,Elt)<0) return(0); return(1); } // Friends that perform normal set operations // Return Union of 2 Sets // Pre: None // Post: Returns union of argument SetADTs with no repitition of elements // Resulting set is ordered template SetADT SetADT::operator+(SetADT right) {SetADT Result; int Index; for (Index=0;IndexListOfElts[Index]) right.removeElt(ListOfElts[Index]); Result.addElt(ListOfElts[Index]); } for (Index=0;Index SetADT SetADT::operator *(SetADT right) {SetADT Result; int Index; for (Index=0;IndexListOfElts[Index]) Result.addElt(ListOfElts[Index]); selSort(Result.ListOfElts,Result.cardinality()); return(Result); } // Return Set Difference - Elts that are in 1st Set but not 2nd // Pre: None // Post: Returns difference of argument SetADTs template SetADT SetADT::operator-(SetADT right) {SetADT Result; int Index; for (Index=0;IndexListOfElts[Index])) Result.addElt(ListOfElts[Index]); selSort(Result.ListOfElts,Result.cardinality()); return(Result); } // Print the set // Pre:None // Post:None template ostream &operator<<(ostream &out,SetADT Set) {out << "Elements in the set are:\n"; for (int i=0;i; template ostream &operator<<(ostream &out,SetADT Set);