Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a method function nodeCount() to count the elements in the list. When yo

ID: 3621279 • Letter: D

Question

Develop a method function nodeCount() to count the elements in the list. When you finish your implementation, you have to make sure that your code works properly with an empty list, a list with one element, and a list with many elements. This is the prototype of the menthod.

int nodeCount();


The program code is listed below:

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

LinkedList.h

//----- LinkedList.h -----
#ifndef LINKEDLIST
#define LINKEDLIST

#include
using namespace std;

typedef int ElementType;

class LinkedList
{
public:
LinkedList(); // constructor
~LinkedList(); // destructor
LinkedList(const LinkedList & original); //copy constructor
void insert(ElementType item, int pos);
void erase(ElementType item);
void traverse();

void display(ostream & out) const;

private:
class Node
{
public:
ElementType data;
Node * next;
//------ Node OPERATIONS
//-- Default constrctor: initializes next member to
Node()
: next(NULL)
{ }
//-- Explicit-value constructor: initializes data member to dataValue
//-- and next member to 0
Node(ElementType dataValue)
: data(dataValue), next(NULL)
{ }
};
Node * first;
int mySize;
};

#endif
------------------------------------------------------------------------------------------------------

LinkedList.cpp

#include
using namespace std;

#include "LinkedList.h"

//-- Default constructor
LinkedList::LinkedList()
{
mySize = 0;
first = NULL;
}

//-- Definition of the copy constructor
LinkedList::LinkedList(const LinkedList & origList)
{
mySize = origList.mySize;
first = NULL;

if (mySize == 0)
return;

Node * origPtr, * lastPtr;
first = new Node(origList.first->data); // copy first node
lastPtr = first;
origPtr = origList.first->next;
while (origPtr != NULL)
{
lastPtr->next = new Node(origPtr->data);
origPtr = origPtr->next;
lastPtr = lastPtr->next;
}
}


//-- Definition of the destructor
LinkedList::~LinkedList()
{
Node * prev = first;
Node * ptr;

while (prev != NULL)
{
ptr = prev->next;
delete prev;
prev = ptr;
}
}

// Traverse the LinkedList and process data in each node
void LinkedList::traverse()
{
Node * ptr = first;
ElementType sum = 0;

while (ptr != NULL)
{
// Process part of data
// For example find sum of all data.
sum += ptr->data;
ptr = ptr->next;
}
return;
}


//-- Definition of insert()
void LinkedList::insert(ElementType dataVal, int index)
{
if (index < 0 || index > mySize)
{
cerr << "Illegal location to insert -- " << index << endl;
return;
}

mySize++;
Node * newPtr = new Node(dataVal);
Node * predPtr = first;
if (index == 0)
{
newPtr->next = first;
first = newPtr;
}
else
{
for(int i = 1; i < index; i++)
predPtr = predPtr->next;
newPtr->next = predPtr->next;
predPtr->next = newPtr;
}
}


//-- Definition of erase()
void LinkedList::erase(int index)
{
if (index < 0 || index >= mySize)
{
cerr << "Illegal location to delete -- " << index << endl;
return;
}

mySize--;
Node * ptr;
Node * predPtr = first;
if (index == 0)
{
ptr = first;
first = ptr->next;
delete ptr;
}
else
{
for(int i = 1; i < index; i++)
predPtr = predPtr->next;
ptr = predPtr->next;
predPtr->next = ptr->next;
delete ptr;
}
}



//-- Definition of display()
void LinkedList::display(ostream & out) const
{
Node * ptr = first;
while (ptr != 0)
{
out << ptr->data << " ";
ptr = ptr->next;
}
}
-----------------------------------------------------------------------------------------------------

Driver.cpp

#include
using namespace std;

#include "LinkedList.h"

int main()
{
// Test the class constructor
LinkedList intList;
cout << "Constructing intList ";

// Test insert()
intList.insert(100, 0);
intList.display(cout);
cout << endl;

intList.insert(200, 0);
intList.display(cout);
cout << endl;

intList.insert(300, 1);
intList.display(cout);
cout << endl;

intList.insert(400, 1);
intList.display(cout);
cout << endl;

intList.insert(500, 3);
intList.display(cout);
cout << endl;

// Test destructor
{
LinkedList anotherList;
for (int i = 0; i < 10; i++)
{
anotherList.insert(100*i, i);
}
cout << " This is another list ";
anotherList.display(cout);
}

// Test erase
intList.erase(1);
intList.erase(1);
cout << " Two items are erased from the first list ";
intList.display(cout);
cout << endl;

}

Explanation / Answer

I'm assuming you didn't write this linked list yourself. Anyways its quite simple you're already keeping count of the size of the list. mySize is the size of your list, just return it. int nodeCount() { return mySize; }