So this is a CS lab that we're doing and my code passes all the tests as far as removing, inserting, clearing, and all that. However afterward it fails due to memory leaks. The Valgrind result points to where I'm initializing new nodes in my program but I'm setting them null at the end. Is that not what you must do to "clear up" the memory? I did Java in high school so all this memory management is new to me.
Here's my LinkedList.cpp
Code:
#include "LinkedList.h"
#include "Node.h"
#include <stdlib.h>
using namespace std;
void LinkedList::insertHead(int value)
{
/*ONEBLOCKLOST*/
//If value is negative,exit
if (value < 0) {
return;
}
(int x = 0;
x < size();
x++) {
if (this->at(x) == value) {
return;
}
}
Node *tmp = new Node(value);
//If there is no head, make this node the head
if (head == NULL) {
head = tmp;
}
//Otherwise,point this node to the head, making this node the new head
else {
tmp->next = head;
head = tmp;
}
tmp = NULL;
}
void LinkedList::insertTail(int value)
{
//If a negative number is being given, exit
if (value < 0) {
return;
}
//Iterate through the list to see if the value already exists in the list
for (int x = 0; x < size(); x++) {
if (this->at(x) == value) {
return;
}
}
//Construct a new Node
new_Node = new Node(value);
//If the list is empty, make this node the head
if (head == NULL) {
head = new_Node;
}
//Otherwise
else {
spot = head;
//Iterate through the list until you reach the end
while (spot->next != NULL) {
spot = spot->next;
}
//Append the new node to the end
spot->next = new_Node;
}
//Clear and remove the new_Node variable
new_Node = NULL;
}
void LinkedList::insertAfter(int value, int insertionNode)
{
//If value is negative,exit
if (value < 0 || insertionNode < 0) {
return;
}
//Iteratethroughthelisttoseeifthevaluealreadyexistsinthelist
for (int x = 0; x < size(); x++) {
if (this->at(x) == value) {
return; //Exit if it does
}
}
bool exists = false;
for (int x = 0; x < size(); x++) {
if (this->at(x) == insertionNode) {
exists = true;
}
}
if (exists == false)
return;
//Construct a new node
new_Node = new Node(value);
spot = head;
//Iteratethroughthelistuntilyoureachanodewho'svalueisequaltoinsertionNode
while (spot->value != insertionNode) {
spot = spot->next;
}
if (spot->next != NULL) {
new_Node->next = spot->next;
}
Node *tmp = spot->next;
spot->next = new_Node;
new_Node->next = tmp;
//Clear and remove the new_Node variable
new_Node = NULL;
tmp = NULL;
}
void LinkedList::remove(int value)
{
//Make sure the array is not empty or that the value is not negative
if (head == NULL || value < 0) {
return;
}
//Makesurethenodeexistsinthelist
bool exists = false;
for (intx = 0; x < size(); x++) {
if (this->at(x) == value) {
exists = true;
}
}
if (exists == false)
return;
//Iftheheadistheonebeingremoved,simplyre-allocatetheheadasthenextnode
else if (head->value == value) {
head = head->next;
} else {
spot = head;
//Start at the beginning
while (spot->next->value != value) {
spot = spot->next;
}
Node *tmp = spot->next;
spot->next = spot->next->next;
//Just delete the node
delete tmp;
}
}
void LinkedList::clear()
{
while (head != NULL) {
remove(head->value);
}
}
int LinkedList::at(int index)
{
if (index < 0 || index > size()) {
return -1;
}
//Otherwiseiteratethroughtheloopn#oftimes
spot = head;
for (int n = 0; n < index; n++) {
spot = spot->next;
}
//Ifwereachanullpointintthelist,
if (spot == NULL) {
return -1;
}
//Otherwisereturntherespectivevalue
else
return spot->value;
}
int LinkedList::size()
{
int n = 0;
spot = head; //Start at the beginning
while (spot != NULL) {
//Iterate through the list
spot = spot->next;
n++;
}
//n=n+1;//ClearanddeletethespotNodevariable
return n;
}
LinkedList::~LinkedList()
{
//Calltheclearfunction
clear();
}
Thanks in advance!