Question: //prob3.cpp // // EDIT THIS FILE ONLY FOR YOUR OWN TESTING // WRITE YOUR CODE IN IntegerLinkedList.cpp // #include #include #include IntegerLinkedList.h using std::string; using

//prob3.cpp

// // EDIT THIS FILE ONLY FOR YOUR OWN TESTING // WRITE YOUR CODE IN IntegerLinkedList.cpp //

#include #include #include "IntegerLinkedList.h"

using std::string; using std::cout; using std::endl;

void testAnswer(string testname, int answer, int expected) { if (answer == expected) cout << "PASSED: " << testname << " expected and returned " << answer << " "; else cout << "FAILED: " << testname << " returned " << answer << " but expected " << expected << " "; }

int main() { cout << "count elements larger than given number (recursive) "; { IntegerLinkedList mylist; mylist.addFront(10); mylist.addFront(17); mylist.addFront(23); mylist.addFront(17); mylist.addFront(92); cout << "List: 92 -> 17 -> 23 -> 17 -> 10" << endl; testAnswer("mylist.countRecurseHelper(12)", mylist.countRecurseHelper(12), 4); testAnswer("mylist.countRecurseHelper(20)", mylist.countRecurseHelper(20), 2); testAnswer("mylist.countRecurseHelper(100)", mylist.countRecurseHelper(100), 0); } { IntegerLinkedList mylist; mylist.addFront(17); cout << "List: 17" << endl; testAnswer("mylist.countRecurseHelper(20)", mylist.countRecurseHelper(20), 0); testAnswer("mylist.countRecurseHelper(12)", mylist.countRecurseHelper(12), 1); } { IntegerLinkedList mylist; cout << "List: empty" << endl; testAnswer("mylist.countRecurseHelper(17)", mylist.countRecurseHelper(17), 0); } // system("pause"); // comment/uncomment if needed }

//integerlinkedlist.h

#pragma once

class SNode { public: int elem; SNode *next; };

class IntegerLinkedList { private: SNode *head; int countRecurse (SNode *ptr, int compare); // for Problem 3; Implement in IntegerLinkedList.cpp

public: IntegerLinkedList(): head(nullptr) {} void addFront(int x);

int count(int compare); // for Problem 2; Implement in IntegerLinkedList.cpp

// recursion helper function called from main for PROBLEM 3 int countRecurseHelper (int compare); };

//integerlinkedlist.cpp

#pragma once

class SNode { public: int elem; SNode *next; };

class IntegerLinkedList { private: SNode *head; int countRecurse (SNode *ptr, int compare); // for Problem 3; Implement in IntegerLinkedList.cpp

public: IntegerLinkedList(): head(nullptr) {} void addFront(int x);

int count(int compare); // for Problem 2; Implement in IntegerLinkedList.cpp

// recursion helper function called from main for PROBLEM 3 int countRecurseHelper (int compare); };

void IntegerLinkedList::addFront(int x) { SNode *tmp = head; head = new SNode; head->next = tmp; head->elem = x; }

// recursion helper function called from main for PROBLEM 3 bool IntegerLinkedList::checkRecurseHelper () { return checkRecurse(head); }

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!