Linked List Destructor Help
When running valgrind on my program, it is not showing any memory leaks, however I am still getting the error saying an invalid read of ... .
I'm not entirely sure what it means. I think it has to do with my linked list's destructor / destroy() function.
Here's what I have so far. What am I missing?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
#include "linkedlist.h"
#include "iostream"
using namespace std;
//Default Constructor
LinkedList::LinkedList()
{
//initialize letters pointer to null
letter = '\n';
next = nullptr;
head = nullptr;
}
LinkedList::~LinkedList()
{
destroy(head);
}
void LinkedList::destroy(LinkedList * currHead)
{
LinkedList *current = currHead;
while (current != nullptr)
{
current = current->next;
}
delete current;
}
void LinkedList::add(char ch)
{
LinkedList *current = head;
if (current == nullptr)
{
LinkedList *newNode = new LinkedList();
newNode->letter = ch;
newNode->next = nullptr;
head = newNode;
return;
}
else if (current->next == nullptr)
{
LinkedList *newNode = new LinkedList();
current->next = newNode;
newNode->letter = ch;
newNode->next = nullptr;
newNode->head = newNode;
return;
}
else
{
current = current->next;
return current->add(ch);
}
}
bool LinkedList::find(char ch)
{
LinkedList *temp = head;
if (temp == nullptr) //if list is empty, return false
{
return false;
}
else if (temp->letter == ch) //if it finds the letter, return true
{
return true;
}
else if (temp->next == nullptr) //if the next spot is empty, return false
{
return false;
}
else
{
return temp->next->find(ch); //recursively call find(ch) function
}
}
bool LinkedList::del(char ch)
{
LinkedList *current = head;
LinkedList *previous = nullptr;
if (current == nullptr) //if current list is empty
return false;
else if (current->letter == ch) //if it finds the letter to delete
{
previous = current;
current = current->next;
head = current;
delete previous;
return true;
}
else if (current->next == nullptr) //if next spot is blank, return false
{
return false;
}
else
{
return current->next->del(ch); //recursively call del(ch) function
}
}
//overload friend << operator
std::ostream& operator<<(std::ostream& out, LinkedList& list)
{
LinkedList *current = list.head;
while (current != nullptr)
{
out << (current->letter);
current = current->next;
}
out << endl;
return out;
}
| |
Last edited on
Give us the header file implementation
1 2 3 4 5 6 7 8 9 10
|
void LinkedList::destroy(LinkedList * currHead)
{
LinkedList *current = currHead;
while (current != nullptr)
{
LinkedList *tmp = current;
current = current->next;
delete tmp;
}
}
| |
Ah yeah I didn't even see that ^
Last edited on
There's no point passing
head into destroy.
And
head should be set to nullptr at the end.
1 2 3 4 5 6 7 8 9
|
void LinkedList::destroy()
{
while (head)
{
LinkedList* to_del = head;
head = head->next;
delete to_del;
}
}
| |
Topic archived. No new replies allowed.