Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ please This week you convert the operator < < function to use a recursive solution and add a recursive exists function that determines whether
C++ please
This week you convert the operator<< function to use a recursive solution and add a recursive exists function that determines whether a particular integer value exists within the IntList.
IntList.h
The following is the IntList.h file you must use. DO NOT add anything to this file.
#ifndef __INTLIST_H__ #define __INTLIST_H__ #includeusing namespace std; struct IntNode { int data; IntNode *next; IntNode(int data) : data(data), next(0) {} }; class IntList { private: IntNode *head; public: /* Initializes an empty list. */ IntList(); /* Inserts a data value to the front of the list. */ void push_front(int); /* Outputs to a single line all of the int values stored in the list, each separated by a space. This function does NOT output a newline or space at the end. */ friend ostream & operator<<(ostream &, const IntList &); /* Returns true if the integer passed in is contained within the IntList, otherwise returns false. */ bool exists(int) const; private: /* Helper function that returns true if the integer passed in is contained within the IntNodes starting from the IntNode whose address is passed in, otherwise returns false. */ bool exists(IntNode *, int) const; }; /* Helper function that passes in the address of a IntNode within an IntList and outputs all of integers within the IntList starting from this IntNode. */ ostream & operator<<(ostream &, IntNode *); #endif
main function
Use the following main function to pass the first 2 tests in zyBooks.
int main() { IntList test1; IntList test2; int testNum; cout << "Enter test number: "; cin >> testNum; cout << endl; // Test operator<< function if (testNum == 1) { // output empty list cout << "Empty : " << test1 << "T" << endl; // output list of size 1 test1.push_front(3); cout << "Size 1: " << test1 << "T" << endl; // output list of size 2 test1.push_front(0); cout << "Size 2: " << test1 << "T" << endl; // output list of size 5 test1.push_front(-3); test1.push_front(8); test1.push_front(0); cout << "Size 5: " << test1 << "T" << endl; } // Test exists function if (testNum == 2) { // test on empty list cout << "Empty: " << test2.exists(0) << endl; test2.push_front(-4); // test on list of size 1, doesn't exist cout << "Size 1 (doesn't exist): " << test2.exists(-1) << endl; // test on list of size 1, does exist cout << "Size 1 (does exist): " << test2.exists(-4) << endl; test2.push_front(-5); test2.push_front(4); test2.push_front(0); test2.push_front(6); // {6 0 4 -5 -4} // test on list of size 5, doesnt' exist cout << "Size 5 (doesn't exist): " << test2.exists(1) << endl; // test on list of size 5, exists in head cout << "Size 5 (exists in head): " << test2.exists(6) << endl; // test on list of size 5, exists in tail cout << "Size 5 (exists in tail): " << test2.exists(-4) << endl; // test on list of size 5, exists somewhere in middle cout << "Size 5 (exists in middle): " << test2.exists(0) << endl; } return 0; }
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