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
A ____ is a prewritten formula that is built into excel.
kondor19780726 [428]
A function is a prewritten formula that is built into excel.
3 0
4 years ago
Sales representatives want to capture custom feedback record details related to each Account. The sales reps want to accomplish
Tomtit [17]

Answer:

Create predefined values for most of the fields.

Create an object-specific action on Account

Explanation:

The sales reps would need to create predefined values for most of the fields, or they can create an object-specific action on account. In doing this, the sales reps will capture custom feedback record details related to each account and will accomplish this with minimal clicks on the Salesforce1 mobile app.

3 0
3 years ago
A ______ object is used for displaying the results of a question based on stored data.
LekaFEV [45]

Answer:

B. report

Explanation:

Report is used to show the result of data base quires or stored data contain useful data use in decision making and analysis.

It is also called a report generator, it is a part of database management system that is used to extract information from one or more files and present it in specific format.

4 0
3 years ago
Mt. Everest, the highest mountain in the world, is located in Asia.<br><br> True<br> False
Roman55 [17]
True, it is located on the border of Nepal and Tibet.
8 0
3 years ago
You create a storyboard at the _____ stage of web development
ddd [48]

Answer:

initial stage of web development.

4 0
4 years ago
Read 2 more answers
Other questions:
  • Which term represents a computational instruction stored in computer memory?
    13·2 answers
  • What is the number 5280 in binary
    14·1 answer
  • o maintain reasonable key security (mark all that apply): Ensure that someone else always has an extra key. Ensure that no extra
    10·1 answer
  • The DNS server at your headquarters holds a standard primary zone for the abc domain. A branch office connected by a slow WAN li
    14·1 answer
  • Together with some anthropologists, you’re studying a sparsely populated region of a rainforest, where 50 farmers live along a 5
    15·1 answer
  • In excel what happens when rename a sheet from sheet1 to inventory​
    7·1 answer
  • Which four of the following hardware components are specifically used to enable networking and are not part of a stand-alone com
    13·2 answers
  • On which of the following pointing devices can you control the pointer by sliding your fingertip?
    11·1 answer
  • Explain how principles of computer programming are applied in different languages to produce
    5·1 answer
  • Hello. I really need this done ASAP. Don't scroll down. JUst look please.
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!