我正在研究此程序,該程序讀取文本文件並從文本文件中抓取數據並將其插入到鏈接列表的節點中。刪除單向鏈接列表中特定數字範圍之外的節點
我有整個程序運行,除了節點刪除工作正常。我正在過濾來自文本文件的數據,因此我只需要打印出具有特定範圍內值的數據。我可以用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;
}
我其實是有它作爲「或」,但改變了它到'和'出於絕望(我正在嘗試所有的東西......),並忘記在發佈之前將它改回來,但正如你所說的那樣,打印所有東西但不打印任何東西。 – DomBavetta
也許在while循環中,打印出正在被刪除的節點,然後打印出這些節點的淨工資。這會讓你更好地看看循環中發生了什麼。 –
那麼問題就在於每個節點都被刪除了,所以沒有什麼可以打印出來的。 – DomBavetta