I tried to implement a singly linked list for my homework. Everything seemed to work perfectly until I tried it with linked list of linked lists, it failed to free the linked list inside and left some leaks. What I've done wrong? Some say I just ignore it the OS will clean up for me, but I really don't want to leave leaks. Everything else seems ok, so I only post the destructor part.
template<typename T>
class linkedList
{
protected:
node<T>* pHead;
node<T>* pTail;
int size;
public:
linkedList() : pHead(nullptr), size(0), pTail(NULL) {};
~linkedList() { this->cleanup(); };
void cleanup()
{
while (pHead) {
node<T>* p = pHead;
pHead = pHead->pNext;
delete p;
}
};
I have tried it with different data types, and it works ok, but when I tried
auto l = new linkedList<linkedList<int>*>;
it failed to free the linked lists inside. What is wrong with this? I need to use pointer because sometimes I need to know if it is NULL or not.