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
Although a user directory is treated as a file, it is flagged to indicate to the file manager that this file is really a ____ wh
mart [117]
Although a user directory is treated as a file, it is flagged to indicate to the file manager that this file is really a subdirectory whose records are filenames that point to files.
3 0
3 years ago
What is an example of fibre optic cable<br>​
Arte-miy333 [17]

Answer:

printing cable

Explanation:

is a cable used to transfer information from a computer to the printer in packages

5 0
2 years ago
GUYSSS!!!
otez555 [7]
Yea, I don’t think you can fix it unless you backed it up or something somehow...
8 0
3 years ago
Read 2 more answers
What kind of operating system is Windows? Command-line Browser based Graphical user interface Linux-based
snow_tiger [21]
Graphical user interface
4 0
3 years ago
Read 2 more answers
Tim has an old server computer that his company uses as a backup. One of the hard drives has gone bad and needs to be replaced.
dimaraw [331]

Answer:

SCSI.

Explanation:

It is a set of ANSI-developed simultaneous interface requirements for connecting scanners, drives, printers, and many other devices to systems.

Although when Tim has such an outdated network system which is used as a backup by his corporation. Then hard drive may have gone wrong and required replacement. The connector used by the hard disk drive is not something he has ever seen before, but he upload photos of that on the community board of the organization to aid determine what kind of hard disk drive they will also have to order.

So, according to the following scenario he required SCSI cable.

4 0
4 years ago
Other questions:
  • When you enter search keywords in the search box of file explorer and the onedrive option is selected?
    15·2 answers
  • Due to the absorption and scattering of shorter wavelengths by interstellar dust, distant stars appear A) bluer. B) brighter. C)
    9·1 answer
  • To connect multiple usb devices to a single usb port, a ____ can be used.
    12·1 answer
  • If you are viewing a webpage with customized or regenerated content, such as updated stock quotes, what type of webpage are you
    14·1 answer
  • Martha wants to invite her coworkers to her birthday party. She uses the mail merge feature to compose and send the invitations.
    6·1 answer
  • ________ is a dot on the screen that contains a color.
    11·1 answer
  • You have just changed the system time within your computer's BIOS. You choose to save the settings upon exit. What happens next
    14·2 answers
  • What is an example of CT SO?
    9·1 answer
  • What is the main difference between a search engine and a web browser?
    6·2 answers
  • Write a program that repeatedly takes integers from the user as long as he
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!