I'm having a problem posting the problem! I'll have this resolved soon.
OK now, I'm back!
The posting problem I was having earlier (at work) was due to the length of my program, the editor refused to load it! So now, I have decided to split the program into two so that it can be loaded on here.
I have two classes as shown in the program below: one specifies the structure of the node of a linked-list; the other defines the linked-list. The problem I am having concerns the statements (annotated with comments) in line 58 and line 59 in function main(). With only the statement in line 58, the program terminates properly; but once I add another insertLast() statement, as shown, in line 59, the program ends abruptly (pretty much "hanging-up"). The question I have is :what is really going on here?
//-------------- class linkedListType member functions' definitions ------------
template<class elemType>
bool linkedListType<elemType>::isEmptyList()
{
return(first == NULL);
}
template<class elemType>
linkedListType<elemType>::linkedListType() //default constructor
{
count = 0;
first = NULL;
last = NULL;
}
template<class elemType>
void linkedListType<elemType>::destroyList()
{
nodeType<elemType> *temp; //pointer to deallocate the memory occupied by the node
while(first != NULL) //while there are nodes in the list
{
temp = first; //set temp to the current node
first = first->getLink(); //advance first to the next node
delete temp; //deallocate the memory occupied by temp
}
last = NULL; //initialize last to NULL; first has already been set to NULL
//by the while loop
count = 0;
}
template<class elemType>
void linkedListType<elemType>::initializeList()
{
destroyList(); //if the list has any nodes, delete them
}
template<class elemType>
void linkedListType<elemType>::print()
{
nodeType<elemType> *current; //pointer to traverse the list
current = first; //set current so that it points to the first node
while (current != NULL) //while there is more data to print
{
cout<<current->getInfo()<<" "; //forced the overloading of the << operator for extPersonType objects
current = current->getLink(); //advance pointer to the next node (mine!)
}
}
template<class elemType>
int linkedListType<elemType>::length() const
{
return count;
}
//destructor
template<class elemType>
linkedListType<elemType>::~linkedListType()
{
destroyList();
}
//copy the list
template<class elemType>
void linkedListType<elemType>::copyList(const linkedListType<elemType>& otherList)
{
nodeType<elemType> *newNode; //pointer to create a node
nodeType<elemType> *current; //pointer to traverse the list
if (first != NULL) //if the list is nonempty, make it empty; DSUC++
destroyList();
if (otherList.first == NULL) //otherList is empty
{
first = NULL;
last = NULL;
count = 0; //DSUC++
}
else
{
current = otherList.first; //current points to the list to be copied
count = otherList.count; //DSUC++
//copy the first node
first = new nodeType<elemType>; //create the (first (MINE)) node
first->info = current->info; //copy the info
first->link = NULL; //set the link field of the node to NULL
last = first; //make last point to the first node (now both point to the same node (MINE))
current = current->link; //make current point to the next node
//copy the remaining list
while (current != NULL)
{
newNode = new nodeType<elemType>; //create a node
newNode->info = current->info; //copy the info
newNode->link = NULL; //set the link of newNode to NULL
last->link = newNode; //attach newNode after last
last = newNode; //make last point to the actual last node
current = current->link; //make current point to the next node (still of object otherList (MINE))
}// end while
}//end else
}
//copy constructor
template<class elemType>
linkedListType<elemType>::linkedListType(const linkedListType<elemType>& otherList)
{
first = NULL;
copyList(otherList);
}
//overloading the assignment operator
template<class elemType>
const linkedListType<elemType>& linkedListType<elemType>::operator=(const linkedListType<elemType>& otherList)
{
if (this != &otherList) //avoid self-copy
{
copyList(otherList);
}
return *this;
}
template<class elemType>
void linkedListType<elemType>::insertLast(const elemType& newItem)
{
nodeType<elemType> *newNode; //pointer to create the new node
newNode = new nodeType<elemType>; //create the new node
newNode->setInfo(newItem); //store the new item in the node
newNode->setLink(NULL); //set the link field of newNode to NULL
if (this->first == NULL) //if the list is empty, ...
{
this->first = newNode; //...newNode is both the first and
this->last = newNode; //...last node
this->count++; //increment count
}
else //the list is not empty, insert newNode after last
{
this->last->setLink(newNode); //insert newNode after last
this->last = newNode; //make last point to the actual last node in the list
this->count++; //increment count
}
}
The easiest is to set a breakpoint in the destroyList function and step through it.
One problem I found was
1 2 3 4 5 6
while (first != NULL) //while there are nodes in the list
{
temp = first; //set temp to the current node
first = first->getLink (); //advance first to the next node
delete temp; //deallocate the memory occupied by temp
}
The second time it was called first->getLink() returned an invalid pointer and when it tried to delete it it crashed.