#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
template <typename T>
class Node //Node Class
{
public:
T data; //Data container
Node *link; //points to the next element in the list
};
template <typename T>
class LinkedList //Singly Linked List ADT
{
private:
Node<T> *ptr, *headptr, *p1, *p2;
public:
LinkedList(); //default constructor
~LinkedList(); //default destructor
bool listIsEmpty(); //check to see if list is empty
};
template <typename T>
LinkedList<T>::LinkedList() //Linked List Constructor initiates everything to NULL
{
headptr = NULL;
ptr = NULL;
p1 = NULL;
p2 = NULL;
}
template <typename T>
bool LinkedList<T>::listIsEmpty()
{
if(headptr->link == NULL)
returntrue;
elsereturnfalse;
}
template <typename T>
~LinkedList<T>::LinkedList() //Linked List Destructor clears and deallocates everything in the list
{
ptr = headptr; //Start at the very beginning of the list
if(listIsEmpty()) //Check to see if the list is empty
{
//nothing contained in the list
}
else //List has something in it
{
while(headptr->link != NULL) //loop to delete every item in the list
{
ptr = headptr->link; //increment pointer
headptr->link = ptr->link; //skip over ptr's element
delete ptr; //delete ptr
}
}
}
#endif // LIST_H_INCLUDED
I am trying to implement a linked list and my compiler can't make it past this line. I don't know what I am doing wrong.
I will say that your destructor implementation is wrong; it will not clean up the entire list, but will rather
leave 1 node around.
I will also say that your LinkedList class is not assignable or copyable, but you haven't disabled these
operations (by declaring the copy constructor and assignment operator private and leaving them unimplemented).
If you do use these operations, your program *will* crash, but you haven't provided all your code to know.