I am suppose to write a program that inserts 25 random integers from 0 to 100 in order in a linked list. The program should calculate the sum of the elements and the floating-point average of the elements.
I have been stuck with the add and average functions for some time now. I have been trying to get the element in the current node and insert it into sum, and then move onto the next element, but everything that I try seems to compile an error.
Here is my code.
#ifndef LIST_H
#define LIST_H
#include <iostream>
#include <new>
#include "ListNode.h"
using namespace std;
template< typename NODETYPE >
class List
{
public:
List(); // default constructor
List( const List< NODETYPE > & ); // copy constructor
~List(); // destructor
void insertAtFront( const NODETYPE & );
void insertAtBack( const NODETYPE & );
bool removeFromFront( NODETYPE & );
bool removeFromBack( NODETYPE & );
bool isEmpty() const;
void print() const;
protected:
ListNode< NODETYPE > *firstPtr; // pointer to first node
ListNode< NODETYPE > *lastPtr; // pointer to last node
// Utility function to allocate a new node
ListNode< NODETYPE > *getNewNode( const NODETYPE & );
}; // end class template List
// default constructor
template< typename NODETYPE >
List< NODETYPE >::List()
{
firstPtr = lastPtr = 0;
} // end constructor
// copy constructor
template< typename NODETYPE >
List< NODETYPE >::List( const List<NODETYPE> © )
{
firstPtr = lastPtr = 0; // initialize pointers
ListNode< NODETYPE > *currentPtr = copy.firstPtr;
// insert into the list
while ( currentPtr != 0 )
{
insertAtBack( currentPtr->data );
currentPtr = currentPtr->nextPtr;
} // end while
} // end List copy constructor
// destructor
template< typename NODETYPE >
List< NODETYPE >::~List()
{
if ( !isEmpty() ) // List is not empty
{
cout << "Destroying nodes ...\n";
ListNode< NODETYPE > *currentPtr = firstPtr;
ListNode< NODETYPE > *tempPtr;
while ( currentPtr != 0 ) // delete remaining nodes
{
tempPtr = currentPtr;
cout << tempPtr->data << ' ';
currentPtr = currentPtr->nextPtr;
delete tempPtr;
} // end while
} // end if
cout << "\nAll nodes destroyed\n\n";
} // end destructor
// Insert a node at the front of the list
template< typename NODETYPE >
void List< NODETYPE >::insertAtFront( const NODETYPE &value )
{
ListNode<NODETYPE> *newPtr = getNewNode( value );
if ( isEmpty() ) // List is empty
firstPtr = lastPtr = newPtr;
else // List is not empty
{
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
} // end else
} // end function insertAtFront
// Insert a node at the back of the list
template< typename NODETYPE >
void List< NODETYPE >::insertAtBack( const NODETYPE &value )
{
ListNode< NODETYPE > *newPtr = getNewNode( value );
if ( isEmpty() ) // List is empty
firstPtr = lastPtr = newPtr;
else // List is not empty
{
lastPtr->nextPtr = newPtr;
lastPtr = newPtr;
} // end else
} // end function insertAtBack
// Delete a node from the front of the list
template< typename NODETYPE >
bool List< NODETYPE >::removeFromFront( NODETYPE &value )
{
if ( isEmpty() ) // List is empty
return false; // delete unsuccessful
else
{
ListNode< NODETYPE > *tempPtr = firstPtr;
if ( firstPtr == lastPtr )
firstPtr = lastPtr = 0;
else
firstPtr = firstPtr->nextPtr;
value = tempPtr->data; // data being removed
delete tempPtr;
return true; // delete successful
} // end else
} // end function removeFromFront
// delete a node from the back of the list
template< typename NODETYPE >
bool List< NODETYPE >::removeFromBack( NODETYPE &value )
{
if ( isEmpty() )
return false; // delete unsuccessful
else
{
ListNode< NODETYPE > *tempPtr = lastPtr;
if ( firstPtr == lastPtr )
firstPtr = lastPtr = 0;
else
{
ListNode< NODETYPE > *currentPtr = firstPtr;
while ( currentPtr->nextPtr != lastPtr )
currentPtr = currentPtr->nextPtr;
lastPtr = currentPtr;
currentPtr->nextPtr = 0;
} // end else
value = tempPtr->data;
delete tempPtr;
return true; // delete successful
} // end else
} // end function removeFromBack
// Is the List empty?
template< typename NODETYPE >
bool List< NODETYPE >::isEmpty() const
{
return firstPtr == 0;
} // end function isEmpty
// Return a pointer to a newly allocated node
template< typename NODETYPE >
ListNode< NODETYPE > *List< NODETYPE >::getNewNode(
const NODETYPE &value)
{
ListNode< NODETYPE > *ptr = new ListNode< NODETYPE >( value );
return ptr;
} // end function getNewNode
// Display the contents of the List
template< typename NODETYPE >
void List< NODETYPE >::print() const
{
if ( isEmpty() ) // empty list
{
cout << "The list is empty\n\n";
return;
} // end if
ListNode< NODETYPE > *currentPtr = firstPtr;
cout << "The list is: ";
while ( currentPtr != 0 ) // display elements in list
{
cout << currentPtr->data << ' ';
currentPtr = currentPtr->nextPtr;
} // end while
cout << "\n\n";
} // end function print
#endif
#ifndef LISTNODE_H
#define LISTNODE_H
template< typename T > class List; // forward declaration
template< typename NODETYPE >
class ListNode
{
friend class List< NODETYPE >; // make List a friend
public:
ListNode( const NODETYPE & ); // constructor
NODETYPE getData() const; // return the data in the node
// set nextPtr to nPtr
void setNextPtr( ListNode *nPtr )
{
nextPtr = nPtr;
} // end function setNextPtr
// return nextPtr
ListNode *getNextPtr() const
{
return nextPtr;
} // end function getNextPtr
private:
NODETYPE data; // data
ListNode *nextPtr; // next node in the list
}; // end class ListNode
// constructor
template< typename NODETYPE >
ListNode< NODETYPE >::ListNode( const NODETYPE &info )
{
data = info;
nextPtr = 0;
} // end constructor
// return a copy of the data in the node
template< typename NODETYPE >
NODETYPE ListNode< NODETYPE >::getData() const
{
return data;
} // end function getData
#endif
#ifndef LIST2_H
#define LIST2_H
#include "ListNode.h"
#include "List.h"
template< typename NODETYPE >
class List2 : public List< NODETYPE >
{
public:
void insertInOrder( const NODETYPE & );
}; // end class List2
// insert a node in order
template< typename NODETYPE >
void List2< NODETYPE >::insertInOrder( const NODETYPE &value )
{
if ( isEmpty() ) // list is empty
{
ListNode< NODETYPE > *newPtr = getNewNode( value );
firstPtr = lastPtr = newPtr;
} // end if
else // list is not empty
{
if ( firstPtr->getData() > value ) // value is the smallest
insertAtFront( value );
else if ( lastPtr->getData() < value ) // value is the largest
insertAtBack( value );
else
{
ListNode< NODETYPE > *currentPtr = firstPtr->getNextPtr();
ListNode< NODETYPE > *previousPtr = firstPtr;
ListNode< NODETYPE > *newPtr = getNewNode( value );
while ( currentPtr != lastPtr && currentPtr->getData() < value )
{
previousPtr = currentPtr;
currentPtr = currentPtr->getNextPtr();
} // end while
previousPtr->setNextPtr( newPtr );
newPtr->setNextPtr( currentPtr );
} // end else
} // end else
} // end function insertInOrder
#endif
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "List2.h"
using namespace std;
template<class T>
int sumList(List <T> &listObject)
{
int sum = 0;
while (!listObject.isEmpty() )
{
}
return sum;
}
template<class T>
double aveList(List <T> &listObject)
{
int sum = 0;
double avg;
while (!listObject.isEmpty() )
{
}
avg = sum / 25;
return avg;
}
int main()
{
srand( time( 0 ) ); // randomize the random number generator
List2< int > intList;
// fill intList with 25 random numbers between 0 and 100
for (int a = 1; a <= 25; a++)
{
intList.insertAtBack(rand() %101);
}
int sum = sumList( intList ); // calculate sum
double average = aveList( intList ); // calculate average
cout << "The sum of the elements is: " << sum << '\n';
cout << "The average of the elements is: " << average << '\n';
system("pause");
return 0;
} // end main
Any tips or help would be appreciated. I feel like I'm on the right track though.