This is my first post so bear with me. I have a program that is an Array of Linked Lists that I'm mostly done with except for one thing, I get a segmentation fault in my Remove and Remove Help Functions. I only get segmentation Fault when I try to remove a Node that is the "head" of the Linked List in the Array. Is there any way to fix this without creating a Head?
I'm also a Student so sorry if I got some terms wrong.
table.h
file:
#pragma once
#include "website.h"
#include <fstream>
class Table
{
public:
Table();
~Table();
void insert (const Website& aWebsite);
bool retrieve (char * topic_keyword,
Website all_matches [], int& num_found);
bool edit (char * newReview, int newRating, char * topic_keyword);
bool remove ();
int getSize () const;
void print (ostream& out) const;
void loadFile (const char * fileName);
private:
struct Node
{
Node(const Website& aWebsite)
{
data = new Website(aWebsite);
next = nullptr;
}
~Node()
{
delete data;
}
Website * data;
Node * next;
};
static const int CAP = 5;
Node ** aTable;
int size;
int hashFunction (const char * key) const;
void destroy (Node **& myTable);
void destroy (Node *& first);
void print (ostream& out, Node * first,
int currPosition) const;
bool removeHelp (Node * curr);
};
table.cpp
file:
#include "table.h"
Table::Table() : size(0)
{
aTable = new Node * [CAP];
for(int index = 0; index < CAP; index++)
aTable[index] = nullptr;
}
Table::~Table()
{
if(aTable)
destroy(this->aTable);
}
void Table::destroy (Node **& myTable)
{
for(int index = 0; index < CAP; index++)
{
destroy(myTable[index]);
}
delete [] myTable;
}
void Table::destroy (Node *& first)
{
if(first)
{
destroy(first->next);
delete first;
}
}
int Table::hashFunction (const char * key) const
{
int total = 0;
const char * ptr = key;
while( *ptr != '\0')
{
total += * ptr;
ptr++;
}
return total % CAP;
}
void Table::insert (const Website& aWebsite)
{
int index = hashFunction(aWebsite.getWebsiteName());
Node * newNode = new Node(aWebsite);
newNode->next = aTable[index];
aTable[index] = newNode;
size++;
}
bool Table::retrieve
(char * topic_keyword, Website all_matches[], int& num_found)
{
int index = hashFunction(topic_keyword);
Node * curr = aTable[index];
while(curr && strcmp(topic_keyword, curr->data->getWebsiteName()) != 0 )
{
curr = curr->next;
}
if(!curr)
return false;
all_matches[index] = *(curr->data);
num_found++;
return true;
}
bool Table::edit (char * newReview, int newRating, char * topic_keyword)
{
int index = hashFunction(topic_keyword);
Node * curr = aTable[index];
while(curr && strcmp(topic_keyword, curr->data->getWebsiteName())!=0)
{
curr = curr->next;
}
if(!curr)
return false;
curr->data->setWebsiteReview(newReview);
curr->data->setWebsiteRating(newRating);
return true;
}
bool Table::remove ()
{
for(int index = 0; index < CAP; index++)
{
removeHelp(aTable[index]);
}
return true;
}
bool Table::removeHelp (Node * curr)
{
Node * prev = nullptr;
while(curr && (curr->data->getWebsiteRating()) != 1)
{
prev = curr;
curr = curr->next;
}
if(!curr)
{
return false;
}
else
{
if(!prev)
{
Node * temp = curr;
curr = curr->next;
delete temp;
}
else{
prev->next = curr->next;
delete curr;
}
}
return true;
}
int Table::getSize () const
{
return size;
}
void Table::print (ostream& out) const
{
for(int index = 0; index < CAP; index++)
{
out << "Chain # " << index << "..." << endl;
out << endl;
print(out, aTable[index], 0);
}
}
void Table::print (ostream& out, Node * currFirst, int currPosition) const
{
if(currFirst)
{
out << *(currFirst->data) << endl;
print(out, currFirst->next, ++currPosition);
}
}
void Table::loadFile (const char * fileName)
{
ifstream in;
const int MAX_CHAR = 101;
char name [MAX_CHAR];
char address [MAX_CHAR];
char summary [MAX_CHAR];
char review [MAX_CHAR];
int rating;
Website aWebsite;
in.open(fileName);
if(!in)
{
cerr << "Failed to Open!" << endl;
exit(1);
}
in.get(name, MAX_CHAR, ';');
while(!in.eof())
{
in.get();
in.get(address, MAX_CHAR, ';');
in.get();
in.get(summary, MAX_CHAR, ';');
in.get();
in.get(review, MAX_CHAR, ';');
in.get();
in >> rating;
in.get();
aWebsite.setWebsiteName (name);
aWebsite.setWebsiteAddress (address);
aWebsite.setWebsiteSummary (summary);
aWebsite.setWebsiteReview (review);
aWebsite.setWebsiteRating (rating);
this->insert(aWebsite);
in.get();
in.get(name, MAX_CHAR, ';');
}
in.close();
}
ostream& operator << (ostream& out, const Table& aTable)
{
aTable.print(out);
return out;
}
Visual:
Node * [] -> Array
index [Pointer * ] -> [ Node *]
index [Pointer * ] -> [ Node *]
index [Pointer * ] -> [ Node *]