Salve, suddetto problema può essere dovuto a un quantitativo di memoria in eccesso da allocare?Il codice è questo:
list.h
//pre-dichiarazione
template < class T > class ListC;
template < class T >
class Node
{
friend class ListC<T>;
public:
Node( const T value ) : data ( value ), nextPtr (0), prevPtr (0) {}
private:
T data;
Node<T>* nextPtr;
Node<T>* prevPtr;
};
template < class T >
class ListC
{
public:
ListC(): firstPtr (0), lastPtr(0) {}
~ListC() { firstPtr = lastPtr = 0; }
bool isEmpty();
void insertAtFront ( const T );
void insertAtBack ( const T );
bool removeFromFront ( T& );
bool removeFromBack ( T& );
void printOrario() ;
void printAntiorario() ;
private:
Node<T>* firstPtr;
Node<T>* lastPtr;
//funzione di utility
Node<T>* getNewNode ( const T );
};
list.cpp
#include "list.h"
#include "exception.h"
#include <iostream>
using namespace std;
template < class T >
bool ListC<T> :: isEmpty()
{
return firstPtr == 0;
}
//funzione di utility per allocare un nuovo nodo
template < class T >
Node<T>* ListC<T> :: getNewNode ( const T value )
{
Node<T>* newPtr = new Node<T> ( value );
if ( newPtr == 0)
throw NoAllocation();
return newPtr;
}
template < class T >
void ListC<T> :: insertAtFront ( const T value )
{
Node<T>* newPtr;
try
{
newPtr = getNewNode ( value );
}
catch ( NoAllocation ex )
{
cout << "Exception occurred: " << ex.what() << endl;
}
Node<T>* tempPtr = firstPtr;
firstPtr = newPtr;
tempPtr -> prevPtr = firstPtr;
firstPtr -> nextPtr = tempPtr;
firstPtr -> prevPtr = lastPtr;
lastPtr -> nextPtr = firstPtr;
}
template < class T >
void ListC<T> :: insertAtBack ( const T value )
{
Node<T>* newPtr;
try
{
newPtr = getNewNode( value);
}
catch ( NoAllocation ex )
{
cout << "Exception occurred: " << ex.what() << endl;
}
Node<T>* tempPtr = lastPtr;
lastPtr = newPtr;
tempPtr -> nextPtr = lastPtr;
lastPtr -> nextPtr = firstPtr;
lastPtr -> prevPtr = tempPtr;
firstPtr -> prevPtr = lastPtr;
}
template < class T >
bool ListC<T> :: removeFromFront ( T& value )
{
if ( isEmpty())
return false;
Node<T>* ptr = firstPtr;
firstPtr = firstPtr -> nextPtr;
lastPtr -> nextPtr = firstPtr;
firstPtr -> prevPtr = lastPtr;
value = ptr -> data; // salviamo nella variabile value il dato eliminato
return true;
}
template < class T >
bool ListC<T> :: removeFromBack ( T& value )
{
if ( isEmpty())
return false;
Node<T>* ptr = lastPtr;
lastPtr = lastPtr -> prevPtr;
lastPtr -> nextPtr = firstPtr;
firstPtr -> prevPtr = lastPtr;
value = ptr -> data; // salviamo nella variabile value il dato eliminato
return true;
}
template < class T >
void ListC<T> :: printOrario()
{
Node<T>* currentPtr = firstPtr;
do
{
cout << currentPtr -> data;
currentPtr = currentPtr -> nextPtr;
} while ( currentPtr != firstPtr );
}
template < class T >
void ListC <T> :: printAntiorario()
{
Node<T>* currentPtr = lastPtr;
do
{
cout << currentPtr -> data;
currentPtr = currentPtr -> prevPtr;
} while ( currentPtr != lastPtr );
}
main.cpp
#include "list.h"
#include "exception.h"
#include "list.cpp"
using namespace std;
int main ()
{
char value1,value2;
ListC<char> l;
//inseriamo in una lista di char CIRCOLAR
l.insertAtBack ('C');
l.insertAtBack ('I');
l.insertAtBack ('R');
l.insertAtBack ('C');
l.insertAtBack ('O');
l.insertAtBack ('L');
l.insertAtBack ('A');
l.insertAtBack ('R');
cout << "La lista è: " ;
l.printOrario ();
cout << endl;
//Inseriamo la lettera E in coda alla lista per ottenere la parola CIRCOLARE
l.insertAtBack ( 'E');
cout << "La lista in senso orario è: ";
l.printOrario();
cout << "La lista in senso antiorario è: ";
l.printAntiorario();
//eliminiamo le lettere R ed E dalla lista per ottenere la parola CIRCOLA
l.removeFromBack(value1);
l.removeFromBack(value2);
cout << "Abbiamo eliminato le lettere " << value2 << value1 << " dalla lista per ottenere la parola ";
l.printOrario();
cout << "End of program...\n";
return 0;
}
exception.h
class NoAllocation
{
public:
NoAllocation() : message ( "No Allocation!" ) {}
const char* what() { return message; }
private:
const char* message;
};