1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ivahew [28]
4 years ago
14

You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function w

ill 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.
Computers and Technology
1 answer:
sashaice [31]4 years ago
3 0

Answer:

\*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 distanceFrom(int key) {

head->data = searchAndModify(head, key);

}

int searchAndModify(IntNode *curr, int key) {

if (key == head->data) {

return(curr->data);

}

/* if key found or last node found , then return 0 */

if (curr==nullptr || key==curr->data) {

return 0;

}

if(curr != nullptr && key != curr->data) {

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

return(curr->data);

}

}

/* 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;

}

system("pause");

return 0}

You might be interested in
Which of the following is similar to defense in depth and supports multiple layers of security?a. Defense in depthb. Diversity o
Lina20 [59]

Answer: B) Diversity of defense

Explanation:

 Diversity of defense support the multiple layer of security and it uses in different security mechanisms. The diversity of defense is the significant in the security system.

It basically reduces the configuration error and bug in the system. It also reduces the cost and complexity of the system. But it takes longer time for installing multiple different system. It is similar to defense in depth as, it also support the multiple layer of security.

 

3 0
3 years ago
To freeze rows 1 and 2, and columns 1, 2, and 3, which cell should you highlight before selecting freeze panes?.
Brilliant_brown [7]

In the case above, the cell that you can highlight before selecting freeze panes is row 3 column 4.

<h3>How do one freeze several column?</h3>

This can be done when a person select the column that's is said to be next to the right of the last column a person want to freeze.

Therefore, In the case above, the cell that you can highlight before selecting freeze panes is row 3 column 4.

Learn more about panes from

brainly.com/question/17194167

#SPJ1

4 0
2 years ago
Which of the following refers to rules written by an organization or entity to guide behavior in a particular setting or situati
nevsk [136]
I think the answer to this is C
7 0
4 years ago
Read 2 more answers
A robot can complete 7 tasks in hour. Each task takes the same amount of time.
bekas [8.4K]

Answer:

A. I believe is 8.57

B. Should be 7

Explanation:

Please forgive me if I'm wrong thats my best shot. However if i am right please mark brainliest

3 0
3 years ago
Read 2 more answers
How do you get a blank business card template in word?
denis23 [38]

Answer:

I dont think you'll get it on word but try Canva thats where i designed my buisness cards

Explanation:

3 0
3 years ago
Other questions:
  • To prevent class objects from being copied or assigned, you can:
    8·1 answer
  • Vpns can be classified based on the kinds of ____ they connect
    6·1 answer
  • Spammers can defeat the registration process of free email services by launching a coordinated _____ attack that can sign up for
    14·1 answer
  • ____________ is defined as the set of activities that revolve around a new software product, from its inception to when the prod
    11·1 answer
  • To find if the Library owns the item listed below using Quick SearchLinks to an external site., what would you type in the searc
    9·1 answer
  • How and why do engineers use models?
    15·1 answer
  • What does px mean in css ?
    13·1 answer
  • Chelsea wants to know about the requirements for being a lab technician where could you find that information
    7·1 answer
  • Three Strings someone help
    15·1 answer
  • What is the full form of SAARC<br><br>And free point!! ​
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!