Question
Please run these files and help me to correct the Function.cpp ( void AnyList::preFour(const AnyList& other) ) the calling object should print empty space
Please run these files and help me to correct the Function.cpp (void AnyList::preFour(const AnyList& other))
the calling object should print "empty space" instead "list is empty"
--------------------------------------------------------------------------
main.cpp
#include "AnyList.h"
#include #include #include #include
int main() { std::cout
std::vector vParameter = { { 1, 2, 4, 1 }, { 1, 4, 4 }, { 1, 4, 4, 2 }, { 1, 3, 4, 2, 4 }, { 4, 4 }, { 3, 3, 4 }, { 1, 2, 1, 4 }, { 3, 4, 3, 4, 3, 4, 4 }, { 2, 1, 4, 2 }, { 2, 1, 2, 1, 4, 2 }, { 3, 8, 2, 5, 7, 9, 1, 4, 6}, };
std::vector vCalling = { { 2, 1 }, { 1 }, { 1 }, { 3, 1 }, { }, { 3, 3 }, { 1, 2, 1 }, { 3 }, { 1, 2 }, { 1, 2, 1, 2 }, { 1, 9, 7, 5, 2, 8, 3 }, };
int sizeV1 = static_cast(vParameter.size());
for (int i = 0; i = 0; --j) paramList.insertFront(vParameter[i].at(j));
// Print out std::cout
// Call function preFour AnyList callingList; callingList.preFour(paramList);
// Print out std::cout
std::cout
std::cout
AnyList.cpp
#include "AnyList.h"
using namespace std;
void AnyList::insertFront(int newData) { ptrToFirst = new Node(newData, ptrToFirst); ++count; }
void AnyList::print() const { if (ptrToFirst == nullptr) // Check if the list is empty. // You can also use: if (count
while (current != nullptr) // While the current pointer is NOT a nullptr, // that is, while the current pointer has not reached // the end of the list. { // Output the data. cout getData() getPtrToNext(); } } }
// This function does not delete the // list object; it ONLY deletes the nodes. void AnyList::clearList() { Node* temp = ptrToFirst; // Pointer to delete the node, which // starts by pointing to the first node.
while (ptrToFirst != nullptr) { ptrToFirst = ptrToFirst->getPtrToNext(); delete temp; temp = ptrToFirst; }
// Update the count outside the loop. count = 0; }
AnyList::~AnyList() { clearList(); }
----------------------------------------------------------------------------------
AnyList.h
#ifndef ANYLIST_H #define ANYLIST_H
#include #include
class Node { public: Node() : data(0), ptrToNext(nullptr) {} Node(int theData, Node* newPtrToNext) : data(theData), ptrToNext(newPtrToNext) {} Node* getPtrToNext() const { return ptrToNext; } int getData() const { return data; } void setData(int theData) { data = theData; } void setPtrToNext(Node* newPtrToNext) { ptrToNext = newPtrToNext; } ~Node() {} private: int data; Node* ptrToNext; // Pointer that points to next node. };
class AnyList { public: AnyList() : ptrToFirst(nullptr), count(0) {}
void insertFront(int);
void print() const;
void clearList(); ~AnyList();
/*********************************************************/
int getMin() const;// Declaration function getMin
bool haveThree() const;// Declaration function haveThree
void preFour(const AnyList&) ;// Declaration function preFour
bool commonEnds(const AnyList&) const;//Declaration function commonEnds
private: // Pointer to point to the first node in the list. Node* ptrToFirst; // Variable to keep track of number of nodes in the list. int count; };
#endif
-------------------------------------------------------------------------------------------------------------
Function.cpp
#include "AnyList.h"
using namespace std;
int AnyList::getMin() const { Node* current = ptrToFirst; if (current == nullptr) { return -1; }
int minVal = current->getData();
while (current != nullptr) { if (current->getData() getData(); }
current = current->getPtrToNext(); } return minVal; }
// Definition function haveThree bool AnyList::haveThree() const { bool found = false; int count = 0; Node* current = ptrToFirst; while (current != nullptr) { if (current->getData() == 3) { if (found) { return false; } ++count; found = true; } else found = false; current = current->getPtrToNext(); } if (count == 3) return true; }
// Definition function preFour void AnyList::preFour(const AnyList& other) { Node* t1 = ptrToFirst; Node* t2 = other.ptrToFirst; while (t2->getData() != 4 && t2 != 0) { insertFront(t2->getData()); t2 = t2->getPtrToNext(); } } //Definition function commonEnds bool AnyList::commonEnds(const AnyList& otherList) const {
if (count == 0 || otherList.count == 0) { return false; } else if (count == 1 && otherList.count == 1) { return true; } else { // get last elements of both lists Node* current = ptrToFirst; Node* temp = otherList.ptrToFirst;
while (current->getPtrToNext() != nullptr) { // update pointer current = current->getPtrToNext(); }
while (temp->getPtrToNext() != nullptr) { // update pointer temp = temp->getPtrToNext(); }
// return true if last items match return current->getData() == temp->getData(); } return false;
}
Elements inserted: 4 4 Your list is: 4 4 Get results... Expected calling object: Your calling object: List is empty. Expected parameter object: 4 4 Your parameter object: 4 4Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started