Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ Not sure how to solve case I am reading the following file: The Problem : I can successfully delete the linked list node containing
C++ Not sure how to solve case
I am reading the following file:
The Problem:
I can successfully delete the linked list node containing the id "1234568", but I am not sure how to handle if the input file requests to delete that same node again. As my code stands, there will be an error if it tries to delete something that no longer exists.
My function that reads the file:
NOTE: Entire Program is not shown
{id: 1234567, first: Mary, last: Green, DOB: 1996-10-03, GPA: 4.0} {id: 1234568, first: Peter, last:White, DOB: 1997-05-22,GPA: 3.8) {id:1354238, first:Nick, last:Park, DOB: 1995-08-18, GPA: 4.0} {id: 1434587, first: Katy, last: Green, DOB: 1995-08-18, GPA:4.0} delete 1234568 {id: 1534570, first:Peter, last:White, DOB: 1997-05-22,GPA: 3.8} {id: 1634567, first: Mary, last: Green, DOB: 1996-10-03,GPA:4.0} {id: 1734568, first: Peter, last:White, DOB: 1997-05-22, GPA: 3.8) {id: 1854238, first: Nick, last:Park, DOB: 1995-08-18, GPA:4.0} {id: 1934587, first:Katy, last: Green, DOB: 1995-08-18, GPA: 4.0} delete 1234568 {id: 1234570, first: Peter, last:White, DOB: 1997-05-22,GPA: 3.8} bool LinkedList::readFile(string filename) { ifstream ifs(filename); string str; while (getline(ifs,str)) { str.erase (remove(str.begin(), str.end(), ' '), str.end(); str.erase(remove(str.begin(), str.end(), ' '), str.end(); if (str.find("delete") != string::npos) { int loc = str.find("delete") + 6; int loc2 = str.find(' '); loc++; int key = stoi(str.substr(loc, loc2 - loc)); deleteItem(key); else if (str.find("del") != string::npos) { int loc = str.find("del") + 3; int loc2 = str.find(' '); loc++; int key = stoi(str.substr(loc, loc2 - loc)); deleteItem(key); else { additem(str); return true; void LinkedList::deleteItem(int key) { Node* curr; Node* prev; Node* temp = new Node; if (head == NULL) { cout id != key)) { prev = curr; curr = curr->next; if (curr == head) { head = head->next; else { prev->next = curr->next; if (curr->next == NULL) { temp = prev; delete(curr)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started