c++ - Deleting nodes that are outside of a specific numeric range in a singly linked list -
i working on program reads text file , grabs data out of text file , inserts nodes of linked list.
i have whole program running , working fine besides node deletion. filtering data text file need print out data have values within range. can if() statement , works fine that's not result want.
i want delete nodes outside of specified range , free memory using. have few lines of code wrote try ends deleting entire list. if point me in right direction , tell me i'm doing wrong great!
#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 data new node in linked list, creating new node each time loop executes. while( in >> first >> last >> salary >> bonus >> deduction) insertathead (head, first, last, salary, bonus, deduction); //close file in.close(); cout << "-salary in 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 input values, function create new node , insert @ beginning of linked list. function allows insertion @ beginning of list , no 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 input values, function creates new node , inserts after node provided in 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 net salary based off of salary, bonus, , deduction variables of input node. float netsalary(employptr& node) { float netsalary, newdeduction; newdeduction = ((node->salary) + (node->bonus)) * (node->deduction); netsalary = (node->salary + node->bonus) - newdeduction; return netsalary; }
edit: changed && || still having issue.
edit #2: solution
while(netsalary(iter)<45000 || netsalary(iter)>60000) { employptr nodetodelete = new employee; nodetodelete = iter; iter = iter->link; delete nodetodelete; }
this line right here:
while(netsalary(iter)<45000 && netsalary(iter)>60000)
i believe conditional should or (||). not make sense value both less 45000 , more 60000 @ same time.
given value 25000, less 45000, not more 60000, therefore nothing ever deleted.
edit: perhaps try along these lines:
for (iter = head; iter != null; iter = iter->link) { cout << iter->salary; // can see node it's looking @ if (netsalary(iter) < 45000 || netsalary(iter) > 60000) { employptr nodetodelete = iter; iter = iter->link; // difference here you're explicitly moving iter forward delete nodetodelete; } }
Comments
Post a Comment