gpt4 book ai didi

c++ - 删除单向链表中特定数值范围之外的节点

转载 作者:行者123 更新时间:2023-11-28 06:48:48 25 4
gpt4 key购买 nike

我正在开发这个读取文本文件并从文本文件中获取数据并将其插入到链表节点中的程序。

除了节点删除之外,我的整个程序都在运行并且工作正常。我正在从文本文件中过滤数据,因此我只需要打印出具有特定范围内值的数据。我可以用 if() 语句来做到这一点,它工作正常,但这不是我想要的结果。

我想删除指定范围之外的节点并释放它们正在使用的内存。我写了几行代码来尝试执行此操作,但最终却删除了整个列表。因此,如果有人能指出我正确的方向并告诉我我做错了什么,那就太好了!

#include <fstream>
#include <iostream>
using namespace std;

struct Employee
{
string firstN;
string lastN;
float salary;
float bonus;
float deduction;

Employee *link;
};

typedef Employee* EmployPtr;
void insertAtHead( EmployPtr&, string, string, float, float,float );
void insert( EmployPtr&, string, string, float, float,float );
float netSalary( EmployPtr& );

int main()
{
//Open file
fstream in( "payroll.txt", ios::in );

//Read lines
string first, last;
float salary, bonus, deduction;
EmployPtr head = new Employee;

//Inserts all the data into a new node in the linked list, creating a new node each time the loop executes.
while( in >> first >> last >> salary >> bonus >> deduction)
insertAtHead (head, first, last, salary, bonus, deduction);

//Close file
in.close();

cout << "-Salary in the range of ($45,000 - $60,000)-\n" << "Printed in format: First Name, Last Name, Salary, Bonus, Deduction, Net Salary.\n";

EmployPtr iter, temp;
for(iter = head; iter!= NULL; iter = iter->link)
{
temp = head;
//Deletes nodes outside of range.
while(netSalary(iter)<45000 || netSalary(iter)>60000)
{
EmployPtr nodeToDelete = temp;
temp = temp->link;
delete nodeToDelete;
}

cout << iter->firstN << ", " << iter->lastN << ", " << iter->salary << ", " << iter->bonus << ", " << iter->deduction << ", " << netSalary(iter) <<endl;

}
return 0;
}

//Based off of the input values, this function will create a new node and insert it at the beginning of the linked list. This function ONLY allows insertion at the beginning of the list and no where else.
void insertAtHead(EmployPtr& head, string firstValue, string lastValue,
float salaryValue, float bonusValue,float deductionValue)
{
EmployPtr tempPtr= new Employee;

tempPtr->firstN = firstValue;
tempPtr->lastN = lastValue;
tempPtr->salary = salaryValue;
tempPtr->bonus = bonusValue;
tempPtr->deduction = deductionValue;

tempPtr->link = head;
head = tempPtr;
}

//Based off of the input values, this function creates a new node and inserts it AFTER the node provided in the argument.
void insert(EmployPtr& afterNode, string firstValue, string lastValue,
float salaryValue, float bonusValue,float deductionValue)
{
EmployPtr tempPtr= new Employee;


tempPtr->firstN = firstValue;
tempPtr->lastN = lastValue;
tempPtr->salary = salaryValue;
tempPtr->bonus = bonusValue;
tempPtr->deduction = deductionValue;

tempPtr->link = afterNode->link;
afterNode->link = tempPtr;
}

//This function calculates a net salary based off of the salary, bonus, and deduction variables of the input node.
float netSalary(EmployPtr& node)
{
float netSalary, newDeduction;

newDeduction = ((node->salary) + (node->bonus)) * (node->deduction);
netSalary = (node->salary + node->bonus) - newDeduction;

return netSalary;
}

编辑:将 && 改回 ||仍有问题。

编辑 #2:解决方案

while(netSalary(iter)<45000 || netSalary(iter)>60000)
{
EmployPtr nodeToDelete = new Employee;
nodeToDelete = iter;
iter = iter->link;
delete nodeToDelete;
}

最佳答案

这一行就在这里:

while(netSalary(iter)<45000 && netSalary(iter)>60000)

我相信你的条件应该是 OR (||)。一个值同时小于 45000 和大于 60000 是没有意义的。

给定值 25000,它将小于 45000,但不超过 60000,因此永远不会删除任何内容。

编辑:也许按照这些思路尝试一些事情:

for (iter = head; iter != NULL; iter = iter->link) 
{
cout << iter->salary; // so you can see what node it's looking at
if (netSalary(iter) < 45000 || netSalary(iter) > 60000)
{
EmployPtr nodeToDelete = iter;
iter = iter->link; // difference here is that you're explicitly moving the iter forward
delete nodeToDelete;
}
}

关于c++ - 删除单向链表中特定数值范围之外的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24457354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com