Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,199 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,324 people online right now. Registration is fast and FREE... Join Now!




Linked List: class Set

 
Reply to this topicStart new topic

Linked List: class Set, adding multiple elements

rancor
21 May, 2008 - 07:38 AM
Post #1

New D.I.C Head
*

Joined: 30 Nov, 2007
Posts: 22



Thanked: 3 times
My Contributions
This is a linked list program implemented using class Set. I have an addElement function that stores one element at a time, and a main program (switch case) that executes each function.
Instead of having the user add of remove an element one at a time, I want to have them be able to add or remove multiple elements at the same time (i.e. removing a line of elements separated by a space or a comma). Is there an easy way to do this?

Specifically, take a look at cases 2 and 3 (where the add and remove functions are executed):
CODE

#include <math.h>
#include <iostream>
#include <iostream>
#include <vector>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
class Set
{
   private:
      struct Node
      {        
         int data;
         Node *next;        
         Node (int d = 0, Node *p = NULL): data(d), next(p) {}
         ~Node() { delete next; }
      }*head;

   public:      
      Set() { head = NULL; }
      bool contains(int);
      bool addElement(int);
      bool remove(int);
      int enumerate();
      Set intersection(Set&);
      Set unionWith(Set&);
      Set difference(Set&);
      void display();
      ~Set() { delete head; }
}; // end class Set
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
// Dynamically creates a set when called

void createSet(vector<Set> &sets)
{
   Set s;
   sets.push_back(s);
}
//------------------------------------------------------------------------------
// Verifies user input

int checkInput()
{
   int ret;
   while(cin.fail())
   {
      if (cin.fail())
      {
         cin.clear();
         cin.ignore(80,'\n');        
      }
      cout << "Invalid selection, please try again: ";
      cin >> ret;
   }
   return ret;
}
//------------------------------------------------------------------------------
//******************************************************************************
//                            MAIN PROGRAM
//******************************************************************************
int main()
{    
   const int MAX_SETS = 6;
   int numSets = 0;
   vector<Set> setVector(0);    // Conatainer to hold each set the user creates
  
   int selection;  
   while (selection != 9)
   {
      cout << "***************************************" << endl;
      cout << "      Welcome to the set program!" << endl;
      cout << "***************************************" << endl;
      cout << "1. Create a set" << endl;
      cout << "2. Add an element to a set" << endl;
      cout << "3. Remove an element from a set" << endl;
      cout << "4. View the current elements in a set" << endl;
      cout << "5. Compute the intersection of two sets" << endl;
      cout << "6. Compute the union of two sets" << endl;
      cout << "7. Compute the difference of two sets" << endl;
      cout << "8. Compute the enumeration of a set" << endl;
      cout << "9. Exit the program" << endl << endl;
      
      cin >> selection;
      if(cin.fail())  { selection = checkInput(); }
      
      switch(selection)
      {
         case 1:
         {
            if(numSets < MAX_SETS)
            {              
               createSet(setVector);
               cout << "\nSet " << numSets+1 << " has been created."
                    << endl << endl;
               numSets++;              
            }
            else
            {
               cout << "\nYou have reached the maximum number of sets; "
                    << "no more sets can be created."  << endl << endl;
            }                                    
            break;
         }
        
         case 2:
         {
            int setNumber = 0, toBeAdded = 0;
            cout << "\nEnter which set you would like to add an element to: ";
            cin >> setNumber;
            if(cin.fail()) { setNumber = checkInput(); }
            if(setNumber > setVector.size() || setNumber < 0)            
            {
               cout << "\nInvalid set number, please try again. " << endl << endl;
               continue;
            }
            if(setVector[setNumber-1].enumerate() > 0)
            {
               cout << "Set " << setNumber << ": ";
               setVector[setNumber-1].display();
            }                                                        ////////////////////////////////////////////////////////////////
            cout << "Enter value to be added: ";     // HERE; is there any way to take in a line of integers?
            cin >> toBeAdded;                              ////////////////////////////////////////////////////////////////
            if(cin.fail()) { toBeAdded = checkInput(); }
            
            if(setVector[setNumber-1].addElement(toBeAdded))   // and add them to the list?
               cout << toBeAdded << " was added to set "
                    << setNumber << endl << endl;
            else { cout << toBeAdded << " is already in the set" << endl << endl; }
                                                                            
            break;
         }
            
         case 3:
         {
            int setNumber = 0, toBeRemoved = 0;
            cout << "\nEnter which set you would like to remove an element from: ";
            cin >> setNumber;
            if(cin.fail()) { setNumber = checkInput(); }
            if(setNumber > setVector.size() || setNumber < 0)
            {
               cout << "\nInvalid set number, please try again. " << endl << endl;
               continue;
            }
            cout << "Set " << setNumber << ": ";
            setVector[setNumber-1].display();
            cout << "Enter value to be removed: ";
            cin >> toBeRemoved;
            if(cin.fail()) { toBeRemoved = checkInput(); }
            
            if (setVector[setNumber-1].remove(toBeRemoved))            ////////////////////////////////
               cout << toBeRemoved << " was removed from set "
                    << setNumber << endl;
            else { cout << toBeRemoved << " is not in the set" << endl << endl; }
                                                                                
            break;
         }
        
         case 4:
         {
            int setNumber = 0;
            cout << "\nEnter which set you would like to see: ";
            cin >> setNumber;
            if(cin.fail()) { setNumber = checkInput(); }
            if(setNumber > setVector.size() || setNumber < 0)
            {
               cout << "\nInvalid set number, please try again. " << endl << endl;
               continue;
            }
            cout << "\nSet " << setNumber << ": ";
            setVector[setNumber-1].display();
            cout << endl;
            break;
         }
              
         case 5:
         {
            int firstSet, secondSet;
            cout << "\nEnter the two sets to compute the intersection of " << endl;
            cout << "First set: ";
            cin >>  firstSet;
            if(cin.fail()) { firstSet = checkInput(); }
            cout << "Second set: ";
            cin >> secondSet;
            if(cin.fail()) { secondSet = checkInput(); }
            if(firstSet < setVector.size() && secondSet < setVector.size())
            {
               cout << "\nInvalid set number, please try again." << endl;
               continue;
            }
            cout << "Set A: ";
            setVector[firstSet-1].display();
            cout << "Set B: ";
            setVector[secondSet-1].display();
            
            cout << "The intersection of the two: ";
            setVector[firstSet-1].intersection(setVector[secondSet-1]).display();
            cout << endl;
            break;
         }
        
         case 6:
         {
            int firstSet, secondSet;
            cout << "\nEnter the two sets to compute the union of " << endl;
            cout << "First set: ";
            cin >>  firstSet;
            if(cin.fail()) { firstSet = checkInput(); }
            cout << "Second set: ";
            cin >> secondSet;
            if(cin.fail()) { secondSet = checkInput(); }
            if(firstSet < setVector.size() && secondSet < setVector.size())
            {
               cout << "\nInvalid set number, please try again." << endl;
               continue;
            }
            cout << "Set A: ";
            setVector[firstSet-1].display();
            cout << "Set B: ";
            setVector[secondSet-1].display();
            
            cout << "The union of the two: ";          
            setVector[firstSet-1].unionWith(setVector[secondSet-1]).display();
            cout << endl;
            break;
         }
        
         case 7:
         {
            int firstSet, secondSet;
            cout << "\nEnter the two sets to compute the difference of ";
            cout << "First set: ";
            cin >>  firstSet;
            if(cin.fail()) { firstSet = checkInput(); }
            cout << "Second set: ";
            cin >> secondSet;
            if(cin.fail()) { secondSet = checkInput(); }
            if(firstSet < setVector.size() && secondSet < setVector.size())
             {
               cout << "\nInvalid set number, please try again." << endl;
               continue;
             }
            cout << "Set A: ";
            setVector[firstSet-1].display();
            cout << "Set B: ";
            setVector[secondSet-1].display();
            
            cout << "The difference of the two: ";
            setVector[firstSet-1].difference(setVector[secondSet-1]).display();
            cout << endl;
            break;
         }
        
         case 8:
         {
            int setNumber;
            cout << "\nEnter the set to compute the enumeration of: ";
            cin >> setNumber;
            if(cin.fail()) { setNumber = checkInput(); }
            if(setNumber > setVector.size())
            {
               cout << "\nInvalid set number, please try again." << endl;
               continue;
            }
            
            cout << "\nSet " << setNumber << " contains "
                 << setVector[setNumber-1].enumerate() << " elements."
                 << endl;          
            break;
         }
      }
   }
   cout << "\nExiting program." << endl;
   return 0;    
}
//******************************************************************************
//                            FUNCTION DEFINITIONS
//******************************************************************************
//------------------------------------------------------------------------------
// Returns true if and only if this Set contains the arguement element.

bool Set::contains(int element)
{          
   for(Node *temp = head; temp; temp = temp->next)
      if (temp->data == element) return true;
   return false;
}
//------------------------------------------------------------------------------
// Adds the argument element to the Set. Returns false if this Set already
// contains the element being added.

bool Set::addElement(int element)
{
   if(!head) { head = new Node(element); return true; }
   if(contains(element)) { return false; }
  
   Node *temp = head;
   for (;temp->next; temp = temp->next);    
   temp->next = new Node(element);
   return true;  
}
//------------------------------------------------------------------------------
// Removes the argument element from the Set. Returns false if this Set does
// not contain the element being removed.

bool Set::remove(int element)
{
   Node *prev = NULL;
   for(Node *temp = head; temp;)
   {
      if(element == temp->data)
      {
         if(!prev)
         {
            prev = temp;
            temp = temp->next;
            prev->next = NULL;
            delete prev;
            prev = NULL;
            head = temp;
            return true;
         }
         else                          
         {
            prev->next = temp->next;
            temp->next = NULL;
            delete temp;
            temp = prev->next;
            return true;
         }
      }
      else                          
      {
         prev = temp;
         temp = temp->next;
      }
   }
   return false;
}
//------------------------------------------------------------------------------


If any verification or more of my code is needed, please let me know. Thanks.

This post has been edited by rancor: 21 May, 2008 - 07:40 AM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:21PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month