Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ Learning Objectives: The objective of this lab is to gain experience working

ID: 3573578 • Letter: C

Question

c++

Learning Objectives: The objective of this lab is to gain experience working with linked list.

Assignment:   
In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted.
The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the linked list. It should provide the following public methods:
•   IntegerSet(int size): this constructor method creates a new set of size integers by prompting the user to enter size elements for the set on the keyboard.


•   int size(): this method returns the cardinality of the set.


•   boolean isEmpty(): this method returns true if the set has no elements, false otherwise.


•   boolean isMember(int item): this method returns true if item is an element of the set, false otherwise.


•   boolean add(int item): if item is not in the set, this method adds it to the set and returns true; otherwise, the set is unchanged and the method returns false.


•   boolean remove(int item): if item is in the set, this method removes it from the set and returns true; otherwise, the set is unchanged and the method returns false.


•   boolean isSubset(IntegerSet set2): this method returns true if every element in set2 is also in the calling set, false otherwise.


•   IntegerSet intersection(IntegerSet set2): this method returns a new set that is the intersection of the calling set and set2. An integer is an element of the intersection set if it is in both the calling set and set2.


•   IntegerSet union(IntegerSet set2): this method returns a new set that is
the union of the calling set and set2. An integer is an element of the union set if it is in either the calling set or set2, or both. Keep in mind that the union set formed should not contain any duplicates.


•   IntegerSet difference(IntegerSet set2): this method returns a new set that is the difference between the calling set and set2 (in this order). An integer is an element of the difference set if it is in the calling set but not in set2.


•   void display(): this method prints all elements of the set on the screen.

In addition to the IntegerSet class, you are also required to write a test driver that serves as the test harness for the IntegerSet class. This test harness should provide the user with options to explicitly test each method in the IntegerSet class.

Explanation / Answer

#include <iostream>
using namespace std;

struct Node {
   int value;
   Node * next;
};

class IntegerSet {
  
   private:
       Node * head = null;
       int cardinality = 0;
       Node * getNode(int n);

   public:
       void IntegerSet(int n);
       public int getSize();
       boolean isEmpty();
       boolean isMember(int item);
       boolean add(int item);
       boolean remove(int item);
       boolean isSubset(IntegerSet set2);
       IntegerSet intersection(IntegerSet set2);
       IntegerSet union(IntegerSet set2);
       IntegerSet difference(IntegerSet set2);
       void display();
};
      
      
Node * IntegerSet::getNode(int n) {
   Node * newNode = new Node();
   newNode->data = n;
   newNode->next = null;
   return newNode;
};

void IntegerSet::IntegerSet(int n) {
   int value;
   cout<<" Please enter the n numbers ";

   cardinality = n;

   for (int i = 0;i<n; i++) {
       cin>>value;
       add(value);
   }
};
  
public int IntegerSet::getSize() {
   return cardinality;
}

boolean IntegerSet::isEmpty() {
   if (head == null && cardinality == 0) {
       return true;
   } else {
       return false;
   }
}

boolean IntegerSet::isMember(int item) {
   Node * tmp = head;
  
   while(tmp) {
       if(tmp->data == item) {
           return true;
       }
       tmp = tmp->next;
   }
  
   return false;
}

boolean IntegerSet::add(int item){
   if(isMember(item)) {
       return false;
   } else {
       Node * node = getNode(item);
       if (head == null) {
           head = node;
       } else {
           Node * tmp = head;
           head = node;
           node->next = head;
       }
       return true;
   }
}

boolean IntegerSet::remove(int item) {
   if(isMember(item)) {
       Node * tmp = head;
       Node * next = tmp->next;
       while(tmp) {
           if(tmp->data == item) {
               if(next) {
                   tmp->data = next->data;
                   tmp->next = next->next;
                   delete(next);
                   return true;
               } else {
                   delete(tmp);
                   tmp = next;
                   return true;
               }
           }
           tmp = next;
           if(next) {
               next = tmp->next;
           } else {
               next = null;
           }
       }
       return false;
   }
}

void IntegerSet::display() {
   Node *tmp = head;
   while(tmp) {
       cout<<tmp->data<<" ";
       tmp= tmp->next;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote