【发布时间】:2014-08-18 21:21:53
【问题描述】:
我正在开发这个程序,它读取文本文件并从文本文件中抓取数据并将其插入到链表的节点中。
除了删除节点外,我的整个程序都运行良好。我正在从文本文件中过滤数据,所以我只需要打印出具有特定范围内的值的数据。我可以用 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;
}
【问题讨论】:
标签: c++ linked-list range nodes delete-operator