Question
C++: This project will read contents from a file and append the contents to a doubly linked list. In this lab, we will modify the
C++:
This project will read contents from a file and append the contents to a doubly linked list. In this lab, we will modify the functionality implemented by the previous code. In this lab, we insert the new node to the doubly linked list IN increasing ORDER. Please note that you MUST use doubly linked list to implement this lab.
-This will read contents from an input file, which is sample.txt in todays lab. Each row of the input file has a leading char, followed by a string of SSN, and first name and last name. Whenever it reads one row from the file, it stores SSN and the corresponding name (including both first name and last name) to a doubly linked list.
Here is "sample.txt":
i 545309496 JANNET THOMURE i 912741495 LIZABETH HOSNER i 173421651 NADIA KROPIDLOWSKI i 815904565 KATHYRN BERRINGER i 766434955 NAKIA PEASLEY i 478362801 SHERILL HYLLE i 138863035 ALINE BAIO i 493582998 LORETA BLESER i 038249140 NORMAND DODSON i 652802112 LATANYA ODENWALD i 926654750 RAMONA FILBURN i 151682139 JAMEY SILCOX i 364523152 IRMGARD SEWALL
-After the whole input file is processed, the program prompts the user to type a SSN, then it will search the SSN in the doubly linked list. If there is a match, the program prints out the index value of the node. Suppose the first node has index value 0. If there is no match in the list, print out -1.
-After printing out the searching result, the program prints out the SSN values node by node. For example,
anuk$ ./a.out sample.txt Input a SSN: 038249140 Found at: 0
List contents: 038249140 138863035 151682139 173421651 364523152 364523152 478362801 493582998 545309496 652802112 766434955 815904565 912741495 926654750
Code:
#include
struct Node{ string fullName; string personSSN; Node* next; };
//appends node into a linked list void append(Node*& head, string SSN, string name){ Node * newPtr = new Node; newPtr->fullName = name; newPtr->personSSN = SSN; newPtr->next = NULL; if(head == NULL){ head = newPtr; } else{ Node* temp = head; while(temp->next != NULL){ temp = temp->next; } temp->next = newPtr; } }
//searches for index of node int search(Node* head, string SSN){ int count = -1; while(head != NULL){ if(SSN.compare(head->personSSN) == 0){ cout << "Found at location: "; return count; } else{ head = head->next; count++; } } return count; }
int main(int argc, const char * argv[]){ fstream input(argv[1]); string x, y, z, w; string userSSN; string nameOfPerson; Node* headPtr = new Node; while(!input.eof()){ input >> x >> y >> z >> w; nameOfPerson = z + " " + w; userSSN = y; append(headPtr, userSSN, nameOfPerson); if(!input){ break; } } cout << "Input a SSN: " << endl; cin >> userSSN; cout << search(headPtr, userSSN) << endl; input.close(); }
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