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

Need answer in C++ Q) Create a class Bag that uses a linked list to store the ba

ID: 3881317 • Letter: N

Question

Need answer in C++

Q) Create a class Bag that uses a linked list to store the bag items. The item type must be char. The class should have the methods listed below. Create a main() that will store in a bag object a fixed number of characters entered by the program user. After the input is completed, the program should modify the bag content so that it does not contain any duplicate characters, if duplicates were entered. For example, if the user entered 'M' 'I' 'S' 'S' 'I' 'S' 'S' 'I' 'P' 'P' 'I', the characters remaining in the bag after the removal of duplicates would be 'M' 'I' 'S' 'P'.

Bag(): default constructor

~Bag(): class destructor

bool isEmpty(): determines whether the bag is empty

void print(): prints the bag elements

int getSize(): returns the number of items in the bag

void clear(): removes all of the items from the bag

void add(char item): adds an item to the bag

void remove(char item): removes an item from the bag; only one occurrence of the item should be removed.

int count(char item): counts the number of occurrences of an item in the bag.

(Note that you can reuse the code in Exercise 1 for the LinkedList class to create your Bag class. It will help you to save development time.)

Explanation / Answer

#include <iostream>

using namespace std;

class Bag {
private:
   struct Node {
       char data;
       Node * next;
   };

   Node *start;

public:
   Bag() { //: default constructor
       start = NULL;
   }

   ~Bag() { //: class destructor
       Node *tmp;
       while (start != NULL) {
           tmp = start;
           start = start -> next;
           delete tmp;
       }
   }

   bool isEmpty() { //: determines whether the bag is empty
       return start == NULL;
   }

   void print() { //: prints the bag elements
       Node *tmp = start;
       cout << "***CONTENTS***" << endl;
       while (tmp != NULL) {
           cout << tmp -> data << endl;
           tmp = tmp -> next;
       }

       cout << "***END OF LIST***" << endl;
   }

   int getSize() { //: returns the number of items in the bag
       Node *tmp = start;
       int i = 0;
       while (tmp != NULL) {
           i++;
           tmp = tmp -> next;
       }

       return i;
   }

   void clear() { //: removes all of the items from the bag
       Node *tmp;
       while (start != NULL) {
           tmp = start;
           start = start -> next;
           delete tmp;
       }
   }

   void add(char item) { // : adds an item to the bag
       Node *tmp = new Node();
       tmp -> data = item;
       tmp -> next = start;
       start = tmp;
   }

   void remove(char item) { // : removes an item from the bag; only one occurrence of the item should be removed.
       if (isEmpty())
           return;

       Node *tmp = start;
      
       if (start -> data == item) {
           tmp = start;
           start = start -> next;
           delete tmp;
           return;
       }

       while (tmp ->next != NULL) {
           if (tmp -> next -> data == item) {
               Node *bkup = tmp -> next;
               tmp -> next = tmp -> next -> next;
               delete bkup;
               return;
           }
           tmp = tmp -> next;
       }
   }

   int count(char item) { //: counts the number of occurrences of an item in the bag.
       Node *tmp = start;
       int i = 0;
       while (tmp != NULL) {
           if (tmp -> data == item)
               i++;
           tmp = tmp -> next;
       }

       return i;
   }

};

int main(int argc, char const *argv[])
{
   /* code */
   Bag obj;
   cout << "printing enpty LIST" << endl;
   obj.print();

   cout << "Adding 'M' 'I' 'S' 'S' 'I' 'S' 'S' 'I' 'P' 'P' 'I'" << endl;
   obj.add('M');
   obj.add('I');
   obj.add('S');
   obj.add('S');
   obj.add('I');
   obj.add('S');
   obj.add('S');
   obj.add('I');
   obj.add('P');
   obj.add('P');
   obj.add('I');


   cout << "printing LIST" << endl;
   obj.print();


   cout << "Removing duplicates from LIST" << endl;
   obj.remove ('S');
   obj.remove ('S');
   obj.remove ('S');
   obj.remove ('I');
   obj.remove ('I');
   obj.remove ('I');
   obj.remove ('P');


   cout << "printing LIST" << endl;
   obj.print();


   cout << "printing LIST count" << endl;
   cout << obj.getSize()<< endl;

   return 0;
}

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