Home / Expert Answers / Computer Science / include-lt-iostream-gt-include-lt-string-gt-include-lt-vector-gt-include-34-node-h-34-include-34-s-pa659

(Solved): #include <iostream>#include <string>#include <vector>#include "Node.h"#include "S ...



student submitted image, transcription available belowstudent submitted image, transcription available below

#include <iostream>
#include <string>
#include <vector>
#include "Node.h"
#include "SortedNumberList.h"
using namespace std;

void PrintList(SortedNumberList& list);
vector<string> SpaceSplit(string source);

int main(int argc, char *argv[]) {
// Read the line of input numbers
string inputLine;
getline(cin, inputLine);

// Split on space character
vector<string> terms = SpaceSplit(inputLine);

// Insert each value and show the sorted list's contents after each insertion
SortedNumberList list;
for (auto term : terms) {
double number = stod(term);
cout << "List after inserting " << number << ": " << endl;
list.Insert(number);
PrintList(list);
}

/*
// Read the input line with numbers to remove
getline(cin, inputLine);
terms = SpaceSplit(inputLine);

// Remove each value
for (auto term : terms) {
double number = stod(term);
cout << "List after removing " << number << ": " << endl;
list.Remove(number);
PrintList(list);
}
*/

return 0;
}

// Prints the SortedNumberList's contents, in order from head to tail
void PrintList(SortedNumberList& list) {
Node* node = list.head;
while (node) {
cout << node->GetData() << " ";
node = node->GetNext();
}
cout << endl;
}

// Splits a string at each space character, adding each substring to the vector
vector<string> SpaceSplit(string source) {
vector<string> result;
size_t start = 0;
for (size_t i = 0; i < source.length(); i++) {
if (' ' == source[i]) {
result.push_back(source.substr(start, i - start));
start = i + 1;
}
}
result.push_back(source.substr(start));
return result;
}

Inspect the class declaration for a doubly-linked list node in Node.h. Access Node.h by clicking on the orange arrow next to main.cpp at the top of the coding window. The Node class has three member variables: - a double data value, - a pointer to the next node, and - a pointer to the previous node. Each member variable is protected. So code outside of the class must use the provided getter and setter member functions to get or set a member variable. Node.h is read only, since no changes are required. Step 2: Implement the Insert() member function A class for a sorted, doubly-linked list is declared in SortedNumberList.h. Implement the SortedNumberList class's Insert() member function. The function must create a new node with the parameter value, then insert the node into the proper sorted position in the linked list. Ex: Suppose a SortedNumberList's current list is , then Insert (33.5) is called. A new node with data value 33.5 is created and inserted between 23 and 47.25 , thus preserving the list's sorted order and yielding: Step 3: Test in develop mode Code in main() takes a space-separated list of numbers and inserts each into a SortedNumberList. The list is displayed after each insertion. Ex: If input is then output is: then output is: List after inserting 77 : 77 List after inserting 1577 List after inserting -421577 List after inserting 63.5 : -421563.577 List after inserting 77 : 77 List after inserting 15 : 1577 List after inserting -42 : List after inserting 63.5: Try various program inputs, ensuring that each outputs a sorted list. Step 4: Implement the Remove() member function Implement the SortedNumberList class's Remove() member function. The function takes a parameter for the number to be removed from the list. If the number does not exist in the list, the list is not changed and false is returned. Otherwise, the first instance of the number is removed from the list and true is returned. Uncomment the commented-out part in main() that reads a second input line and removes numbers from the list. Test in develop mode to ensure that insertion and removal both work properly, then submit code for grading. Ex: If input is then output is: List after inserting 84 List after inserting 7284 List after inserting 197284 List after inserting 19617284 List after removing 617284 List after removing 6172 List after inserting 84: 84 List after inserting 72: 7284 List after inserting 19: List after inserting 61 : List after removing 19: List after removing 84 :


We have an Answer from Expert

View Expert Answer

Expert Answer



We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe