Question: You are given this code: linkedlist.h #ifndef _LINKED_LIST_ #define _LINKED_LIST_ #include class LinkedList { public: LinkedList(); ~LinkedList(); void add(int value); int sum(); // sum of

You are given this code:

linkedlist.h

#ifndef _LINKED_LIST_

#define _LINKED_LIST_

#include

class LinkedList

{

public:

LinkedList();

~LinkedList();

void add(int value);

int sum(); // sum of ints in list, calculated iteratively

int sumR(); // sum of ints in list, calculated recursively

friend std::ostream& operator<<(std::ostream& out, LinkedList& list);

private:

struct Node

{

Node(int value) : value(value), next(nullptr) {}

Node(int value, Node* next) : value(value), next(next) {}

int value;

Node* next;

};

int _sumR(Node* node); // sum of ints in list, calculated recursively

Node* head;

};

#endif // _LINKED_LIST_

makefile

CC = g++

CPPFLAGS = -Wall -g -std=c++11 -m32

LDFLAGS = -m32

app: app.o linkedlist.o randomarray.o

app.o: linkedlist.h

linkedlist.o: linkedlist.h

.PHONY: clean

clean: # clean the directory

$(info -- cleaning the directory --)

rm -f app.o linkedlist.o

app.cpp

#include

#include "linkedlist.h"

using namespace std;

static const int N_RANDS{9};

int* randomArray(int lgth);

int main()

{

int* ar{randomArray(N_RANDS)};

LinkedList list;

for (int i{0}; i < N_RANDS; i++)

list.add(ar[i]);

cout << "sum() = " << list.sum() << endl;

cout << "sumR() = " << list.sumR() << endl;

delete[] ar;

return 0;

}

Add two public functions to your linked list class:

int sum(void) Iteratively compute and return the sum of the ints contained in the linked list.

int sumR(void) Recursively compute and return the sum of the ints contained in the linked list.

You will need to add a private function to your linked list class for sumR to call. Once you figure out how to do the recursion, you should see why you need the private function. In app.cpp, the function randomArray is used to return an array containing random numbers. The function prints the numbers in the array, along with their sum. You can use this information to check the accuracy of your code. The function is provided in object code file randomArray.o.

What is the output?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!