I am a beginner in programming. I am writing a sorted linked list in C++, I added three new functions, removeMiddleNode(), findWordStartWith(), and displayBackwards(). I built the solution and it says successful, however when I run the program everything also runs perfectly fine but it takes me to another file name xstring.cpp and reads "Exception thrown: read access violation. this was nullptr.". I do not understand what is wrong with my code, I would be grateful if someone could help/inform me on what this meant. It is my first-time learning C++ and writing on Visual Studio. Thank you. Below I have included my .h and .cpp file as well as my main.
HEADER FILE
#include <iostream>
#include <string>
using namespace std;
//EXCULSIVE TO DATATYPE STRING
class node
{
public:
string data;
node* next;
};
class sortedRevLLStr
{
private:
node* listData;
int length;
node* currentPos;
public:
sortedRevLLStr(); //No arg constructor
void makeEmpty();
bool isFull();
int getLength();
bool findItem(string item);
void putItem(string item);
void deleteItem(string item);
void resetList();
string getNextItem();
string findWordStartWith(char startAlphabet);
bool removeMiddleNode();
void displayListBackwards();
void printList();
};
Here's the .cpp file.
#include "sortedRevLLStr.h"
#include <iostream>
#include <string>
using namespace std;
sortedRevLLStr::sortedRevLLStr()
{
length = 0;
listData = NULL;
currentPos = NULL;
}
void sortedRevLLStr::makeEmpty()
{
node* tempPtr;
while (listData != NULL)
{
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0;
}
bool sortedRevLLStr::isFull()
{
node* location;
try
{
location = new node;
delete location;
return false;
}
catch (std::bad_alloc exception)
{
return true;
}
}
int sortedRevLLStr::getLength()
{
return length;
}
bool sortedRevLLStr::findItem(string item)
{
bool moreToSearch;
node* location;
location = listData;
bool found = false;
moreToSearch = (location != NULL);
while (moreToSearch && !found)
if (item == location->data)
{
found = true;
break;
}
else if (item > location->data)
{
location = location->next;
moreToSearch = (location != NULL);
}
else
{
moreToSearch = false;
break;
}
return found;
}
void sortedRevLLStr::putItem(string item)
{
if (isFull())
return;
node* location;
node* prevLoc;
node* newNode;
bool moreToSearch;
location = listData;
prevLoc = NULL;
moreToSearch = (location != NULL);
while (moreToSearch)
{
if (item < location->data) // change to descending order
{
prevLoc = location;
location = location->next;
moreToSearch = (location != NULL);
}
else if (item >= location->data) // change to descending order
{
moreToSearch = false;
break;
}
}
newNode = new node;
newNode->data = item;
if (prevLoc == NULL)
{
newNode->next = listData;
listData = newNode;
}
else
{
newNode->next = location;
prevLoc->next = newNode;
}
length++;
}
void sortedRevLLStr::deleteItem(string item)
{
//resetList();
node* location;
node* prevLoc;
node* tempLocation = NULL;
location = listData;
prevLoc = NULL;
bool moreToSearch = (location != NULL);
bool found = false;
if (item == location->data)
{
found = true;
tempLocation = location;
listData = listData->next;
}
else
{
while (moreToSearch)
{
if (item == location->data)
{
found = true;
moreToSearch = false;
tempLocation = location;
prevLoc->next = location->next;
}
else if (item > location->data)
{
prevLoc = location;
location = location->next;
moreToSearch = (location != NULL);
}
else
{
moreToSearch = false;
break;
}
}
}
if (found)
{
delete tempLocation;
length--;
}
}
void sortedRevLLStr::resetList()
{
currentPos = NULL;
}
string sortedRevLLStr::getNextItem()
{
if (currentPos == NULL)
currentPos = listData;
else
currentPos = currentPos->next;
return currentPos->data;
}
string sortedRevLLStr::findWordStartWith(char a)
{
node* temp = listData;
string str;
resetList();
while(temp != NULL)
{
for (int i = 0; i < length; i++)
{
str = getNextItem();
char startWith = str[0];
if (startWith == a)
return str;
}
return "Not Found";
cout << endl;
}
return "";
}
bool sortedRevLLStr::removeMiddleNode()
{
int length = getLength();
int getmiddleNode = length / 2;
int deleteMidNode;
if (listData == NULL)
{
return false;
}
if (length < 2)
{
return false;
}
else
{
if (length % 2 == 0)
deleteMidNode = getmiddleNode - 1;
else
deleteMidNode = getmiddleNode;
node* prevNode = NULL; // set to NULL
node* midNode = listData;
for (int i = 0; i < deleteMidNode; i++)
{
prevNode = midNode;
midNode = midNode->next;
}
prevNode->next = midNode->next;
delete midNode;
}
length--;
return true;
}
void sortedRevLLStr::displayListBackwards()
{
resetList();
node* currentPos = listData;
node* prevPtr = NULL;
node* temp = NULL;
while (currentPos != NULL)
{
temp = currentPos->next;
currentPos->next = prevPtr;
prevPtr = currentPos;
currentPos = temp;
}
listData = prevPtr;
}
void sortedRevLLStr::printList()
{
string item;
resetList();
for (int i = 0; i < length; i++)
{
item = getNextItem();
cout << item << " ";
}
cout << endl;
}
MAIN.CPP
#include "sortedRevLLStr.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
sortedRevLLStr list = sortedRevLLStr();
string inp1, inp2, inp3, inp4, inp5;
cout << "Please enter 5 strings to enter in the list: ";
cin >> inp1;
cin >> inp2;
cin >> inp3;
cin >> inp4;
cin >> inp5;
list.putItem(inp1);
list.putItem(inp2);
list.putItem(inp3);
list.putItem(inp4);
list.putItem(inp5);
list.printList();
list.displayListBackwards();
list.printList();
string inp6;
cout << "Pick an item you would like to delete from the list: ";
cin >> inp6;
list.deleteItem(inp6);
list.printList();
cout << "Enter a char to find the word in the list" << endl;
char sChar;
cin >> sChar;
cout << list.findWordStartWith(sChar) << endl;
list.printList();
list.removeMiddleNode();
list.printList();
return 0;
}