Hello,
Was wodering if someone could give me some insight on templates.
The code that is between the **** ****. If I swap the two lines , will it change anything? I think the list is not empty here. This is inserting a node at the front of the list. The new node points to what use to be the first nodeof the list,and copies newPtr to firstPtr so that first firstPtr now points to the new first node of the list. Are they not enterchangable? Or is this a big problem?
What if this was inserting at the back of the list?
''''''''lastPtr->nextPtr = newPtr;''''''''
'''''''lastPtr = newPtr;'''''''
Same differencece, except lastPtr now points to the new last node of the list?

Next question refers to the line ^^^^^delete tempPtrt^^^^.
If I remove it, will the program not be able to reallocate the memory. Or Am I off base, And what would this cause?

If I delete the line:##### value = tempPtr->data;######
This is just removing a noded frrom the front of the list. Is this a memory issue also?

+++++currentPtr->nextPtr = 0;++++++++++. If I remove this line what would happen? I know this sets the nextPtr to 0 in the last node of the list. Will this cause a printing problem?ie, print past the end of the list.

If I swap these 2 lines of code, I haven't the foggiest.
~~~ cout << currentPtr->data << ' ';~~~
~~~currentPtr = currentPtr->nextPtr;~~~

Thanks for your troubles.
Sam T.


#ifndef LIST_H
#define LIST_H

#include <iostream>
#include <cassert>
#include "listnd.h"

using std::cout;

template< class NODETYPE >
class List {
public:
List();
~List();
void insertAtFront( const NODETYPE & );
void insertAtBack( const NODETYPE & );
bool removeFromFront( NODETYPE & );
bool removeFromBack( NODETYPE & );
bool isEmpty() const;
void print() const;
private:
ListNode< NODETYPE > *firstPtr;
ListNode< NODETYPE > *lastPtr;

ListNode< NODETYPE > *getNewNode( const NODETYPE & );
};

template< class NODETYPE >
List< NODETYPE >::List() : firstPtr( 0 ), lastPtr( 0 ) { }

template< class NODETYPE >
List< NODETYPE >::~List()
{
if ( !isEmpty() ) {
cout << "Destroying nodes ...\n";

ListNode< NODETYPE > *currentPtr = firstPtr, *tempPtr;

while ( currentPtr != 0 ) {
tempPtr = currentPtr;
cout << tempPtr->data << '\n';
currentPtr = currentPtr->nextPtr;
^^^^^ delete tempPtr;^^^^^^^
}
}

cout << "All nodes destroyed\n\n";
}

template< class NODETYPE >
void List< NODETYPE >::insertAtFront( const NODETYPE &value )
{
ListNode< NODETYPE > *newPtr = getNewNode( value );

if ( isEmpty() )
firstPtr = lastPtr = newPtr;
else {
**** newPtr->nextPtr = firstPtr;****
****firstPtr = newPtr;****
}
}

template< class NODETYPE >
void List< NODETYPE >::insertAtBack( const NODETYPE &value )
{
ListNode< NODETYPE > *newPtr = getNewNode( value );

if ( isEmpty() )
firstPtr = lastPtr = newPtr;
else {
''''''''lastPtr->nextPtr = newPtr;''''''''
'''''''lastPtr = newPtr;'''''''
}
}

template< class NODETYPE >
bool List< NODETYPE >::removeFromFront( NODETYPE &value )
{
if ( isEmpty() )
return false;
else {
ListNode< NODETYPE > *tempPtr = firstPtr;

if ( firstPtr == lastPtr )
firstPtr = lastPtr = 0;
else
firstPtr = firstPtr->nextPtr;

##### value = tempPtr->data;######
delete tempPtr;
return true;
}
}

template< class NODETYPE >
bool List< NODETYPE >::removeFromBack( NODETYPE &value )
{
if ( isEmpty() )
return false;
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;++++++++++
}

value = tempPtr->data;
delete tempPtr;
return true;
}
}

template< class NODETYPE >
bool List< NODETYPE >::isEmpty() const
{ return firstPtr == 0; }

template< class NODETYPE >
ListNode< NODETYPE > *List< NODETYPE >::getNewNode(
const NODETYPE &value )
{
ListNode< NODETYPE > *ptr =
new ListNode< NODETYPE >( value );
assert( ptr != 0 );
return ptr;
}

template< class NODETYPE >
void List< NODETYPE >:rint() const
{
if ( isEmpty() ) {
cout << "The list is empty\n\n";
return;
}

ListNode< NODETYPE > *currentPtr = firstPtr;

cout << "The list is: ";

while ( currentPtr != 0 ) {
~~~ cout << currentPtr->data << ' ';~~~
~~~currentPtr = currentPtr->nextPtr;~~~
}

cout << "\n\n";
}

#endif