【发布时间】: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++