Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

--------------------------------------------------------------------------------------------------------------------------------------------- // LinkedBag.cpp #include #include Node.h #include LinkedBag.h // // // PLEASE DO NOT CHANGE THIS FILE // // template LinkedBag::LinkedBag() : headPtr(nullptr), itemCount(0) {}

image text in transcribed

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

// LinkedBag.cpp

#include

#include "Node.h"

#include "LinkedBag.h"

//

//

// PLEASE DO NOT CHANGE THIS FILE

//

//

template

LinkedBag::LinkedBag() : headPtr(nullptr), itemCount(0) {}

template

LinkedBag::LinkedBag(const LinkedBag& aBag) {

itemCount = aBag.itemCount;

Node* origChainPtr = aBag.headPtr;

if (origChainPtr == nullptr) {

headPtr = nullptr;

}

else {

headPtr = new Node();

headPtr->setItem(origChainPtr->getItem());

Node* newChainPtr = headPtr;

origChainPtr = origChainPtr->getNext();

while (origChainPtr != nullptr)

{

ItemType nextItem = origChainPtr->getItem();

Node* newNodePtr = new Node(nextItem);

newChainPtr->setNext(newNodePtr);

newChainPtr = newChainPtr->getNext();

origChainPtr = origChainPtr->getNext();

}

newChainPtr->setNext(nullptr);

}

}

template

LinkedBag::~LinkedBag() {

clear();

}

template

bool LinkedBag::isEmpty() const {

return itemCount == 0;

}

template

int LinkedBag::getCurrentSize() const {

return itemCount;

}

template

bool LinkedBag::add(const ItemType& newEntry) {

Node* nextNodePtr = new Node();

nextNodePtr->setItem(newEntry);

nextNodePtr->setNext(headPtr);

headPtr = nextNodePtr;

itemCount++;

return true;

}

template

std::vector LinkedBag::toVector() const {

std::vector bagContents;

Node* curPtr = headPtr;

int counter = 0;

while ((curPtr != nullptr) && (counter

bagContents.push_back(curPtr->getItem());

curPtr = curPtr->getNext();

counter++;

}

return bagContents;

}

template

bool LinkedBag::remove(const ItemType& anEntry) {

Node* entryNodePtr = getPointerTo(anEntry);

bool canRemoveItem = !isEmpty() && (entryNodePtr != nullptr);

if (canRemoveItem) {

entryNodePtr->setItem(headPtr->getItem());

Node* nodeToDeletePtr = headPtr;

headPtr = headPtr->getNext();

nodeToDeletePtr->setNext(nullptr);

delete nodeToDeletePtr;

nodeToDeletePtr = nullptr;

itemCount--;

}

return canRemoveItem;

}

template

void LinkedBag::clear() {

Node* nodeToDeletePtr = headPtr;

while (headPtr != nullptr) {

headPtr = headPtr->getNext();

nodeToDeletePtr->setNext(nullptr);

delete nodeToDeletePtr;

nodeToDeletePtr = headPtr;

}

itemCount = 0;

}

template

int LinkedBag::getFrequencyOf(const ItemType& anEntry) const {

int frequency = 0;

int counter = 0;

Node* curPtr = headPtr;

while ((curPtr != nullptr) && (counter

if (anEntry == curPtr->getItem()) {

frequency++;

}

counter++;

curPtr = curPtr->getNext();

}

return frequency;

}

template

bool LinkedBag::contains(const ItemType& anEntry) const {

return (getPointerTo(anEntry) != nullptr);

}

template

Node* LinkedBag::getPointerTo(const ItemType& anEntry) const {

bool found = false;

Node* curPtr = headPtr;

while (!found && (curPtr != nullptr)) {

if (anEntry == curPtr->getItem()) {

found = true;

}

else {

curPtr = curPtr->getNext();

}

}

return curPtr;

}

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

// Include.h

//Nothing inside

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

Output

----- LINKED BAG 340 C++-----

--->>>>> Test 1 --->>>>> !add()... #-END 5-FIVE 4-FOUR 4-FOUR 3-THREE 2-TWO 1-ONE 0-ZERO #-BEGIN !Display bag: #-BEGIN 0-ZERO 1-ONE 2-TWO 3-THREE 4-FOUR 4-FOUR 5-FIVE #-END -----------> 9 item(s) total

--->>>>> Test 2 --->>>>> !removeSecondNode340()... !removeSecondNode340()... !Display bag: #-BEGIN 2-TWO 3-THREE 4-FOUR 4-FOUR 5-FIVE #-END -----------> 7 item(s) total

!removeSecondNode340()... !removeSecondNode340()... !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END -----------> 5 item(s) total

--->>>>> Test 3 --->>>>> !addEnd340()... !addEnd340()... !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR -----------> 7 item(s) total

!addEnd340()... !addEnd340()... !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 4 --->>>>> !getCurrentSize340Iterative - Iterative... ---> Current size: 9 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 5 --->>>>> !getCurrentSize340Recursive() - Recursive... ---> Current size: 9 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 6 --->>>>> !getCurrentSize340RecursiveNoHelper() - Recursive... ---> Current size: 9 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 7 --->>>>> !getFrequencyOf()... ---> 0-ZERO: 1 ---> 1-ONE: 0 ---> 2-TWO: 0 ---> 4-FOUR: 3 ---> 9-NINE: 2 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

!getFrequencyOf340Recursive() - Recursive... ---> 0-ZERO: 1 ---> 1-ONE: 0 ---> 2-TWO: 0 ---> 4-FOUR: 3 ---> 9-NINE: 2 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 8 --->>>>> !getFrequencyOf340RecursiveNoHelper() - Recursive... ---> 0-ZERO: 1 ---> 1-ONE: 0 ---> 2-TWO: 0 ---> 4-FOUR: 3 ---> 9-NINE: 2 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 9 --->>>>> !removeRandom340() ---> 4-FOUR !removeRandom340() ---> 9-NINE !Display bag: 4-FOUR 5-FIVE #-END #-BEGIN 4-FOUR 9-NINE 0-ZERO -----------> 7 item(s) total

!removeRandom340() ---> #-BEGIN !removeRandom340() ---> 4-FOUR !Display bag: #-END 5-FIVE 4-FOUR 9-NINE 0-ZERO -----------> 5 item(s) total

!removeRandom340() ---> 9-NINE !removeRandom340() ---> #-END !Display bag: 4-FOUR 5-FIVE 0-ZERO -----------> 3 item(s) total

!removeRandom340() ---> 0-ZERO !removeRandom340() ---> 5-FIVE !Display bag: 4-FOUR -----------> 1 item(s) total

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

Output2

----- LINKED BAG 340 C++-----

--->>>>> Test 1 --->>>>> !add()... #-END 5-FIVE 4-FOUR 4-FOUR 3-THREE 2-TWO 1-ONE 0-ZERO #-BEGIN !Display bag: #-BEGIN 0-ZERO 1-ONE 2-TWO 3-THREE 4-FOUR 4-FOUR 5-FIVE #-END -----------> 9 item(s) total

--->>>>> Test 2 --->>>>> !removeSecondNode340()... !removeSecondNode340()... !Display bag: #-BEGIN 2-TWO 3-THREE 4-FOUR 4-FOUR 5-FIVE #-END -----------> 7 item(s) total

!removeSecondNode340()... !removeSecondNode340()... !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END -----------> 5 item(s) total

--->>>>> Test 3 --->>>>> !addEnd340()... !addEnd340()... !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR -----------> 7 item(s) total

!addEnd340()... !addEnd340()... !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 4 --->>>>> !getCurrentSize340Iterative - Iterative... ---> Current size: 9 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 5 --->>>>> !getCurrentSize340Recursive() - Recursive... ---> Current size: 9 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 6 --->>>>> !getCurrentSize340RecursiveNoHelper() - Recursive... ---> Current size: 9 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 7 --->>>>> !getFrequencyOf()... ---> 0-ZERO: 1 ---> 1-ONE: 0 ---> 2-TWO: 0 ---> 4-FOUR: 3 ---> 9-NINE: 2 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

!getFrequencyOf340Recursive() - Recursive... ---> 0-ZERO: 1 ---> 1-ONE: 0 ---> 2-TWO: 0 ---> 4-FOUR: 3 ---> 9-NINE: 2 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 8 --->>>>> !getFrequencyOf340RecursiveNoHelper() - Recursive... ---> 0-ZERO: 1 ---> 1-ONE: 0 ---> 2-TWO: 0 ---> 4-FOUR: 3 ---> 9-NINE: 2 !Display bag: #-BEGIN 4-FOUR 4-FOUR 5-FIVE #-END 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 9 item(s) total

--->>>>> Test 9 --->>>>> !removeRandom340() ---> #-END !removeRandom340() ---> 4-FOUR !Display bag: 4-FOUR 5-FIVE #-BEGIN 9-NINE 4-FOUR 9-NINE 0-ZERO -----------> 7 item(s) total

!removeRandom340() ---> 4-FOUR !removeRandom340() ---> 9-NINE !Display bag: #-BEGIN 5-FIVE 4-FOUR 9-NINE 0-ZERO -----------> 5 item(s) total

!removeRandom340() ---> 5-FIVE !removeRandom340() ---> #-BEGIN !Display bag: 4-FOUR 9-NINE 0-ZERO -----------> 3 item(s) total

!removeRandom340() ---> 9-NINE !removeRandom340() ---> 4-FOUR !Display bag: 0-ZERO -----------> 1 item(s) total

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

PARTB-Linked Bag. 20 points - Please change only files: LinkedBag340.cpp and Include.h, no other files. - We are to implement 8 small additional functions and 2 helper functions to the Linked Bag. Our programs must produce identical output to the output in the 2 sample runs: Asmt03_Runl.txt and Asmt03_Run2.txt Our Test 9's output must also be identical to the sample output excepts the random values. Our Test 9's random values in our 2 sample runs' output must be different. Updated: 10/29/2018 6:00 AM CSC 340.03+04+05 ASSIGNMENT 03 FALL 2018 TA ns Please ask questions, if any, during the in-class discussions and demos for this assignment. 1. removeSecondNode340 deletes the second node in the Linked Bag. 2 pts 2. addEnd340 inserts the new node at the end of the Linked Bag. 2 pts 3. getcurrentsize340Iterative counts the number of nodes in the Linked Bag iteratively. 2 pts 4. getCurrentsize340Recursive counts the number of nodes in the Linked Bag recursively. Use 1 helper function: getCurrentsize340RecursiveHelper. 2 pts 5 IMMEDIATE RECURSION: getcurrentsize340RecursiveNoHelper counts the number of nodes in the Linked Bag recursively. This recursive function does not use any helper functions. 4 pts getFrequencyof340Recursive recursively counts the number of times an entry appears in the Linked Bag. Use 1 helper function: getFrequencyof340RecursiveHelper. 2 pts IMMEDIATE RECURSION: getFrequencyof340RecursiveNoHelper recursively counts the number of times an entry appears in the Linked Bag. This recursive function does not use any helper functions. 4 pts removeRandom340 removes a random entry from the Linked Bag. 2 pts 6. 7. 8. PARTB-Linked Bag. 20 points - Please change only files: LinkedBag340.cpp and Include.h, no other files. - We are to implement 8 small additional functions and 2 helper functions to the Linked Bag. Our programs must produce identical output to the output in the 2 sample runs: Asmt03_Runl.txt and Asmt03_Run2.txt Our Test 9's output must also be identical to the sample output excepts the random values. Our Test 9's random values in our 2 sample runs' output must be different. Updated: 10/29/2018 6:00 AM CSC 340.03+04+05 ASSIGNMENT 03 FALL 2018 TA ns Please ask questions, if any, during the in-class discussions and demos for this assignment. 1. removeSecondNode340 deletes the second node in the Linked Bag. 2 pts 2. addEnd340 inserts the new node at the end of the Linked Bag. 2 pts 3. getcurrentsize340Iterative counts the number of nodes in the Linked Bag iteratively. 2 pts 4. getCurrentsize340Recursive counts the number of nodes in the Linked Bag recursively. Use 1 helper function: getCurrentsize340RecursiveHelper. 2 pts 5 IMMEDIATE RECURSION: getcurrentsize340RecursiveNoHelper counts the number of nodes in the Linked Bag recursively. This recursive function does not use any helper functions. 4 pts getFrequencyof340Recursive recursively counts the number of times an entry appears in the Linked Bag. Use 1 helper function: getFrequencyof340RecursiveHelper. 2 pts IMMEDIATE RECURSION: getFrequencyof340RecursiveNoHelper recursively counts the number of times an entry appears in the Linked Bag. This recursive function does not use any helper functions. 4 pts removeRandom340 removes a random entry from the Linked Bag. 2 pts 6. 7. 8

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions

Question

Ability to work comfortably in a team environment

Answered: 1 week ago

Question

Exposure to SQL desirable but not required

Answered: 1 week ago