Question: Please write linkedlist.cpp for the following files -------------------------------------------------------------- // linkedlist.h #ifndef _LINKED_LIST_ #define _LINKED_LIST_ #include class LinkedList { public: LinkedList(); ~LinkedList(); void add(int value); int

Please write linkedlist.cpp for the following files

--------------------------------------------------------------

// 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_

----------------------------------------

// main.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;

}

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!