So I have a linked list of a class, and I am trying to delete one of the nodes, but when it reaches that line of code, the program freezes and crashes.
Here is the linked list classes I am using:
template <class Type>
struct nodeType {
Type info;
nodeType<Type>* link;
};
template <class Type>
class linkedListIterator {
public:
linkedListIterator();
linkedListIterator(nodeType<Type>* ptr);
Type operator*();
linkedListIterator<Type> operator++();
bool operator==(const linkedListIterator<Type>& right) const;
bool operator!=(const linkedListIterator<Type>& right) const;
private:
nodeType<Type>* current;
};
template <class Type>
class linkedListType {
public:
const linkedListType<Type>& operator=(const linkedListType<Type>&);
void initializeList();
bool isEmptyList() const;
void print() const;
int length() const;
void destroyList();
Type front() const;
Type back() const;
virtual bool search(const Type& searchItem) const = 0;
virtual void insertFirst(const Type& newItem) = 0;
virtual void insertLast(const Type& newItem) = 0;
virtual void deleteNode(const Type& deleteItem) = 0;
linkedListIterator<Type> begin();
linkedListIterator<Type> end();
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();
protected:
int count;
nodeType<Type>* first;
nodeType<Type>* last;
private:
void copyList(const linkedListType<Type>& otherList);
};
template <class Type>
class orderedLinkedList : public linkedListType<Type> {
using linkedListType<Type>::first;
using linkedListType<Type>::last;
using linkedListType<Type>::count;
public:
bool search(const Type& searchItem) const;
void insert(const Type& newItem);
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
};
This is the function I am calling to delete the node:
template <class Type>
void orderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
nodeType<Type>* current; //pointer to traverse the list
nodeType<Type>* trailCurrent; //pointer just before current
bool found;
if (first == nullptr) //Case 1
cout << "Cannot delete from an empty list." << endl;
else
{
current = first;
found = false;
while (current != nullptr && !found) //search the list
if (current->info >= deleteItem)
found = true;
else
{
trailCurrent = current;
current = current->link;
}
if (current == nullptr) //Case 4
cout << "The item to be deleted is not in the "
<< "list." << endl;
else
if (current->info == deleteItem) //the item to be
//deleted is in the list
{
if (first == current) //Case 2
{
first = first->link;
if (first == nullptr)
last = nullptr;
delete current;
}
else //Case 3
{
trailCurrent->link = current->link;
if (current == last)
last = trailCurrent;
delete current;
}
count--;
}
else //Case 4
cout << "The item to be deleted is not in the "
<< "list." << endl;
}
}//end deleteNode
And here is where I call the deleteNode function:
void deleteBook(orderedLinkedList<bookType> books)
{
char choice = '\0';
char ans = '\0';
string title = "Delete Book";
int len = title.length();
int bookCount = bookType::getBookCount();
do {
system("cls");
cout << setfill(' ');
cout << setw((SIZE_MENUS / 2) + LEN_TITLE / 2) << right << TITLE << endl;
cout << setw((SIZE_MENUS / 2) + len / 2) << right << title << endl;
cout << endl;
cout << setw(SIZE_MENUS / 3) << "" << "Current Book Count: " << bookCount << endl;
cout << endl;
linkedListIterator<bookType> current = lookUpBook(books);
if (current != nullptr)
{
cout << setfill(' ');
cout << setw(SIZE_MENUS / 4) << "" << "Is this the book you would like to delete? (Y/N): ";
cin >> ans;
cin.ignore(100, '\n');
cout << endl;
while (ans != 'Y' && ans != 'y' && ans != 'N' && ans != 'n')
{
cout << setw(SIZE_MENUS / 4) << "" << "Please enter Y or N." << endl;
cout << setw(SIZE_MENUS / 4) << "" << "Is this the book you would like to delete? (Y/N): ";
cin >> ans;
cin.ignore(100, '\n');
cout << endl;
}
if (ans == 'Y' || ans == 'y')
{
cout << "found" << endl;
books.deleteNode(*current);
bookType::decBookCount();
bookCount = bookType::getBookCount();
cout << setw(SIZE_MENUS / 4) << "" << "Book Deleted." << endl;
cout << setw(SIZE_MENUS / 4) << "";
system("pause");
cout << endl;
cout << setw(SIZE_MENUS / 4) << "" << "Would you like to delete another book? (Y/N): ";
cin >> ans;
cin.ignore(100, '\n');
cout << endl;
if (ans == 'Y' || ans == 'y')
{
cout << endl;
}
if (ans == 'N' || ans == 'n')
{
break;
}
else {
cout << setw(SIZE_MENUS / 3) << "" << "Please enter Y or N." << endl;
cout << setw(SIZE_MENUS / 3) << "" << "Would you like to delete another book? (Y/N): ";
cin >> ans;
cin.ignore(100, '\n');
cout << endl;
}
}
else {
break;
}
}
else {
break;
}
} while (choice != 'n' && choice != 'N');
}
So in my deleteBook function, I search for a book in the lookUpBook function (not shows as it seems to be working perfectly), and it returns a linkedListIterator, which I store into current. I then allow the user to choose whether or not to delete the book. If they say 'Y' or 'y' for yes, it prints out a "found" statement (seen in deleteBook as a check to see if it enters the print statement), and then the program crashes.