Help with doubly linked list
OK, so im fairly new to pointers and Node types, etc... I'm trying to use a function to insert an item (first item) in a list, but my program crashes whenever i try to set the front pointer to that.
I'm using a test driver that the first thing that does is call the function with the string "this is a test." So the list is empty before it calls the function.
PS: don't mind that it says sorted... i also cut a lot of the functions out of the code to not take up space... and only put the one im having troubles with at the moment.
Code:
template<class ItemType>
struct NodeType
{
ItemType info;
NodeType<ItemType>* next;
NodeType<ItemType>* back;
};
template <class ItemType>
class SortedType
{
public:
SortedType(); // Class constructor
SortedType(const SortedType& list);
// Constructor
~SortedType();
// Destructor
const SortedType& operator=(const SortedType& rhs);
bool IsFull() const;
int GetLength() const;
void MakeEmpty();
void RetrieveItem(ItemType& item, bool& found);
void ResetList();
void GetNextItem(ItemType&);
ItemType CurrentItem();
void Delete();
void InsertAfter(ItemType item);
void InsertBefore(ItemType item);
void GoToFront();
void GoToRear();
void GoForward();
void GoBack();
private:
void Copy(const SortedType& list);
void FindItem(ItemType item, NodeType<ItemType>*& location, bool& found);
NodeType<ItemType>* front;
NodeType<ItemType>* rear;
int length;
NodeType<ItemType>* currentPos;
};
template<class ItemType>
SortedType<ItemType>::SortedType() // Class constructor
{
length = 0;
front = NULL;
rear = NULL;
currentPos = NULL;
}
template<class ItemType>
void SortedType<ItemType>::InsertAfter(ItemType item)
{
NodeType<ItemType>* location;
location = new NodeType<ItemType>;
location->info = item;
if(currentPos == NULL)
currentPos = front;
if(currentPos == front)
{
location->next = NULL;
location->back = front;
front->next = location; //Right here is where it crashes.
front = location;
}
else
{
location->next = currentPos->next;
location->back = currentPos;
currentPos->next = location;
}
}