Question

This is a c++ class utilizing class templates and linked lists and Nodes. I need to...

This is a c++ class utilizing class templates and linked lists and Nodes. I need to implement the following member function(s) to LinkedBag.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done.

In this case, I need to join the original items with the user items(a_bag). So if the original has {1,2,3} and a_bag has {4,5,6}, the program would return {1,2,3,4,5,6}.

/** @param a_bag to be combined with the contents of this (the calling) bag @return a new LinkedBag that contains all elements from this bag (items_)and from a_bag. Note that it may contain duplicates */

LinkedBag bagUnion(const LinkedBag& a_bag) const;

Node.hpp

-----------------------------------------------
/** @file Node.h
Listing 4-1 */
#ifndef NODE_
#define NODE_

template
class Node
{
public:
Node();
Node(const T& an_item_);
Node(const T& an_item, Node* next_node_ptr);
void setItem(const T& an_item);
void setNext(Node* next_node_ptr);
T getItem() const ;
Node* getNext() const ;


private:
T item_; // A data item_
Node* next_; // Pointer to next_ node
}; // end Node

//#include "Node.cpp" //Unsure if this should be included or not
#endif

-----------------------------------------------

Node.cpp

-----------------------------------------------

#include "Node.hpp"
//#include

template
Node::Node() : next_(nullptr)
{
} // end default constructor

template
Node::Node(const T& an_item) : item_(an_item), next_(nullptr)
{
} // end constructor

template
Node::Node(const T& an_item, Node* next_node_ptr) :
   item_(an_item), next_(next_node_ptr)
{
} // end constructor

template
void Node::setItem(const T& an_item)
{
   item_ = an_item;
} // end setItem

template
void Node::setNext(Node* next_node_ptr)
{
   next_ = next_node_ptr;
} // end setNext

template
T Node::getItem() const
{
   return item_;
} // end getItem

template
Node* Node::getNext() const
{
   return next_;
} // end getNext

-----------------------------------------------

LinkedBag.hpp

-----------------------------------------------


/** ADT bag: Link-based implementation.
@file LinkedBag.h
Listing 4-3 */
#ifndef LINKED_BAG_
#define LINKED_BAG_

#include
#include
#include
#include "Node.hpp"

template
class LinkedBag
{
public:
LinkedBag();
LinkedBag(const LinkedBag& a_bag); // Copy constructor
~LinkedBag(); // Destructor
int getCurrentSize() const;
bool isEmpty() const;
bool add(const T& new_entry);
bool remove(const T& an_entry);
void clear();
bool contains(const T& an_entry) const;
int getFrequencyOf(const T& an_entry) const;
std::vector toVector() const;


private:
Node* head_ptr_; // Pointer to first node
int item_count_; // Current count of bag items

/**
@return Returns either a pointer to the node containing a given entry
or the null pointer if the entry is not in the bag.
*/
Node* getPointerTo(const T& target) const;


}; // end LinkedBag

//#include "LinkedBag.cpp" //Unsure if this should be included or not
#endif

-----------------------------------------------

LinkedBag.cpp

-----------------------------------------------

#include "LinkedBag.hpp"
#include "Node.hpp"
#include

template
LinkedBag::LinkedBag() : head_ptr_(nullptr), item_count_(0)
{
} // end default constructor

template
LinkedBag::LinkedBag(const LinkedBag& a_bag)
{
   item_count_ = a_bag.item_count_;
   Node* orig_chain_ptr = a_bag.head_ptr_; // Points to nodes in original chain

   if (orig_chain_ptr == nullptr)
       head_ptr_ = nullptr; // Original bag is empty
   else
   {
       // Copy first node
       head_ptr_ = new Node();
       head_ptr_->setItem(orig_chain_ptr->getItem());

       // Copy remaining nodes
       Node* new_chain_ptr = head_ptr_; // Points to last node in new chain
       orig_chain_ptr = orig_chain_ptr->getNext(); // Advance original-chain pointer

       while (orig_chain_ptr != nullptr)
       {
           // Get next item from original chain
           T next_item = orig_chain_ptr->getItem();

           // Create a new node containing the next item
           Node* new_node_ptr = new Node(next_item);

           // Link new node to end of new chain
           new_chain_ptr->setNext(new_node_ptr);

           // Advance pointer to new last node
           new_chain_ptr = new_chain_ptr->getNext();

           // Advance original-chain pointer
           orig_chain_ptr = orig_chain_ptr->getNext();
       } // end while

       new_chain_ptr->setNext(nullptr); // Flag end of chain
   } // end if
} // end copy constructor

template
LinkedBag::~LinkedBag()
{
   clear();
} // end destructor

template
bool LinkedBag::isEmpty() const
{
   return item_count_ == 0;
} // end isEmpty

template
int LinkedBag::getCurrentSize() const
{
   return item_count_;
} // end getCurrentSize

template
bool LinkedBag::add(const T& new_entry)
{
   // Add to beginning of chain: new node references rest of chain;
   // (head_ptr_ is null if chain is empty)
   Node* next_node_ptr = new Node();
   next_node_ptr->setItem(new_entry);
   next_node_ptr->setNext(head_ptr_); // New node points to chain
                                       // Node* next_node_ptr = new Node(new_entry, head_ptr_); // alternate code

   head_ptr_ = next_node_ptr; // New node is now first node
   item_count_++;

   return true;
} // end add


template
std::vector LinkedBag::toVector() const
{
   std::vector bag_contents;
   Node* cur_ptr = head_ptr_;
   while ((cur_ptr != nullptr))
   {
       bag_contents.push_back(cur_ptr->getItem());
       cur_ptr = cur_ptr->getNext();
   } // end while

   return bag_contents;
} // end toVector

template
bool LinkedBag::remove(const T& an_entry)
{
   Node* entry_node_ptr = getPointerTo(an_entry);
   bool can_remove = !isEmpty() && (entry_node_ptr != nullptr);
   if (can_remove)
   {
       // Copy data from first node to located node
       entry_node_ptr->setItem(head_ptr_->getItem());

       // Delete first node
       Node* node_to_delete = head_ptr_;
       head_ptr_ = head_ptr_->getNext();

       // Return node to the system
       node_to_delete->setNext(nullptr);
       delete node_to_delete;
       node_to_delete = nullptr;

       item_count_--;
   } // end if

   return can_remove;
} // end remove


template
void LinkedBag::clear()
{
   Node* node_to_delete = head_ptr_;
   while (head_ptr_ != nullptr)
   {
       head_ptr_ = head_ptr_->getNext();

       // Return node to the system
       node_to_delete->setNext(nullptr);
       delete node_to_delete;

       node_to_delete = head_ptr_;
   } // end while
   // head_ptr_ is nullptr; node_to_delete is nullptr

   item_count_ = 0;
} // end clear


template
int LinkedBag::getFrequencyOf(const T& an_entry) const
{
   int frequency = 0;
   int counter = 0;
   Node* cur_ptr = head_ptr_;
   while ((cur_ptr != nullptr) && (counter < item_count_))
   {
       if (an_entry == cur_ptr->getItem())
       {
           frequency++;
       } // end if

       counter++;
       cur_ptr = cur_ptr->getNext();
   } // end while

   return frequency;
} // end getFrequencyOf

template
bool LinkedBag::contains(const T& an_entry) const
{
   return (getPointerTo(an_entry) != nullptr);
} // end contains


// private


/**
@return Returns either a pointer to the node containing a given entry
or the null pointer if the entry is not in the bag.
*/
template
Node* LinkedBag::getPointerTo(const T& an_entry) const
{
   bool found = false;
   Node* cur_ptr = head_ptr_;

   while (!found && (cur_ptr != nullptr))
   {
       if (an_entry == cur_ptr->getItem())
           found = true;
       else
           cur_ptr = cur_ptr->getNext();
   } // end while

   return cur_ptr;
} // end getPointerTo

-----------------------------------------------

0 0
Add a comment Improve this question Transcribed image text
Answer #1

LinkedBag.cpp

#include "LinkedBag.hpp"
#include "Node.hpp"
#include <cstddef>
#include <iostream>

template <class T>
LinkedBag<T>::LinkedBag() : head_ptr_(nullptr), item_count_(0)
{
} // end default constructor

template <class T>
LinkedBag<T>::LinkedBag(const LinkedBag<T> &a_bag)
{
item_count_ = a_bag.item_count_;
Node<T> *orig_chain_ptr = a_bag.head_ptr_; // Points to nodes in original chain

if (orig_chain_ptr == nullptr)
    head_ptr_ = nullptr; // Original bag is empty
else
{
    // Copy first node
    head_ptr_ = new Node<T>();
    head_ptr_->setItem(orig_chain_ptr->getItem());

    // Copy remaining nodes
    Node<T> *new_chain_ptr = head_ptr_;         // Points to last node in new chain
    orig_chain_ptr = orig_chain_ptr->getNext(); // Advance original-chain pointer

    while (orig_chain_ptr != nullptr)
    {
      // Get next item from original chain
      T next_item = orig_chain_ptr->getItem();

      // Create a new node containing the next item
      Node<T> *new_node_ptr = new Node<T>(next_item);

      // Link new node to end of new chain
      new_chain_ptr->setNext(new_node_ptr);

      // Advance pointer to new last node
      new_chain_ptr = new_chain_ptr->getNext();

      // Advance original-chain pointer
      orig_chain_ptr = orig_chain_ptr->getNext();
    } // end while

    new_chain_ptr->setNext(nullptr); // Flag end of chain
}                                  // end if
} // end copy constructor

template <class T>
LinkedBag<T>::~LinkedBag()
{
clear();
} // end destructor

template <class T>
bool LinkedBag<T>::isEmpty() const
{
return item_count_ == 0;
} // end isEmpty

template <class T>
int LinkedBag<T>::getCurrentSize() const
{
return item_count_;
} // end getCurrentSize

template <class T>
bool LinkedBag<T>::add(const T &new_entry)
{
// Add to beginning of chain: new node references rest of chain;
// (head_ptr_ is null if chain is empty)
Node<T> *next_node_ptr = new Node<T>();
next_node_ptr->setItem(new_entry);
next_node_ptr->setNext(head_ptr_); // New node points to chain
                                     //   Node<T>* next_node_ptr = new Node<T>(new_entry, head_ptr_); // alternate code

head_ptr_ = next_node_ptr; // New node is now first node
item_count_++;

return true;
} // end add

template <class T>
std::vector<T> LinkedBag<T>::toVector() const
{
std::vector<T> bag_contents;
Node<T> *temp_cur_ptr = head_ptr_;
while ((temp_cur_ptr != nullptr))
{
    bag_contents.push_back(temp_cur_ptr->getItem());
    temp_cur_ptr = temp_cur_ptr->getNext();
} // end while

return bag_contents;
} // end toVector

template <class T>
bool LinkedBag<T>::remove(const T &an_entry)
{
Node<T> *entry_node_ptr = getPointerTo(an_entry);
bool can_remove = !isEmpty() && (entry_node_ptr != nullptr);
if (can_remove)
{
    // Copy data from first node to located node
    entry_node_ptr->setItem(head_ptr_->getItem());

    // Delete first node
    Node<T> *node_to_delete = head_ptr_;
    head_ptr_ = head_ptr_->getNext();

    // Return node to the system
    node_to_delete->setNext(nullptr);
    delete node_to_delete;
    node_to_delete = nullptr;

    item_count_--;
} // end if

return can_remove;
} // end remove

template <class T>
void LinkedBag<T>::clear()
{
Node<T> *node_to_delete = head_ptr_;
while (head_ptr_ != nullptr)
{
    head_ptr_ = head_ptr_->getNext();

    // Return node to the system
    node_to_delete->setNext(nullptr);
    delete node_to_delete;

    node_to_delete = head_ptr_;
} // end while
// head_ptr_ is nullptr; node_to_delete is nullptr

item_count_ = 0;
} // end clear

template <class T>
int LinkedBag<T>::getFrequencyOf(const T &an_entry) const
{
int frequency = 0;
int counter = 0;
Node<T> *temp_cur_ptr = head_ptr_;
while ((temp_cur_ptr != nullptr) && (counter < item_count_))
{
    if (an_entry == temp_cur_ptr->getItem())
    {
      frequency++;
    } // end if

    counter++;
    temp_cur_ptr = temp_cur_ptr->getNext();
} // end while

return frequency;
} // end getFrequencyOf

template <class T>
bool LinkedBag<T>::contains(const T &an_entry) const
{
return (getPointerTo(an_entry) != nullptr);
} // end contains

template <class T>
LinkedBag<T> LinkedBag<T>::bagUnion(const LinkedBag<T> &a_bag) const
{
if (a_bag.isEmpty())
{
    return *this;
}

if (this->isEmpty())
{
    return a_bag;
}

LinkedBag<T> temp;

Node<T> *this_cur_ptr = head_ptr_;
while ((this_cur_ptr != nullptr))
{
    temp.add(this_cur_ptr->getItem());
    this_cur_ptr = this_cur_ptr->getNext();
} // end while for this bag

Node<T> *bag_cur_ptr = a_bag.head_ptr_;
while ((bag_cur_ptr != nullptr))
{
    temp.add(bag_cur_ptr->getItem());
    bag_cur_ptr = bag_cur_ptr->getNext();
} // end while for a_bag

// Print contents of a LinkedBag

Node<T> *temp_cur_ptr = temp.head_ptr_;
while ((temp_cur_ptr != nullptr))
{
    std::cout << temp_cur_ptr->getItem() << std::endl;
    temp_cur_ptr = temp_cur_ptr->getNext();
} // end while for temp bag

// End print contents

return temp;
} // end bagUnion

template <class T>
LinkedBag<T> LinkedBag<T>::bagIntersectionNoDuplicates(const LinkedBag<T> &a_bag) const
{
LinkedBag<T> temp;

if (a_bag.isEmpty() || this->isEmpty())
{
    return temp;
}

Node<T> *this_cur_ptr = head_ptr_;
while ((this_cur_ptr != nullptr))
{
    T curThisItem = this_cur_ptr->getItem();
    if (!temp.contains(curThisItem) && a_bag.contains(curThisItem))
    {
      temp.add(curThisItem);
    }
    this_cur_ptr = this_cur_ptr->getNext();
} // end while for this bag

Node<T> *bag_cur_ptr = a_bag.head_ptr_;
while ((bag_cur_ptr != nullptr))
{
    T curBagItem = bag_cur_ptr->getItem();
    if (!temp.contains(curBagItem) && this->contains(curBagItem))
    {
      temp.add(curBagItem);
    }
    bag_cur_ptr = bag_cur_ptr->getNext();
} // end while for a_bag

// Print contents of a LinkedBag

Node<T> *temp_cur_ptr = temp.head_ptr_;
while ((temp_cur_ptr != nullptr))
{
    std::cout << temp_cur_ptr->getItem() << std::endl;
    temp_cur_ptr = temp_cur_ptr->getNext();
} // end while for temp bag

// End print contents

return temp;
} // end bagIntersectionNoDuplicates

template <class T>
LinkedBag<T> LinkedBag<T>::bagDifference(const LinkedBag<T> &a_bag) const
{
LinkedBag<T> temp;

Node<T> *this_cur_ptr = head_ptr_;
while ((this_cur_ptr != nullptr))
{
    T curThisItem = this_cur_ptr->getItem();
    if (!temp.contains(curThisItem) && !a_bag.contains(curThisItem))
    {
      temp.add(curThisItem);
    }
    this_cur_ptr = this_cur_ptr->getNext();
} // end while for this bag

Node<T> *bag_cur_ptr = a_bag.head_ptr_;
while ((bag_cur_ptr != nullptr))
{
    T curBagItem = bag_cur_ptr->getItem();
    if (!temp.contains(curBagItem) && !this->contains(curBagItem))
    {
      temp.add(curBagItem);
    }
    bag_cur_ptr = bag_cur_ptr->getNext();
} // end while for a_bag

// Print contents of a LinkedBag

Node<T> *temp_cur_ptr = temp.head_ptr_;
while ((temp_cur_ptr != nullptr))
{
    std::cout << temp_cur_ptr->getItem() << std::endl;
    temp_cur_ptr = temp_cur_ptr->getNext();
} // end while for temp bag

// End print contents

return temp;
} // end bagDifference

template <class T>
void LinkedBag<T>::operator=(const LinkedBag<T> &a_bag)
{
this->clear();
if (a_bag.isEmpty())
{
    return;
}
Node<T> *bag_cur_ptr = a_bag.head_ptr_;
while ((bag_cur_ptr != nullptr))
{
    T curBagItem = bag_cur_ptr->getItem();
    this->add(curBagItem);
    bag_cur_ptr = bag_cur_ptr->getNext();
} // end while for a_bag

// Print contents of a LinkedBag

Node<T> *temp_cur_ptr = this->head_ptr_;
while ((temp_cur_ptr != nullptr))
{
    std::cout << temp_cur_ptr->getItem() << std::endl;
    temp_cur_ptr = temp_cur_ptr->getNext();
} // end while for temp bag

// End print contents

} // end = overload

template <class T>
bool LinkedBag<T>::addToEnd(const T &new_entry)
{
// Create a new node containing the next item
Node<T> *new_node_ptr = new Node<T>(new_entry);

Node<T> *this_cur_ptr = head_ptr_;

if (this->isEmpty())
{
    head_ptr_ = new_node_ptr;
}

while ((this_cur_ptr != nullptr))
{
    // As long as this isn't the last node...
    if (this_cur_ptr->getNext() != nullptr)
    {
      //std::cout << "getNext != nullptr" << std::endl;
      this_cur_ptr = this_cur_ptr->getNext();
    }
    // Break once the last node is reached (next_ == nullptr)
    else
    {
      break;
    }

} // end while for this bag

this_cur_ptr->setNext(new_node_ptr);

// Print contents of a LinkedBag

Node<T> *temp_cur_ptr = this->head_ptr_;
while ((temp_cur_ptr != nullptr))
{
    std::cout << temp_cur_ptr->getItem() << std::endl;
    temp_cur_ptr = temp_cur_ptr->getNext();
} // end while for temp bag

// End print contents
return (this_cur_ptr->getItem() == new_entry);

} // end addToEnd

template <class T>
bool LinkedBag<T>::removeRetainOrder(const T &an_entry)
{
bool found = false;
bool firstEntry = false;
Node<T> *this_cur_ptr = head_ptr_;

if (this->isEmpty())
{
    return false;
}

while ((this_cur_ptr != nullptr))
{
    // As long as this isn't the last node...
    if (this_cur_ptr->getNext() != nullptr)
    {
      // If the first entry is the desired entry
      if (this_cur_ptr->getItem() == an_entry)
      {
        std::cout << "First entry!" << std::endl;
        std::cout << "The item: " << this_cur_ptr->getItem() << " | an_entry: " << an_entry << std::endl;
        found = true;
        firstEntry = true;
        break;
      }
      // Else if the next node's item is the desired entry...
      else if (this_cur_ptr->getNext()->getItem() == an_entry)
      {
        std::cout << "The item: " << this_cur_ptr->getNext()->getItem() << " | an_entry: " << an_entry << std::endl;
        found = true;
        break;
      }
      // Else keep moving the pointer along.
      else
      {
        this_cur_ptr = this_cur_ptr->getNext();
      }
    }
    // Else if the while loop has reached the last node, then the entry hasn't been found.
    else
    {
      return false;
    }
} // end while for this bag

if (found && firstEntry)
{
    Node<T> *node_to_delete = this_cur_ptr;
    head_ptr_ = node_to_delete->getNext();
    node_to_delete->setNext(nullptr);
    delete node_to_delete;

    // Print contents of a LinkedBag

    Node<T> *temp_cur_ptr = this->head_ptr_;
    while ((temp_cur_ptr != nullptr))
    {
      std::cout << temp_cur_ptr->getItem() << std::endl;
      temp_cur_ptr = temp_cur_ptr->getNext();
    } // end while for temp bag

    // End print contents

    return true;
}
else if (found)
{
    Node<T> *node_to_delete = this_cur_ptr->getNext();
    this_cur_ptr->setNext(node_to_delete->getNext());
    node_to_delete->setNext(nullptr);
    delete node_to_delete;

    // Print contents of a LinkedBag

    Node<T> *temp_cur_ptr = this->head_ptr_;
    while ((temp_cur_ptr != nullptr))
    {
      std::cout << temp_cur_ptr->getItem() << std::endl;
      temp_cur_ptr = temp_cur_ptr->getNext();
    } // end while for temp bag

    // End print contents

    return true;
}

} // end removeRetainOrder

// private

/**
@return Returns either a pointer to the node containing a given entry
or the null pointer if the entry is not in the bag.
*/
template <class T>
Node<T> *LinkedBag<T>::getPointerTo(const T &an_entry) const
{
bool found = false;
Node<T> *temp_cur_ptr = head_ptr_;

while (!found && (temp_cur_ptr != nullptr))
{
    if (an_entry == temp_cur_ptr->getItem())
      found = true;
    else
      temp_cur_ptr = temp_cur_ptr->getNext();
} // end while

return temp_cur_ptr;
} // end getPointerTo


LinkedBag.hpp
#ifndef LINKED_BAG_
#define LINKED_BAG_

#include <vector>
#include <cstdlib>
#include <algorithm>
#include "Node.hpp"

template <class T>
class LinkedBag
{
public:
LinkedBag();
LinkedBag(const LinkedBag<T> &a_bag); // Copy constructor
~LinkedBag();                         // Destructor
int getCurrentSize() const;
bool isEmpty() const;
bool add(const T &new_entry);
bool remove(const T &an_entry);
void clear();
bool contains(const T &an_entry) const;
int getFrequencyOf(const T &an_entry) const;
std::vector<T> toVector() const;

/**
@param a_bag to be combined with the contents of this (the calling) bag
@return a new LinkedBag that contains all elements from this
bag (items_)and from a_bag. Note that it may contain duplicates
*/
LinkedBag<T> bagUnion(const LinkedBag<T> &a_bag) const;

/**
@param a_bag to be intersected with the contents of this (the calling)
bag
@return a new LinkedBag that contains the intersection of the contents
of this bag and those of the argument a_bag. This intersection does not
contain duplicates (e.g. every element occurring in BOTH bags will be
found only once in the intersection, no matter how many occurrences in
the original bags) as in set intersection A ∩ B
*/
LinkedBag<T> bagIntersectionNoDuplicates(const LinkedBag<T> &a_bag) const;

/**
@param a_bag to be subtracted from this bag
@return a new LinkedBag that contains only those items that occur in
this bag or in a_bag but not in both, and it does not contain duplicates
*/
LinkedBag<T> bagDifference(const LinkedBag<T> &a_bag) const;

/**
@param a_bag whose contents are to be copied to this (the calling) bag
@post this (the calling) bag has same contents as a_bag
*/
void operator=(const LinkedBag<T> &a_bag);

/**
@param new_entry to be added to the bag
@post new_entry is added at the end of the chain preserving the order of
items in the bag
@return true if added successfully, false otherwise
*/
bool addToEnd(const T &new_entry);

/**
@param an_entry to be removed from the bag
@post the first occurrence of an_entry starting from the head node is
removed from the chain preserving the order of the remaining items in
the bag
@return true if removed successfully, false otherwise
*/
bool removeRetainOrder(const T &an_entry);

private:
Node<T> *head_ptr_; // Pointer to first node
int item_count_;    // Current count of bag items

/**
@return Returns either a pointer to the node containing a given entry
or the null pointer if the entry is not in the bag.
*/
Node<T> *getPointerTo(const T &target) const;

}; // end LinkedBag

#include "LinkedBag.cpp"
#endif


Node.cpp

#include "Node.hpp"
//#include <cstddef>

template<class T>
Node<T>::Node() : next_(nullptr)
{
} // end default constructor

template<class T>
Node<T>::Node(const T& an_item) : item_(an_item), next_(nullptr)
{
} // end constructor

template<class T>
Node<T>::Node(const T& an_item, Node<T>* next_node_ptr) :
                item_(an_item), next_(next_node_ptr)
{
} // end constructor

template<class T>
void Node<T>::setItem(const T& an_item)
{
   item_ = an_item;
} // end setItem

template<class T>
void Node<T>::setNext(Node<T>* next_node_ptr)
{
   next_ = next_node_ptr;
} // end setNext

template<class T>
T Node<T>::getItem() const
{
   return item_;
} // end getItem

template<class T>
Node<T>* Node<T>::getNext() const
{
   return next_;
} // end getNext


Node.hpp
#ifndef NODE_
#define NODE_

template<class T>
class Node
{
public:
   Node();
   Node(const T& an_item_);
   Node(const T& an_item, Node<T>* next_node_ptr);
   void setItem(const T& an_item);
   void setNext(Node<T>* next_node_ptr);
   T getItem() const ;
   Node<T>* getNext() const ;


private:
    T item_; // A data item_
    Node<T>* next_; // Pointer to next_ node
}; // end Node

#include "Node.cpp"
#endif

main.cpp

#include <iostream>
#include "LinkedBag.hpp"
#include "Node.hpp"

int main()
{
std::cout << "Starting main" << std::endl;

// Test bagUnion
std::cout << "Testing bagUnion" << std::endl;

LinkedBag<int> bag1;
bag1.add(0);
bag1.add(1);
bag1.add(2);
bag1.add(3);
bag1.add(4);

LinkedBag<int> bag2;
bag2.add(5);
bag2.add(6);
bag2.add(7);
bag2.add(8);
bag2.add(9);

bag1.bagUnion(bag2);
// New bag should have 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 in any order

std::cout << "Ending bagUnion test" << std::endl;
// End bagUnion test

// Test bagIntersectionNoDuplicates
std::cout << "Testing bagIntersectionNoDuplicates" << std::endl;

LinkedBag<int> bag3;
LinkedBag<int> bag4;

bag3.add(0);
bag3.add(1);
bag3.add(2);
bag3.add(7);
bag3.add(4);

bag4.add(0);
bag4.add(4);
bag4.add(7);
bag4.add(8);

LinkedBag<int> intersectedBagsNoDupes = bag3.bagIntersectionNoDuplicates(bag4);
// New bag should have 0, 4, 7 in any order

std::cout << "Ending bagIntersectionNoDuplicates test" << std::endl;
// End bagIntersectionNoDuplicates test

// Test bagDifference
std::cout << "Testing bagDifference" << std::endl;

LinkedBag<int> bag5;
LinkedBag<int> bag6;

bag5.add(0);
bag5.add(1);
bag5.add(2);
bag5.add(7);
bag5.add(4);

bag6.add(0);
bag6.add(4);
bag6.add(7);
bag6.add(8);

LinkedBag<int> differencedBags = bag5.bagDifference(bag6);
// New bag should have 1, 2, 8 in any order

std::cout << "Ending bagDifference test" << std::endl;
// End bagDifference test

// Test overloading of = operator
std::cout << "Testing overloading of = operator" << std::endl;

LinkedBag<int> bag7;
LinkedBag<int> bag8;

bag7.add(0);
bag7.add(1);
bag7.add(2);
bag7.add(7);
bag7.add(4);
bag7.add(0);
bag7.add(0);
bag7.add(8);
bag7.add(8);

bag8.add(0);
bag8.add(0);
bag8.add(0);
bag8.add(4);
bag8.add(7);
bag8.add(8);
bag8.add(8);
bag8.add(8);

bag7 = bag8;
// New bag should have 0, 0, 0, 4, 7, 8, 8, 8 in any order

std::cout << "Ending overloading of = operator test" << std::endl;
// End overloading of = operator test

// Test addToEnd
std::cout << "Testing addToEnd" << std::endl;

LinkedBag<int> bag9;

bag9.add(1);
bag9.add(2);
bag9.add(3);
bag9.add(4);
bag9.add(5);
bag9.add(6);
bag9.add(7);
bag9.add(8);

bag9.addToEnd(0);
// New bag should have 8, 7, 6, 5, 4, 3, 2, 1, 0 in this order

std::cout << "Ending addToEnd test" << std::endl;
// End addToEnd test

// Test removeRetainOrder
std::cout << "Testing removeRetainOrder" << std::endl;

LinkedBag<int> bag10;

bag10.add(1);
bag10.add(2);
bag10.add(3);
bag10.add(4);
bag10.add(5);
bag10.add(6);
bag10.add(7);
bag10.add(8);

bag10.removeRetainOrder(4);
// New bag should have 8, 7, 6, 5, 3, 2, 1, 0 in this order
bag10.removeRetainOrder(8);
// New bag should have 7, 6, 5, 3, 2, 1, 0 in this order
bag10.removeRetainOrder(0); // Good, no error even though 0 is not in the bag
// New bag should have 7, 6, 5, 3, 2, 1 in this order
bag10.removeRetainOrder(6);
// New bag should have 7, 5, 3, 2, 1 in this order
bag10.removeRetainOrder(1);
// New bag should have 7, 5, 3, 2 in this order

std::cout << "Ending removeRetainOrder test" << std::endl;
// End removeRetainOrder test

std::cout << "Ending main" << std::endl;
return 0;
}

Add a comment
Know the answer?
Add Answer to:
This is a c++ class utilizing class templates and linked lists and Nodes. I need to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a c++ class utilizing class templates and linked lists. I need to implement the...

    This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...

  • C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS...

    C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)" The objective of the assignment is to revise the public method add in class template LinkedBag so that the new node is inserted at the end of the linked chain instead of at the beginning. This is the code I have: linkedBag-driver.cpp BagInterface.hpp LinkedBag.hpp Node.hpp int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...

  • / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false,...

    / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false, bool predator=false); std::string getName() const; bool isDomestic() const; bool isPredator() const; void setName(std::string); void setDomestic(); void setPredator(); protected: // protected so that derived class can directly access them std::string name_; bool domestic_; bool predator_; }; #endif /* ANIMAL_H_ */ //end of Animal.h // /////////////////////////////////////////////////////////////////Animal.cpp #include "Animal.h" Animal::Animal(): name_(""),domestic_(false), predator_(false){ } Animal::Animal(std::string name, bool domestic, bool predator): name_(name),domestic_(domestic), predator_(predator) { } std::string Animal::getName() const{ return...

  • C++: I need implement this code using Double Linked List using the cosiderations 1. head point...

    C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes:    -head: first node in the list    -tail: last node in the list #include "circDLLNode.h" template class { public:    //Default constructor: creates an empty list    ();    //Destructor: deallocate memory    ~();  ...

  • can someone please fix this error in the stack file at line 18 ( class Stack2:public...

    can someone please fix this error in the stack file at line 18 ( class Stack2:public Stack { ) this is thew error ----> expected class name before '{' token Queue.h --> #include "Stack.hpp" template <typename ITEM> class Queue { private: ITEM item; Stack <ITEM> * s1= new Stack<ITEM>; Stack <ITEM> * s2= new Stack<ITEM>; public: Queue(){}; bool enqueue (ITEM item); bool dequeue (ITEM &item); bool check = true; ~Queue(); }; //this is the class which contains the functions of...

  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node...

    I need help implemeting the remove_repetitions() Here is a brief outline of an algorithm: A node pointer p steps through the bag For each Item, define a new pointer q equal to p While the q is not the last Item in the bag If the next Item has data equal to the data in p, remove the next Item Otherwise move q to the next Item in the bag. I also need help creating a test program _____________________________________________________________________________________________________________________________________________________ #ifndef...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT