it compiles and runs, but in one moment it gives me an unhandled exception. I tracked the problem till i found it. it is the line in bold in my code. Whats wrong with it?
"LinkedList.h"
Code:
#include <iostream>
using namespace std;
template<typename T>
struct LinkedNode
{
T data;
LinkedNode* next;
LinkedNode* prev;
};
template<typename T>
class LinkedList
{
public:
LinkedList();
~LinkedList();
LinkedList(const LinkedList& rhs);
LinkedList& operator=(const LinkedList& rhs);
bool isEmpty();
LinkedNode<T>* getFirst();
LinkedNode<T>* getLast();
void insertFirst(T data);
void insertLast(T data);
bool insertAfter(T tKey, T tData);
void removeFirst();
void removeLast();
bool remove(T removalCandidate);
void destroy();
private:
LinkedNode<T>* mFirst;
LinkedNode<T>* mLast;
};
template<typename T>
LinkedList<T>::LinkedList()
: mFirst(), mLast()
{
}
template<typename T>
LinkedList<T>::~LinkedList()
{
destroy();
}
template<typename T>
LinkedList<T>::LinkedList(const LinkedList &rhs)
{
destroy();
*this = rhs;
}
template<typename T>
LinkedList<T>& LinkedList<T>::operator =(const LinkedList<T> &rhs)
{
if(this = &rhs)
return *this;
destroy();
mFirst = rhs.mFirst;
mLast = rhs.mLast;
return this*;
}
template<typename T>
bool LinkedList<T>::isEmpty()
{
if(mFirst == 0)
return true;
else
return false;
}
template<typename T>
LinkedNode<T>* LinkedList<T>::getFirst()
{
return mFirst;
}
template<typename T>
LinkedNode<T>* LinkedList<T>::getLast()
{
return mLast;
}
template<typename T>
void LinkedList<T>::insertFirst(T tData)
{
LinkedNode<T>* newNode = new LinkedNode<T>();
newNode->data = tData;
if(isEmpty())
mLast = newNode;
else
mFirst->prev = newNode;
newNode->next = mFirst;
mFirst = newNode;
}
template<typename T>
void LinkedList<T>::insertLast(T tData)
{
LinkedNode<T>* newNode = new LinkedNode<T>();
newNode->data = tData;
if(isEmpty())
mFirst = newNode;
else
mLast->next = newNode;
newNode ->prev = mLast;
newNode ->next = 0;
mLast = newNode;
}
template<typename T>
bool LinkedList<T>::insertAfter(T tKey, T tData)
{
if(isEmpty())
return false;
LinkedNode<T>* current = mFirst;
while(current->data != tKey)
{
current = current->next;
if(current == 0)
return false;
}
LinkedNode<T>* newNode = new LinkedNode<T>();
newNode->data = tData;
if(current == mLast)
{
newNode->next = 0;
mLast = newNode;
}
else
{
newNode->next = current->next;
current->next->prev = newNode;
}
newNode->prev = current;
current->next = newNode;
return true;
}
template<typename T>
void LinkedList<T>::removeFirst()
{
LinkedNode<T>* oldNode = mFirst;
mFirst = mFirst->next;
delete oldNode;
oldNode = 0;
}
template<typename T>
void LinkedList<T>::removeLast()
{
LinkedNode<T>* oldNode = mLast;
delete oldNode;
mLast = mLast->prev;
}
template<typename T>
bool LinkedList<T>::remove(T removalCandidate)
{
if(isEmpty())
return false;
LinkedNode<T>* current = mFirst;
while(current->data != removalCandidate)
{
current = current->next;
if(current == 0)
return false;
}
if(current == mLast)
{
current->prev->next = 0;
mLast = current->prev;
}
else
{
current->prev->next = current->next;
current->next->prev = current->prev;
}
delete current;
current = 0;
return true;
}
template<typename T>
void LinkedList<T>::destroy()
{
if(mFirst != 0)
{
LinkedNode<T>* current = mFirst;
while(current != 0)
{
LinkedNode<T>* oldNode = current;
current = current->next;
delete oldNode;
oldNode = 0;
}
}
}
"Main.cpp"
Code:
#include "LinkedList.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
LinkedList<int> myIntList;
// Insert to the front of the list.
myIntList.insertFirst(4);
myIntList.insertFirst(3);
myIntList.insertFirst(2);
myIntList.insertFirst(1);
// Insert to the back of the list.
myIntList.insertLast(5);
myIntList.insertLast(7);
myIntList.insertLast(8);
myIntList.insertLast(9);
// Forgot to add 6 to the list, insert before 7. But first
// we must get an iterator that refers to the position
// we want to insert 6.
cout << "Before deletion..." << endl;
myIntList.insertAfter(5, 6);
// Print the list to the console window.
for(LinkedNode<int>* i = myIntList.getFirst(); i != myIntList.getLast()->next; i = i->next)
cout << i->data << " ";
cout << endl;
// Remove the first node in the list.
myIntList.removeFirst();
// Remove the last node in the list.
myIntList.removeLast();
// Remove the node that has a value of 5
myIntList.remove( 1 );
// Print the list to the console window.
cout << "After deletion..." << endl;
for(LinkedNode<int>* i = myIntList.getFirst(); i != myIntList.getLast()->next; i = i->next)
cout << i->data << " ";
cout << endl;
system("PAUSE");
}
This code is supposed to simulate the std::list class.