Question: I keep getting memory leaks in my LinkedList class here are my files. //main.cpp #include #include #include #include LinkedList.h using namespace std; void TestAddHead(); void
I keep getting memory leaks in my LinkedList class here are my files.
//main.cpp
#include #include #include #include "LinkedList.h" using namespace std;
void TestAddHead(); void TestAddTail(); void TestAddingArrays();
int main() { int testNum; cin >> testNum; if (testNum == 1) TestAddHead(); else if (testNum == 2) TestAddTail(); else if (testNum == 3) TestAddingArrays();
return 0; }
void TestAddHead() { cout << "=====Testing AddHead() functionality====" << endl; LinkedList data; for (int i = 0; i < 12; i += 2) data.AddHead(i); cout << "Node count: " << data.NodeCount() << endl; cout << "Print list forward:" << endl; data.PrintForward(); cout << "Print list in reverse:" << endl; data.PrintReverse(); }
void TestAddTail() { cout << "=====Testing AddTail() functionality====" << endl; LinkedList data; for (int i = 0; i <= 21; i += 3) data.AddTail(i); cout << "Node count: " << data.NodeCount() << endl; cout << "Print list forward:" << endl; data.PrintForward(); cout << "Print list in reverse:" << endl; data.PrintReverse(); }
void TestAddingArrays() { cout << "=====Testing AddNodesHead() and AddNodesTail() =====" << endl;
string values[5]; values[0] = "*"; values[1] = "**"; values[2] = "***"; values[3] = "****"; values[4] = "*****";
LinkedList list; list.AddHead("**"); list.AddHead("***"); list.AddHead("****"); list.AddHead("*****"); list.AddHead("******"); list.AddHead("*******"); list.AddHead("********"); list.AddHead("*********"); list.AddHead("********"); list.AddHead("*******"); list.AddHead("******");
list.AddNodesHead(values, 5); list.AddNodesTail(values, 5); list.PrintForward(); }
//LinkedList.h
#pragma once #include using namespace std; #ifndef LinkedList_h #define LinkedList_h template
class LinkedList{ public: struct Node{ T data; Node* next; Node* prev; }; void PrintForward() const; void PrintReverse() const; unsigned int NodeCount() const; void AddHead(const T &data); void AddTail(const T &data); LinkedList(); LinkedList(const LinkedList &list); ~LinkedList(); void AddNodesHead(const T* data, unsigned int count); void AddNodesTail(const T* data, unsigned int count); void Clear(); private:
Node* head = new Node; Node* tail = new Node; unsigned int count;
}; template LinkedList::LinkedList(){ head = nullptr; tail = nullptr; count = 0; }
template LinkedList::LinkedList(const LinkedList &list){
this->head = list.head; this->tail = list.tail; this->count = list.count; }
template LinkedList::~LinkedList(){ Clear(); }
template void LinkedList::AddHead(const T& data){ Node* temp = new Node; //Node* temp = nullptr; temp->data=data; temp->next=nullptr; temp->prev=nullptr; if(head==nullptr){ head=temp; tail=temp; } else{ temp->next=head; head->prev=temp; head=temp; } //delete temp; } template void LinkedList::AddTail(const T& data){
Node* temp = new Node; //Node* temp = nullptr; temp->data = data; temp->next = nullptr; temp->prev = nullptr;
if(tail==nullptr){ head = temp; } else{ tail->next = temp; temp->prev = tail; } tail = temp; //delete temp; } template unsigned int LinkedList::NodeCount() const{ Node* temp = head; int count = 0;
while(temp != nullptr){ ++count; temp = temp->next; } return count; }
template void LinkedList::PrintForward() const{ Node* temp = head; while(temp != nullptr){ cout<data <next; } }
template void LinkedList::PrintReverse() const{ Node* temp = tail; while(temp != nullptr){ cout<data <prev; } } template void LinkedList::AddNodesHead(const T* data, unsigned int count) { //for (unsigned int i = 0; i <= count-1; i++) for(unsigned int i=count-1; i>=1; i--) { AddHead(data[i]); //count++; } } template void LinkedList::AddNodesTail(const T* data, unsigned int count) { for (unsigned int i = 0; i <= count-1; i++) { AddTail(data[i]); //count++; } } template void LinkedList::Clear(){ Node* temp = head; Node* nextN=nullptr; while(temp!=nullptr){ nextN = temp-> next; delete temp; temp = nextN; } temp=nullptr; nextN=nullptr; /* Node* newNode = tail; Node* nextNode; while(newNode != nullptr){ nextNode = newNode->prev; delete newNode; newNode=nextNode; } newNode=nullptr; nextNode=nullptr; */ }
#endif