Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

C++ Database Development

Authors: Al Stevens

1st Edition

1558283579, 978-1558283572

More Books

Students also viewed these Databases questions

Question

6. What is polling?

Answered: 1 week ago