Question: IntegerList.h: #pragma once #include #include IntListNode.h using namespace std; class IntegerList { // a singly linked list public: IntegerList(); // empty list constructor ~IntegerList(); //

IntegerList.h: #pragma once #include  #include "IntListNode.h" using namespace std; class IntegerList { // a singly linked list public: IntegerList(); // empty list constructor ~IntegerList(); // destructor int& front(); // return front element void addFront(int e); // add to front of list void removeFront(); // remove front item list void print(); int size() const; // list size private: IntListNode* head; // head of the list int n; // number of items }; 
IntListNode:
#pragma once class IntListNode { // singly linked list node public: IntListNode(int e, IntListNode *nextptr): elem(e), next(nextptr) { } int &getElement() {return elem;} IntListNode *getNext() {return next;} private: int elem; // linked list element value IntListNode *next; // next item in the list };

IntegerList.cpp:

#include  #include "IntegerList.h" IntegerList::IntegerList() // constructor : head(NULL), n(0) { } int& IntegerList::front() // return front element { if (n==0) throw length_error("empty list"); return head->getElement(); } IntegerList::~IntegerList() // destructor { while (n > 0) removeFront(); } void IntegerList::addFront(int e) { // add to front of list IntListNode* v = new IntListNode(e, head); // create new node head = v; // v is now the head n++; } void IntegerList::removeFront() { // remove front item if (n == 0) throw length_error("empty list"); IntListNode* old = head; // save current head head = old->getNext(); // skip over old head delete old; // delete the old head n--; } void IntegerList::print() { // print all elements IntListNode* current = head; while (current != NULL) { std::cout getElement() getNext(); } } int IntegerList::size() const { // list size return n; } 

Test.cpp:

#include  #include  #include "IntegerList.h" using namespace std; int main() { IntegerList mylist; for (int i = 0; i  

On the class Titanium site for Problem 2, you are given the following four files defining a simple linked list class called IntegerList: IntegerList.h, IntegerList.cpp, IntListNode.h and IntListNode.cpp. There is also a file called test.cpp that allows testing of this list containing integers. An IntegerList object contains one pointer pointing to the first or front node of the linked list. An IntListNode object contains an integer and a pointer to another IntListNode object following it. The main function in test.cpp allows you to insert integers at the front of the list and to print all integers in it.

Add a member function to the class IntegerList called AddAtPosition that takes as parameters an integer to be placed in the list and a number indicating the position where the new integer should be inserted. IntegerList.h has the member function heading in the public part of IntegerList. All you need to do is to add the member function declaration (the body of the member function).

See below for the how the position number corresponds to where the new node is placed in the list. If the position number is greater than the length of the list, it will just add the new node at the end.

3. Take either your revised IntegerList.cpp or the original IntegerList.cpp and add a recursive member function sum( ) that will return the sum of all numbers stored in the list. If it is called with the list pictured in problem 2, it should return 25 since 2 + 5 + 6 + 8 + 4 = 25.

A non-recursive version will get no credit.

Rename the file prob3IntegerList.cpp. Submit it to the EPP - Problem 3 section on the class Titanium site. If you wrote more than one file, make sure to send and copy them as well. Remember to put in your name and ID at the start of each file you turn in.

Positions 0 2 any integer> 4 2 4 Start of list End of list What above list would look like after AddAtPosition (6, 2) was called 2 6 4 Start of list End of list

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!