My assignment is to make and ADT for a Set that uses linked lists. I have working methods for the ADT except for one called the "difference." This method should return a new set that contains the strings that the called upon Set and the argument Set don't have in common.
Ex.) Set 1 contains (one, two, three)
Set 2 contains (two, three)
The function should return a Set containing (one)
Any help would be greatly appreciated :) Thanks much!
LinkedSet header:
*
* @course CS1521
* @section 8
* @term Spring 2020
*
* Header file for a linked implementation of the ADT set.
*
* @date 20 Feb 2020*/
#ifndef LINKED_SET_
#define LINKED_SET_
#include <vector>
#include "SetInterface.h"
#include "Node.h"
/** @class LinkedBag LinkedBag.h "LinkedBag.h"
*
* Specification of a pointer-based ADT bag. */
template <typename ItemType>
class LinkedSet : public SetInterface<ItemType> {
private:
using NodePtr = Node<ItemType>*;
/** Pointer to first node. */
NodePtr headPtr = nullptr;
/** Number of items in this bag. */
int itemCount = 0;
/** Gets a pointer to the node containing the target in this bag.
*
* @pre None.
*
* @post None.
*
* @param target The ItemType value to find.
*
* @return A pointer to the node that contains the given target or
* nullptr if the bag does not contain the target. */
NodePtr getPointerTo(const ItemType& target) const;
public:
/** Default constructor. */
LinkedSet() = default;
/** Copy constructor. */
LinkedSet(const LinkedSet<ItemType>& aBag);
virtual ~LinkedSet();
virtual int getCurrentSize() const;
virtual bool isEmpty() const;
virtual bool add(const ItemType& newEntry);
virtual bool remove(const ItemType& anEntry);
virtual void clear();
virtual int getFrequencyOf(const ItemType& anEntry) const;
virtual bool contains(const ItemType& anEntry) const;
virtual LinkedSet<ItemType> difference(LinkedSet<ItemType>& set) const;
virtual std::vector<ItemType> toVector() const;
};
#include "LinkedSet.cpp"
#endif
Linked Set C++:
*
* @course CS1521
* @section 8
* @term Spring 2020
*
* Implementation file for the class LinkedSet.
*
* @date 20 Feb 2020*/
#include <vector>
template <typename ItemType>
LinkedSet<ItemType>::LinkedSet(const LinkedSet<ItemType>& aSet) {
itemCount = aSet.itemCount;
// If aSet.headPtr is not pointing at a node:
if (!aSet.headPtr) { // aSet.headPtr == nullptr (!nullptr == true)
headPtr = nullptr;
}
else {
NodePtr origChainPtr(aSet.headPtr);
headPtr = new Node<ItemType>(origChainPtr->getItem() );
NodePtr newChainPtr(headPtr);
origChainPtr = origChainPtr->getNext();
while (origChainPtr) { // while (origChainPtr != nullptr) {
newChainPtr->setNext(new Node<ItemType>(origChainPtr->getItem()) );
newChainPtr = newChainPtr->getNext();
origChainPtr = origChainPtr->getNext();
}
}
}
template <typename ItemType>
LinkedSet<ItemType>::~LinkedSet() {
clear();
}
template <typename ItemType>
int LinkedSet<ItemType>::getCurrentSize() const {
return itemCount;
}
template <typename ItemType>
bool LinkedSet<ItemType>::isEmpty() const {
return itemCount == 0;
}
template <typename ItemType>
bool LinkedSet<ItemType>::add(const ItemType& newEntry) {
if (getPointerTo(newEntry) == nullptr)
{
headPtr = new Node<ItemType>(newEntry, headPtr);
++itemCount;
return true;
}
}
template <typename ItemType>
bool LinkedSet<ItemType>::remove(const ItemType& anEntry) {
NodePtr entryNodePtr(getPointerTo(anEntry) );
// If entryNodePtr is pointing at a node:
if (entryNodePtr) { // entryNodePtr != nullptr (nullptr == false)
entryNodePtr->setItem(headPtr->getItem() );
NodePtr nodeToDeletePtr(headPtr);
headPtr = headPtr->getNext();
delete nodeToDeletePtr;
--itemCount;
}
return static_cast<bool>(entryNodePtr);
}
template <typename ItemType>
Node<ItemType>* LinkedSet<ItemType>::getPointerTo(const ItemType& target) const {
NodePtr curPtr(headPtr);
while (curPtr) { // curPtr != nullptr
if (curPtr->getItem() == target) {
return curPtr;
}
curPtr = curPtr->getNext();
}
return curPtr; // nullptr
}
template <typename ItemType>
void LinkedSet<ItemType>::clear() {
NodePtr nodeToDeletePtr(headPtr);
while (headPtr) { // headPtr != nullptr
headPtr = headPtr->getNext();
delete nodeToDeletePtr;
nodeToDeletePtr = headPtr;
}
// Note that headPtr == nodeToDeletePtr == nullptr
itemCount = 0;
}
template <typename ItemType>
bool LinkedSet<ItemType>::contains(const ItemType& anEntry) const {
return static_cast<bool>(getPointerTo(anEntry) ); // ... != nullptr;
}
template <typename ItemType>
int LinkedSet<ItemType>::getFrequencyOf(const ItemType& anEntry) const {
int frequency(0);
NodePtr curPtr(headPtr);
while (curPtr)
{ // != nullptr
if (curPtr->getItem() == anEntry) {
++frequency;
}
curPtr = curPtr->getNext();
}
return frequency;
}
template <typename ItemType>
LinkedSet<ItemType> LinkedSet<ItemType>::difference(LinkedSet<ItemType>& set) const{
LinkedSet<ItemType> newSet;
NodePtr curPtr(headPtr);
while (curPtr)
{
if (set.getPointerTo(curPtr) != getPointerTo(curPtr))
{
newSet.add(curPtr);
}
curPtr = curPtr->getNext();
}
return newSet;
}
template <typename ItemType>
std::vector<ItemType> LinkedSet<ItemType>::toVector() const {
std::vector<ItemType> SetContents;
NodePtr curPtr(headPtr);
while (curPtr) { // != nullptr
SetContents.push_back(curPtr->getItem() );
curPtr = curPtr->getNext();
}
return SetContents;
}
Aucun commentaire:
Enregistrer un commentaire