Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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_2

Step: 3

blur-text-image_3

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

4. Explain how to use fair disciplinary practices.

Answered: 1 week ago

Question

1. Television more Over watching faceing of many problems ?

Answered: 1 week ago

Question

Is there a link between chronic stress and memory function?

Answered: 1 week ago