Question

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 the end of the list.

Example 1

list: 5->7->3->2->1->4
list.distanceFrom(2);
list: 3->2->1->2->1->4

Example 2

list: 5->7->3->2->1->4
list.distanceFrom(6);
list: 6->5->4->3->2->1

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

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;
}

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 (help with this part)

#include "IntList.h"

void IntList::distanceFrom(int key) {
}


int IntList::searchAndModify(IntNode *curr, int key) {
return 0;
}

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

NOTE: As requested, I only changed the code in file 'IntList.cpp'. The code in rest of the files is unchanged.

1.The requested code is given below:-

//--------------------------------------------------------

#include "IntList.h"

void IntList::distanceFrom(int key) {
searchAndModify(head,key-1);
}


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

//--------------------------------------------------------

2.Screenshot of the output:-

IntList.cpp [RecursionList] - Code::Blocks 17.12 File C:\Users\vipul Documents\CBProjects\RecursionList\bin\Debug RecursionLi

Add a comment
Know the answer?
Add Answer to:
You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided)....
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
  • (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...

  • 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...

  • 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...

  • 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;   ...

  • 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...

  • 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,...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • 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:...

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