【问题标题】:allowing users to delete a node from a linked list [closed]允许用户从链表中删除节点[关闭]
【发布时间】:2022-01-05 08:08:15
【问题描述】:

我正在为我的课程做一个项目,我想知道是否有一种简单的方法可以让用户从我的链接列表中删除节点?这就是我目前所拥有的

#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;
}

用户可以添加自己的数据,但我不知道如何允许他们删除他们想要的任何节点。我的老师在解释这一点方面做得很差,如果有任何帮助,我们将不胜感激。

【问题讨论】:

  • “我的老师在解释这个方面做得很差……” 好吧,告诉他们,或者校长。检查课堂上的其他人是否有同感,并获得他们对您的投诉的支持。顺便说一句,从单链表或双链表中删除节点只是琐事,在责怪你的老师之前,考虑到你只是不理解这个概念。用铅笔和纸自己想象一下。
  • 你的教科书中有没有讲到链表的章节?如果不是,那么您的讲师使用的教科书很差,或者没有教正确的材料,这是没有人真正可以帮助您的事情。如果你的学习材料涵盖了这个主题,那么它应该有从链接列表中删除节点的示例,那么,在描述或示例中,你不清楚的是什么?

标签: c++ visual-c++


【解决方案1】:

如果您询问如何从链表中删除任何节点,您可以通过将前一个节点连接到下一个已删除节点旁边来删除任何节点,然后在要删除的节点上使用delete

您可以通过在迭代时跟踪两个节点来获取上一个节点。一个用于当前,一个用于上一个。

如果您询问获取用户输入,您可以按顺序打印列表以及每个元素的数字,然后询问用户要删除的数字。稍后您可以多次迭代列表并删除其中的节点,如我之前提到的。

作为旁注,不要指望你的老师会深入地教你一切。学习如何通过给定的教科书additional books 或使用互联网学习如何自学是很好的。

【讨论】:

    猜你喜欢
    • 2016-05-19
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2021-07-01
    相关资源
    最近更新 更多