Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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
      Assumptions:
      • 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 #include #include #include

int main() { std::cout << "TEST: commonEnds ";

std::vector< std::vector> vCalling = { { 1 }, { 2 }, { 6, 3 }, { 7, 5 }, { 8, 4, 2, 9, 5, 6, 7, 1 }, { 1, 2, 3 }, { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5, 6, 7 , 8, 9 }, { 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85 }, { 67, 45, 23, 79, 15, 62, 94, 57, 71, 29, 45, 32 }

};

std::vector< std::vector> vParameter = { { 3 }, { 2 }, { 6, 4 }, { 8, 5 }, { 8, 4, 2, 9 }, { 7, 3, 3 }, { 4, 9, 2, 5 }, { 1, 23, 45, 78, 75, 25, 13, 67, 43, 25, 9 }, { 10, 25, 50, 45, 96, 60, 90, 36, 74, 93, 60, 87, 34, 14, 56, 85 }, { 67, 12, 36, 79, 97, 26, 49, 75, 71, 92, 54, 76, 34, 45, 32 } };

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(sizeInnerV1 - 1); int limit2 = static_cast(sizeInnerV2 - 1); for (int j = limit1; j >= 0; --j) callingList.insertFront(vCalling[i].at(j)); for (int j = limit2; j >= 0; --j) paramList.insertFront(vParameter[i].at(j));

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

int main() { std::cout << "TEST: getMin... ";

std::vector< std::vector> v1 = { { 1 }, { 5, 6, 2, 9, 3 }, { 3, 9 }, { 5, 7 }, { 2, 6, 9 }, { 9, 7, 5 }, { 6, 8, 3 }, { 2, 5, 3, 7, 9, 4, 1 }, { 2, 5, 7, 3, 4, 8, 6, 9 } };

int sizeV1 = static_cast(v1.size());

for (int i = 0; i < sizeV1; ++i) { // Create a new list and insert elements AnyList list1; int sizeInnerV1 = static_cast(v1[i].size()); for (int j = sizeInnerV1 - 1; j >= 0; --j) list1.insertFront(v1[i].at(j));

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

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

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions

Question

What is Ohm's law and also tell about Snell's law?

Answered: 1 week ago