Question: Please can i get help with this C++ home work... Modify the following program to replace the delete nxt; statement. Instead of freeing the memory,
Please can i get help with this C++ home work...
Modify the following program to replace the delete nxt; statement. Instead of freeing the memory, add the deleted object to a deleted list, so that it is removed from the list of states, but not returned to free store. Then add another counting loop at the end of the program to count the number of entries in the deleted list and verify that no State instances have been lost. Then, add code to cleanup both lists. Use the attached input file.
class State { std::string name; State *next; public: State(std::string &s); State *getNext() const; State *setNext(State *w); std::string getState() const; }; State::State(std::string& s) { name = s; next = 0; } State *State::getNext() const { return next; } State *State::setNext(State *n) { return next = n; } static int everyOther() { std::ifstream inputFile; std::string path = "path-to-file-on-your-system/states.txt"; std::string state; State *head, *tail, *nxt, *prv; int rcd; std::string sent = "$$"; inputFile.open(path); if (!inputFile) { cout << "file not found:" << path << endl; return -1; } tail = head = new State(sent); rcd = 0; while (getline(inputFile, state)) { ++rcd; tail = tail->setNext(new State(state)); } inputFile.close(); std::cout << "Created " << rcd << " State instances" << std::endl; prv = head; while (0 != prv) { nxt = prv->getNext(); if (0 != nxt) { prv->setNext(nxt->getNext()); delete nxt; } prv = prv->getNext(); } rcd = 0; nxt = head->getNext(); while (0 != nxt) { ++rcd;; nxt = nxt->getNext(); } std::cout << "There are " << rcd << " State instances remaining" << std::endl; return 0; }
testcode(states.test)
California Texas Florida New York Illinois Pennsylvania Ohio Georgia North Carolina Michigan New Jersey Virginia Washington Arizona Massachusetts Indiana Tennessee Missouri Maryland Wisconsin Minnesota Colorado South Carolina Alabama Louisiana Kentucky Oregon Oklahoma Connecticut Iowa Utah Mississippi Arkansas Kansas Nevada New Mexico Nebraska West Virginia Idaho Hawaii New Hampshire Maine Rhode Island Montana Delaware South Dakota North Dakota Alaska Washington DC Vermont Wyoming
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
