Question

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 on type T
@param position contained in the sorted/increasing (first <= position <=
last) sublist to be generated
@return a sublist containing the item at position consisting of sorted/
increasing items (first <= position <= last)
*/
List<T> scanSublist(size_t position);

HERE IS THE CODE

Node.hpp

#ifndef NODE_
#define NODE_

template<class ItemType>
class Node
{

public:
Node();
Node(const ItemType& an_item);
Node(const ItemType& an_item, Node<ItemType>* next_node_ptr);
void setItem(const ItemType& an_item);
void setNext(Node<ItemType>* next_node_ptr);
void setPrevious(Node<ItemType>* previous_node_ptr);
ItemType getItem() const ;
Node<ItemType>* getNext() const ;
Node<ItemType>* getPrevious() const ;

private:
ItemType item_; // A data item
Node<ItemType>* next_; // Pointer to next node
Node<ItemType>* previous_; // Pointer to next node
}; // end Node

#include "Node.cpp"
#endif

Node.cpp

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

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

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

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

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

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

template<class ItemType>
void Node<ItemType>::setPrevious(Node<ItemType>* previous_node_ptr)
{
previous_ = previous_node_ptr;
} // end setPrevious

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

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


template<class ItemType>
Node<ItemType>* Node<ItemType>::getPrevious() const
{
return previous_;
} // end getPrevious

List.hpp

#ifndef LIST_H_
#define LIST_H_
#include <iostream>
#include "Node.hpp"

template<class T>
class List
{

public:
List(); // constructor
List(const List<T>& a_list); // copy constructor
~List(); // destructor

/**@return true if list is empty - item_count_ == 0 */
bool isEmpty() const;

/**@return the number of items in the list - item_count_ */
size_t getLength() const;


/**
@param position indicating point of insertion
@param new_element to be inserted in list
@post new_element is added at position in list (before the node previously at that position)
@return true always - it always inserts, if position > item_count_ it inserts at end of list */
bool insert(size_t position, const T& new_element);

/**
@param position indicating point of deletion
@post node at position is deleted, if any. List order is retains
@return true if ther eis a node at position to be deleted, false otherwise */
bool remove(size_t position);

/**
@pre assumes there is an item at position - NO ERROR HANDLING
@param position of item to be retrieved
@return the item at position in list if there is one, otherwise it returns a dummy UNITIALIZED object of type T -- temporary suboptimal solution in place of error handling to be discussed later in the course */
T getItem(size_t position) const;

/**@post the list is empty and item_count_ == 0*/
void clear();


//*** PROJECT-SPECIFIC METHODS ***//

/**
@pre assumes std::cout << is defined for objects of type T (can be sent to standard output) -- This method is not general, thus not appropriate for a templated class, it is provided for project debugging purposes
@post traverses the list and prints (std::cout) every item in the list*/
void traverse();

private:
Node<T>* first_; // Pointer to first node
Node<T>* last_; // Pointer to last node
size_t item_count_; // number of items in the list

//if position > item_count_ returns nullptr
Node<T>* getPointerTo(size_t position) const;

}; // end List

#include "List.cpp"
#endif // LIST_H_

List.cpp

#include "List.hpp"

template<class T>
List<T>::List(): item_count_(0), first_(nullptr), last_(nullptr){} // constructor



//copy constructor
template<class T>
List<T>::List(const List<T>& a_list)
{
    item_count_ = a_list.item_count_;
    Node<T>* orig_chain_ptr = a_list.first_;  // Points to nodes in original chain

    if (orig_chain_ptr == nullptr)
    {// Original chain is empty
        first_ = nullptr;
        last_ = nullptr;
    }
    else
    {
        // Copy first node
        first_ = new Node<T>();
        first_->setPrevious(nullptr);
        first_->setItem(orig_chain_ptr->getItem());

        // Copy remaining nodes
        Node<T>* new_chain_ptr = first_;                          // 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);
            new_node_ptr->setPrevious(new_chain_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

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


// destructor

template<class T>
List<T>::~List(){ clear();}

/**@return true if list is empty - item_count_ == 0 */
template<class T>
bool List<T>::isEmpty() const{ return (item_count_ == 0);}


 /**@return the number of items in the list - item_count_ */
template<class T>
size_t List<T>::getLength() const{return item_count_;}


/**
 @param position indicating point of insertion
 @param new_element to be inserted in list
 @post new_element is added at position in list (before the node previously at that position)
 @return true always - it always inserts, if position > item_count_ it inserts at end of list */
template<class T>
bool List<T>::insert(size_t position, const T& new_element)
{
    // Create a new node containing the new entry and get a pointer to position
    Node<T>* new_node_ptr = new Node<T>(new_element);
    Node<T>* pos_ptr = getPointerTo(position);

    // Attach new node to chain
    if (first_ == nullptr)
    {
        //Chain is empty -  Insert first node
        new_node_ptr->setNext(nullptr);
        new_node_ptr->setPrevious(nullptr);
        first_ = new_node_ptr;
        last_ = new_node_ptr;

    }
    else if (pos_ptr == first_)
    {
        // Insert new node at beginning of list
        new_node_ptr->setNext(first_);
        new_node_ptr->setPrevious(nullptr);
        first_->setPrevious(new_node_ptr);
        first_ = new_node_ptr;

    }
    else if (pos_ptr == nullptr)
    {
        //insert at end of list
        new_node_ptr->setNext(nullptr);
        new_node_ptr->setPrevious(last_);
        last_->setNext(new_node_ptr);
        last_ = new_node_ptr;

    }
    else
    {
        // Insert new node before node to which pos_ptr points
        new_node_ptr->setNext(pos_ptr);
        new_node_ptr->setPrevious(pos_ptr->getPrevious());
        pos_ptr->getPrevious()->setNext(new_node_ptr);
        pos_ptr->setPrevious(new_node_ptr);

    }  // end if

    item_count_++;  // Increase count of entries
    return true;    //It will always insert, if pos_ptr is nullptr it will insert at end
}//end insert


/**
 @param position indicating point of deletion
 @post node at position is deleted, if any. List order is retains
 @return true if ther eis a node at position to be deleted, false otherwise */
template<class T>
bool List<T>::remove(size_t position)
{
    //get pointer to position
    Node<T>* pos_ptr = getPointerTo(position);

    if(pos_ptr == nullptr)
        return false;
    else
    {
        // Remove node from chain

        if (pos_ptr == first_)
        {
            // Remove first node
            first_ = pos_ptr->getNext();
            first_->setPrevious(nullptr);

            // Return node to the system
            pos_ptr->setNext(nullptr);
            delete pos_ptr;
            pos_ptr = nullptr;
        }
        else if (pos_ptr == last_)
        {
            //remove last node
            last_ = pos_ptr->getPrevious();
            last_->setNext(nullptr);

            // Return node to the system
            pos_ptr->setPrevious(nullptr);
            delete pos_ptr;
            pos_ptr = nullptr;

        }
        else
        {
            //Remove from the middle
            pos_ptr->getPrevious()->setNext(pos_ptr->getNext());
            pos_ptr->getNext()->setPrevious(pos_ptr->getPrevious());

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

        }

        item_count_--;  // decrease count of entries
        return true;
    }

}//end remove


/**
 @pre assumes there is an item at position - NO ERROR HANDLING
 @param position of item to be retrieved
 @return the item at position in list if there is one, otherwise it returns a dummy UNITIALIZED object of type T -- temporary suboptimal solution in place of error handling to be discussed later in the course  */
template<class T>
T List<T>::getItem(size_t position) const
{
    T dummy;
    Node<T>* pos_ptr = getPointerTo(position);
    if(pos_ptr != nullptr)
        return pos_ptr->getItem();
    else
        return dummy;
}

/**@post the list is empty and item_count_ == 0*/
template<class T>
void List<T>::clear()
{
    Node<T>* node_to_delete = first_;
    while (first_ != nullptr)
    {
        first_ = first_->getNext();

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

        node_to_delete = first_;
    }  // end while
    // head_ptr_ is nullptr; node_to_delete is nullptr
    last_ = nullptr;
    item_count_ = 0;
}//end clear



//position follows classic indexing from 0 to item_count_-1
//if position > item_count it returns nullptr
template<class T>
Node<T>* List<T>::getPointerTo(size_t position) const
{

    Node<T>* find = nullptr;
    if(position < item_count_)
    {
        find = first_;
        for(size_t i = 0; i < position; ++i)
        {
            find = find->getNext();
        }
    }

    return find;
}//end getPointerTo


//**** PROJECT-SPECIFIC METHODS ***//



/**
 @pre assumes std::cout << is defined for objects of type T (can be sent to standard output) -- This method is not general, thus not appropriate for a templated class, it is provided for project debugging purposes
 @post traverses the list and prints (std::cout) every item in the list*/
template<class T>
void List<T>::traverse()
{
    for(Node<T>*  ptr = first_; ptr != nullptr; ptr = ptr->getNext())
    {
        std::cout << ptr->getItem() << " ";
    }
    std::cout << std::endl;
}

//endof list.cpp

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

#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. I need to implement the...
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 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...

  • 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...

  • Hello, this is my code and it have redefinition error, please help me to fix it...

    Hello, this is my code and it have redefinition error, please help me to fix it //doublenode.hpp #ifndef DOUBLENODE_HPP #define DOUBLENODE_HPP template<class ItemType> class DoubleNode { private: ItemType item; DoubleNode<ItemType>* next; DoubleNode<ItemType>* prev; public: DoubleNode(); DoubleNode(const ItemType& anItem); DoubleNode(const ItemType& anItem,DoubleNode<ItemType>* nextNodePtr, DoubleNode<ItemType>* previousNodePtr); void setItem(const ItemType& anItem); ItemType getItem() const; void setNext(DoubleNode<ItemType>* nextNodePtr); DoubleNode<ItemType>* getNext() const; void setprevious(DoubleNode<ItemType>* previousNodePtr); DoubleNode<ItemType>* getPrevious() const; }; #include "DoubleNode.cpp" #endif //doblenode.cpp //#ifndef DOUBLE_NODE_CPP //#define DOUBLE_NODE_CPP #include "DoubleNode.hpp" template <class ItemType> DoubleNode<ItemType>::DoubleNode():next(nullptr),prev(nullptr) { }...

  • Design and implement your own linked list class to hold a sorted list of integers in...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • 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    ~();  ...

  • 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...

  • Trace the following program. What is the output after execution? typedef double T; class PlainBox{ private:...

    Trace the following program. What is the output after execution? typedef double T; class PlainBox{ private: T item; public: PlainBox();   PlainBox(const T& theItem); void setItem(const T& itemItem); T getItem() const; friend ostream & operator<<(ostream & out, const PlainBox & theBox);   bool operator<(const PlainBox & theBox); }; PlainBox::PlainBox(){} PlainBox::PlainBox( const ItemType & theItem){ item = theItem; } void PlainBox:: setItem(const T& theItem){ item = theItem; } T PlainBox:: getItem () const { return item; } //remaining implementation is not shown source.cpp...

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