Question
//the complete thispart for problem 2 is indicating where you should impliment the code, do not worry about the problem 3 do not touch that
//the complete thispart for problem 2 is indicating where you should impliment the code, do not worry about the "problem 3" do not touch that part the prob2.cpp is what you use to test if the code actually fits with it or not. so do not touch any other code except"complete this part for problem 2 please hurryi have 15 minutes
integerlinkedlist.cpp
// ADD ANSWER TO THIS FILE
#include "IntegerLinkedList.h"
// bool IntegerLinkedList::checkList() // COMPLETE THIS FOR PROBLEM 2 bool IntegerLinkedList::checkList() { SNode *temp=head; int positive=0; int negative=0; bool pos=false; while(temp!=nullptr) { if (temp->elem%2==0 && temp->elem!=0) positive++;
else if(temp->elem!=0) negative++;
temp=temp->next; }
if (positive>negative) pos=true;
return pos; }
bool IntegerLinkedList::checkRecurse (SNode *ptr) { int count(int compare){ return -1; } //return -1; // COMPLETE THIS FOR PROBLEM 3 bool pos=false;
if(ptr==nullptr) return pos;
else if(ptr->elem>0) pos = true + checkRecurse(ptr->next);
else pos = false + checkRecurse(ptr->next);
return pos; }
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); } //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); }; //prob2.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 "; { 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.count(12)", mylist.count(12), 4); testAnswer("mylist.count(20)", mylist.count(20), 2); testAnswer("mylist.count(100)", mylist.count(100), 0); } { IntegerLinkedList mylist; mylist.addFront(17); cout << "List: 17" << endl; testAnswer("mylist.count(20)", mylist.count(20), 0); testAnswer("mylist.count(12)", mylist.count(12), 1); } { IntegerLinkedList mylist; cout << "List: empty" << endl; testAnswer("mylist.count(17)", mylist.count(17), 0); } // system("pause"); // comment/uncomment if needed }
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