I'm working on a project for my course and I was wondering if there is a simple way to allow users to delete node from my linked list? this is what I have so far
#include <iostream>
#include <string>
using namespace std;
struct node {
public:
int year;
string industry;
string individual;
int indcode;
int outbreak;
int cases;
node* next;
};
void printlist(node* n) {
while (n != NULL) {
cout << n->year; cout << " "; cout << n->industry; cout << " "; cout << n->individual; cout << " "; cout << n->indcode; cout << " "; cout << n->outbreak; cout << " "; cout << n->cases << endl;
n = n->next;
}
//the function above will print the list when it is called printlist and can have any node in the (); will print from that point onwardrs
}
int main() {
node* head = new node();
node* second = new node();
node* third =new node();
head->year = 2021; head->industry = "Agriculture"; head->individual = "All"; head->indcode = 0; head->outbreak = 75; head->cases = 565;
head->next = second;
second->year = 2021; second->industry = "Agriculture"; second->individual = "Crops production"; second->indcode = 170; second->outbreak = 53; second->cases = 410;
second->next = NULL;
printlist(head);
node* fiftyone = nullptr, * current = nullptr, * newNode = nullptr;
int addyear;
string addindustry;
string addindividual;
int addindcode;
int addoutbreak;
int addcases;
cout << "enter a year (-1 to stop): ";
cin >> addyear;
cout << "enter the industry please (-1 to stop):" << endl;
cin >> addindustry;
cout << "enter the individual setting (-1 to stop)" << endl;
cin >> addindividual;
cout << "enter the indcode (-1 to stop)" << endl;
cin >> addindcode;
cout << "enter the number of outbreaks percase (-1 to stop)" << endl;
cin >> addoutbreak;
cout << "enter the number of cases (-1 to stop)" << endl;
cin >> addcases;
while (addyear != -1)
{
//this adds nodes and data to the linked list
newNode = new node;
newNode->year = addyear;
newNode->industry = addindustry;
newNode->individual = addindividual;
newNode->indcode = addindcode;
newNode->outbreak = addoutbreak;
newNode->cases = addcases;
newNode->next = nullptr;
if (head == nullptr)
{
head = newNode;
}
else
{
current = head;
while (current->next != nullptr)
{
current = current->next;
}
current->next = newNode;
}
cout << "enter a year (-1 for all 6 settings to stop): ";
cin >> addyear;
cout << "enter the industry please (-1 for all 6 settings to stop):" << endl;
cin >> addindustry;
cout << "enter the individual setting (-1 for all 6 settings to stop)" << endl;
cin >> addindividual;
cout << "enter the indcode (-1 for all 6 settings to stop)" << endl;
cin >> addindcode;
cout << "enter the number of outbreaks percase (-1 for all 6 settings to stop)" << endl;
cin >> addoutbreak;
cout << "enter the number of cases (-1 for all 6 settings to stop)" << endl;
cin >> addcases;
}
printlist(head);
return 0;
}
users can add their own data but I have no idea how to allow them to delete any node that they would want. My teacher has done a poor job of explaining this overall any help would be much appreciated.