Question

(C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class...

(C++)

You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance from the end of the list.

The function must be recursive, you are not allowed to use any kind of loop. You also may NOT use global or static variables.

IntList.h:

#ifndef __INTLIST_H__
#define __INTLIST_H__

#include <ostream>

using namespace std;

struct IntNode {
int data;
IntNode *next;
IntNode(int data) : data(data), next(nullptr) {}
};


class IntList {

private:
IntNode *head;

public:

/* Initializes an empty list.
*/
IntList() : head(nullptr) {}

/* Inserts a data value to the front of the list.
*/
void push_front(int val) {
if (!head) {
head = new IntNode(val);
} else {
IntNode *temp = new IntNode(val);
temp->next = head;
head = temp;
}
}

/* 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 &out, const IntList &rhs) {
if (rhs.head) {
IntNode *curr = rhs.head;
out << curr->data;
for (curr = curr->next ; curr ; curr = curr->next) {
out << ' ' << curr->data;
}
}
return out;
}


/* Update all nodes previous to the node containing the passed in integer to be the distance from that node
(1 for the node directly preceding it)
No return value. Works by calling a recursive function (defined below).
*/
void distanceFrom(int);

private:


/* Recursive helper functions that will (1) find the key passed in and then
(2) recursively update the nodes preceding it to contain their distance from the node containing the key.
If the key is not found, update with the distance from the end, with the last node having the value of 1.
*/
int searchAndModify(IntNode *, int);
};

#endif

IntList.cpp:

#include "IntList.h"

void IntList::distanceFrom(int key) {
IntNode *cur = head;
IntNode *prv = head;
int i = 0;

while (cur != nullptr && cur->data != key) {
prv = cur;
cur = cur->next;
i++;
}
prv->data = searchAndModify(head, i);
}

int IntList::searchAndModify(IntNode *curr, int key) {
if (key == 1) {
return 1;
} else {
curr->data = key;
return searchAndModify(curr->next, key - 1);
}
}

Main.cpp:

#include <iostream>

using namespace std;

#include "IntList.h"

int main() {

int testNum;

cout << "Enter test number: ";
cin >> testNum;
cout << endl;

if (testNum == 1) {
IntList test1;
test1.push_front(-3);
test1.push_front(1);
test1.push_front(5);
test1.push_front(8);
test1.push_front(2);
test1.push_front(4);
test1.push_front(0);
test1.push_front(9);
cout << "Key is 8" << endl;
cout << "Before: " << test1 << endl;
test1.distanceFrom(8);
cout << "After : " << test1 << endl;
}

// Test exists function
if (testNum == 2) {
IntList test2;
test2.push_front(-3);
test2.push_front(8);
test2.push_front(10);
test2.push_front(-42);
test2.push_front(3);
test2.push_front(58);
cout << "Key is -3" << endl;
cout << "Before: " << test2 << endl;
test2.distanceFrom(-3);
cout << "After : " << test2 << endl;
}

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//InList.cpp file

#include "IntList.h"

void IntList::distanceFrom(int key) {

searchAndModify(head,key);

}

int IntList::searchAndModify(IntNode *curr, int key) {

if(curr == NULL || curr->data == key)

return 0;

return curr->data = (1 + searchAndModify(curr->next, key));

}

===================================================
SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
(C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided)....

    You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance from...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • C++, Change the destroy_list function in the header file to a recursive destroy_list function, main is...

    C++, Change the destroy_list function in the header file to a recursive destroy_list function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();    void...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • I need help and have to get this done asap: Using the following source codes provided below, create recursive functions...

    I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...

  • Objectives Familiarize the student with: implementing data structures in C++; dynamic allocation of C++ objects. Scenario...

    Objectives Familiarize the student with: implementing data structures in C++; dynamic allocation of C++ objects. Scenario There are some classic data structures in computer science. One example of this that you've seen in the lectures is called the stack. In the next few exercises, we'll build yet another classic data structure, the linked list. To be exact, we'll be building the singly linked list. The building block of a singly linked list is a node, which consists of two parts:...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT