Question
I need a completed Functions.cpp fiile. AnyList.h, AnyList.cpp and Functions.cpp are attached at the end. Use these files: AnyList.h AnyList.cpp Functions.cpp To test each function
I need a completed Functions.cpp fiile. AnyList.h, AnyList.cpp and Functions.cpp are attached at the end.
Use these files:
- AnyList.h
- AnyList.cpp
- Functions.cpp
To test each function use these files one at a time:
- TestingGetMin.cpp
- TestingHaveThree.cpp
- TestingCommonEnds.cpp
- NEW version => TestingPreFour_v2.cpp
Implement the functions listed below as members of the AnyList class by writing the declaration in the AnyList.h file and the definition in the Functions.cpp file.
- Function getMin
- The function traverses the list and returns the minimum (smallest) value found.
- Assumptions:
- List has at least one element.
- All values are greater than zero.
- Function haveThree
- The function returns true if the value 3 appears in the calling list exactly 3 times and no 3's are next to each other.
- After implementing and testing your function, analyze your code and make sure it is efficient.
- Example:
- 3, 1, 3, 1, 3 => true
- 3, 1, 3, 3 => false
- 3, 4, 3, 3, 4 => false
- Assumptions:
- List has at least one element.
- Function preFour
- Parameter: An object of the class AnyList.
- Given a non-empty singly-linked list, the function copies all elements from the parameter list that come before the first 4 into the calling object in reverse order.
- Example:
- Parameter object: 1, 2, 7, 5, 4, 1 => Calling object becomes: 5, 7, 2, 1
- Parameter object: 3, 1, 4 => Calling object becomes: 1, 3
- Parameter object: 1, 4, 4 => Calling object becomes: 1
- The calling object is empty.
- The parameter list contains at least one 4.
- Function commonEnds
- Parameter: An object of the class AnyList.
- The function returns true if the calling object and the parameter object have the same first element AND the same last element.
- Example:
- Calling object: 1, 2, 3 => Parameter object: 1, 4, 3 => true
- Calling object: 5, 6, 1, 4 => Parameter object: 5, 3, 1, 7, 4, 3, 4 => true
- Calling object: 3, 4, 5 => Parameter object: 3, 5 => true
- Assumptions: Both lists have at least one element.
//
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 < 1) cerr << "List is empty."; // Use cerr, instead of cout. Why? else { Node* current = ptrToFirst; // Create a pointer to traverse the list. // This pointer will point to the first node in the list.
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 << current->getData() << " "; // Move the pointer current forward. current = current->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();
/*********************************************************/
// Declaration function getMin
// Declaration function haveThree
// Declaration function preFour
//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
//
Functions.cpp
/* (name header) */
#include "AnyList.h"
using namespace std;
// Definition function getMin
// Definition function haveThree
// Definition function preFour
//Definition function commonEnds
//
TestingCommonEnds.cpp
#include "AnyList.h"
#include
int main() { std::cout << "TEST: commonEnds ";
std::vector< std::vector
};
std::vector< std::vector
auto sizeV1 = vCalling.size(); auto sizeV2 = vParameter.size();
for (int i = 0; i < sizeV1; ++i) { // Create lists and insert elements AnyList callingList; AnyList paramList;
auto sizeInnerV1 = vCalling[i].size(); auto sizeInnerV2 = vParameter[i].size();
// Casting needed to avoid 4-8 byte overflow bug in VS. int limit1 = static_cast
// Print out std::cout << "Elements inserted in calling obj: "; for (auto elem : vCalling[i]) std::cout << elem << " "; std::cout << " Your calling obj is: "; callingList.print(); // Print out std::cout << " Elements inserted in parameter obj: "; for (auto elem : vParameter[i]) std::cout << elem << " "; std::cout << " Your parameter obj is: "; paramList.print(); std::cout << " Common ends? "; bool result = false; if (sizeInnerV1 != 0 && sizeInnerV2 != 0) result = (vCalling[i].at(0) == vParameter[i].at(0) && vCalling[i].at(sizeInnerV1 - 1) == vParameter[i].at(sizeInnerV2 - 1)); std::cout << " Expected: " << ((result) ? "Yes." : "No."); std::cout << " Your output: "; callingList.commonEnds(paramList); std::cout << ((result) ? "Yes." : "No.");
std::cout << " ---------------------------------------- "; }
std::cout << std::endl; return 0; } //
TestingGetMin.cpp
#include "AnyList.h"
#include
int main() { std::cout << "TEST: getMin... ";
std::vector< std::vector
int sizeV1 = static_cast
for (int i = 0; i < sizeV1; ++i) { // Create a new list and insert elements AnyList list1; int sizeInnerV1 = static_cast
// Print out std::cout << "Elements inserted: "; for (auto elem : v1[i]) std::cout << elem << " "; std::cout << " Your list is: "; list1.print();
// Get minimum value std::cout << " Get minimum value..."; std::cout << " Expected: " << *(min_element(v1[i].begin(), v1[i].end())); std::cout << " Your output: " << list1.getMin();
std::cout << " ---------------------------------------- "; }
std::cout << std::endl; return 0; }
Step 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