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* next; // Pointer to next node

public:

Node();

Node(const ItemType& anItem);

Node(const ItemType& anItem, Node* nextNodePtr);

void setItem(const ItemType& anItem);

void setNext(Node* nextNodePtr);

ItemType getItem() const;

Node* getNext() const;

}; // 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::Node() : next(nullptr)

{

} // end default constructor

template< class ItemType>

Node::Node(const ItemType& anItem) : item(anItem), next(nullptr)

{

} // end constructor

template< class ItemType>

Node::Node(const ItemType& anItem, Node* nextNodePtr) :

item(anItem), next(nextNodePtr)

{

} // end constructor

template< class ItemType>

void Node::setItem(const ItemType& anItem)

{

item = anItem;

} // end setItem

template< class ItemType>

void Node::setNext(Node* nextNodePtr)

{

next = nextNodePtr;

} // end setNext

template< class ItemType>

ItemType Node::getItem() const

{

return item;

} // end getItem

template< class ItemType>

Node* Node::getNext() const

{

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* headPtr; // Pointer to first 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* getPointerTo(const ItemType& target) const;

public:

LinkedBag();

LinkedBag(const LinkedBag& aBag); // Copy constructor

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 toVector() const;

}; // 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* bagPtr)

{

cout << "The bag contains " << bagPtr->getCurrentSize()

<< " items:" << endl;

vector bagItems;

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* bagPtr)

{

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* bagPtr = nullptr;

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::LinkedBag(const LinkedBag& aBag)

{

itemCount = aBag->itemCount;

Node* origChainPtr = aBag->headPtr

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* newChainPtr = headPtr; // Last-node pointer

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* newNodePtr = new Node(nextItem);

// 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!