Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions