-2

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.

  • _"My teacher has done a poor job of explaining this..."_ Well, tell 'em, or the principal. Check if others in class feel the same, and get their support for your complaint. BTW deleting nodes from a single or double linked list is just trivia, consider that you just didn't understand the concept yet before blaming your teacher. Visualize that yourself with pencil and paper. – πάντα ῥεῖ Nov 27 '21 at 20:21
  • Is there a chapter in your textbook that talks about linked list? If no, then your instructor is using a poor textbook, or is not teaching the right material, and that's something that nobody really can help you with. If this topic is covered by your study material it should have examples of deleting nodes from a linked list, so what, exactly, in the description or examples there that's unclear to you? – Sam Varshavchik Nov 27 '21 at 20:30

1 Answers1

1

If you're asking about how to delete any node from the linked list, you can delete any node by connecting the previous nodes next to the deleted nodes next, then using delete on the node that you are deleting.

You can get the previous node by keeping track of two nodes while iterating. One for the current and one for the previous.

If you're asking about getting the user input, you can print the list in order with numbers for each element then ask the user for a number to delete. Later you can iterate over the list that many number of times and delete the node there as I mentioned before.

As a side note don't expect your teachers to teach you everything in depth. It's good to learn how to learn on your own studying your textbook given, additional books, or using the internet.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190