I'm having trouble with a linked list. I keep getting a seg fault when I hit the del function. I'm not sure if my linked list is bad, or if my del function is bad... or if it's the destructor.
Any help is appreciated!!!
app.cpp:
Code:
#include <iostream>
#include "linkedlist.h"
using namespace std;
void find(LinkedList& list, char ch)
{
if (list.find(ch))
cout << "found ";
else
cout << "did not find ";
cout << ch << endl;
}
int main()
{
LinkedList list;
list.add('x');
list.add('y');
list.add('z');
cout << list;
find(list, 'y');
list.del('y');
cout << list;
find(list, 'y');
list.del('x');
cout << list;
find(list, 'y');
list.del('z');
cout << list;
find(list, 'y');
return 0;
}
linkedlist.h:
Code:
#ifndef _LINKED_LIST_#define _LINKED_LIST_
#include <ostream>
class LinkedList
{
public:
LinkedList();
~LinkedList();
void add(char ch);
bool find(char ch);
bool del(char ch);
friend std::ostream& operator<<(std::ostream& out, LinkedList& list);
private:
char letter; //spot to store our letter
LinkedList *next; //pointer to next list elemenent
};
#endif // _LINKED_LIST_
linkedlist.cpp:
Code:
#include "linkedlist.h"
using namespace std;
//default data constructor
LinkedList::LinkedList()
{
//initialize letters pointer to null
letter = '\n';
next = nullptr;
}
//default destructor
LinkedList::~LinkedList()
{
LinkedList *forward = nullptr;
LinkedList *current = this;
//iterate through list, deleting each element as we go
while (current != nullptr)
{
//set next pointer to current's next
forward = current->next;
delete current; //delete the current memory
current = forward; //reset current to next's pointer
}
}
//FUNCTION - add letter (single) to letters list
void LinkedList::add(char ch)
{
LinkedList *current = this;
LinkedList *prev = nullptr;
LinkedList *new_node = new LinkedList();
if (current == nullptr) //check if it's first item in the list
{
new_node->letter = ch;
new_node->next = nullptr;
current = new_node;
return;
}
while (current != nullptr)
{
prev = current;
current = current->next;
}
new_node->letter = ch;
new_node->next = nullptr;
prev->next = new_node;
return;
}
//FUNCTION - find a letter in our letters list
bool LinkedList::find(char ch)
{
LinkedList *temp = this;
while (temp != nullptr)
{
if (temp->letter == ch) //if ch is found in our list, return true
return true;
else
temp = temp->next;
}
return false; //if ch is not found
}
//FUNCTION - //delete a letter from our letters list
bool LinkedList::del(char ch)
{
LinkedList *current = this;
LinkedList *previous = nullptr;
if (current == nullptr) //if current is empty
return false;
if (current->letter == ch) //if the first spot is the correct spot to delete
{
previous = current;
current = current->next;
delete previous;
return true;
}
while (current->letter != ch && current != nullptr) //find spot to delete at
{
previous = current;
current = current->next;
}
if (current == nullptr) //it didn't find the ch
return false;
else
{
if (current->next == nullptr) //if at end of list
{
previous->next = nullptr;
delete current;
return true;
}
else
{
previous->next = current->next;
delete current;
return true;
}
}
}
//overload friend << operator
std::ostream& operator<<(std::ostream& out, LinkedList& list)
{
LinkedList *temp = &list;
while (temp != nullptr)
{
out << temp->letter;
temp = temp->next;
}
out << endl;
return out;
}
What am I doing wrong??
So far, with the code I have here, I get the first three letters added and printed out, then it hangs up on the del function it appears. I'm thinking it's something to do with the destructor...but I'm not sure.
I should also point out, I'm not to change anything in the main function. Only the linkedlist.cpp file and the private variables in the linkedlist.h file.