Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This week you convert the operator < < function to use a recursive solution and add a recursive exists function that determines whether a particular

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. Make a .cpp file. DO NOT USE ANY LOOPS.

intlist.h

#ifndef __INTLIST_H__ #define __INTLIST_H__ #include using 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.cpp

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

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

Step: 3

blur-text-image

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

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions

Question

This week you convert the operator Answered: 1 week ago

Answered: 1 week ago

Question

Ty e2y Evaluate the integral dy

Answered: 1 week ago

Question

Describe key customer feedback collection tools.

Answered: 1 week ago

Question

Know what customers expect from the firm when they complain.

Answered: 1 week ago

Question

Understand why customers complain.

Answered: 1 week ago