Question: In C++ please: Compile the following code and run it. Please show the output for a thumbsup! 1) The header file for the template class
In C++ please: Compile the following code and run it. Please show the output for a thumbsup!
1) The header file for the template class Node
/** @file Node.h */
#ifndef _NODE
#define _NODE
template< class ItemType>
class Node
{
private:
ItemType item; // A data item
Node
public:
Node();
Node(const ItemType& anItem);
Node(const ItemType& anItem, Node
void setItem(const ItemType& anItem);
void setNext(Node
ItemType getItem() const;
Node
}; // end Node
#include "Node.cpp"
#endif
2) The implementation fi le for the class Node
/** @file Node.cpp */
#include "Node.h"
#include
template< class ItemType>
Node
{
} // end default constructor
template< class ItemType>
Node
{
} // end constructor
template< class ItemType>
Node
item(anItem), next(nextNodePtr)
{
} // end constructor
template< class ItemType>
void Node
{
item = anItem;
} // end setItem
template< class ItemType>
void Node
{
next = nextNodePtr;
} // end setNext
template< class ItemType>
ItemType Node
{
return item;
} // end getItem
template< class ItemType>
Node
{
return next;
} // end getNext
3)
/** ADT bag: Link-based implementation.
@file LinkedBag.h */
#ifndef _LINKED_BAG
#define _LINKED_BAG
#include "BagInterface.h"
#include "Node.h"
template< class ItemType>
class LinkedBag : public BagInterface
{
private:
Node
int itemCount; // Current count of bag items
// Returns either a pointer to the node containing a given entry
// or the null pointer if the entry is not in the bag.
Node
public:
LinkedBag();
LinkedBag(const LinkedBag
virtual ~LinkedBag(); // Destructor should be virtual
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
vector
}; // end LinkedBag
#include "LinkedBag.cpp"
#endif
4)
// A program that tests the core methods of classes that are derived from the
//abstract class BagInterface
#include "BagInterface.h"
#include "ArrayBag.h"
#include "LinkedBag.h"
#include
#include
#include
using namespace std;
void displayBag(BagInterface
{
cout << "The bag contains " << bagPtr->getCurrentSize()
<< " items:" << endl;
vector
bagPtr->toVector(bagItems);
int numberOfEntries = bagItems.size();
for (int i = 0; i < numberOfEntries; i++)
{
cout << bagItems[i] << " ";
} // end for
cout << endl << endl;
} // end displayBag
void bagTester(BagInterface
{
cout << "isEmpty: returns " << bagPtr->isEmpty()
<< "; should be 1 (true)" << endl;
string items[] = { "one", "two", "three", "four", "five", "one" };
cout << "Add 6 items to the bag: " << endl;
for (int i = 0; i < 6; i++)
{
bagPtr->add(items[i]);
} // end for
displayBag(bagPtr);
cout << "isEmpty: returns " << bagPtr->isEmpty()
<< "; should be 0 (false)" << endl;
cout << "getCurrentSize returns : " << bagPtr->getCurrentSize()
<< "; should be 6" << endl;
cout << "Try to add another entry: add(\"extra\") returns "
<< bagPtr->add("extra") << endl;
} // end bagTester
int main()
{
BagInterface
char userChoice;
cout << "Enter 'A' to test the array-based implementation ";
<< " or 'L' to test the link-based implementation: ";
cin >> userChoice;
if (toupper(userChoice) == 'A')
{
bagPtr = new ArrayBag
cout << "Testing the Array-Based Bag:" << endl;
}
else
{
bagPtr = new LinkedBag
cout << "Testing the Link-Based Bag:" << endl;
} // end if
cout << "The initial bag is empty." << endl;
bagTester(bagPtr);
delete bagPtr;
bagPtr = nullptr;
cout << "All done!" << endl;
return 0;
} // end main
Additional info:
//Constructor for linked bag
template< class ItemType>
LinkedBag
{
itemCount = aBag->itemCount;
Node
if (origChainPtr == nullptr)
headPtr = nullptr; // Original bag is empty; so is copy
else
{
// Copy first node
headPtr = new Node
headPtr->setItem(origChainPtr->getItem());
// Copy remaining nodes
Node
while (origPtr != nullptr)
{
origChainPtr = origChainPtr->getNext(); // Advance pointer
// Get next item from original chain
ItemType nextItem = origChainPtr->getItem();
// Create a new node containing the next item
Node
// Link new node to end of new chain newChainPtr->setNext(newNodePtr); // Advance pointer to new last node newChainPtr = newChainPtr->getNext(); } // end while newChainPtr->setNext(nullptr); // Flag end of new chain } // end if } // end copy constructor
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
