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