Description
Goal: This lab includes function and linked list, fstream, and eof().
Please try your best to finish the lab in class, and submit it to CANVAS.
In this lab, please write a Lab7.cpp file. There is a main function in this file. Copy your
Lab6.cpp to be the Lab7.cpp. Please define one more function in the lab. The new
function defined by you should be insert().
In Lab6, we read contents from a file and append the contents to a singly linked list. In
this lab, we will modify the functionality implemented by Lab6. In this lab, we insert the
new node to the singly linked list IN increasing ORDER. Please note that you MUST
use singly linked list to implement this lab. If the code is implemented with array or
vector, or any data structure other than singly linked list, the grade will be 0!
Just like Lab6, Lab7.cpp reads contents from an input file, which is “sample.txt” in
today’s lab. Each row of the input file has a leading char, followed by a string of SSN,
and first name and last name. Whenever Lab6.cpp reads one row from the file, it stores
SSN and the corresponding name (including both first name and last name) to a singly
linked list. After the whole input file is processed, the program prompts the user to type a
SSN, then it will search the SSN in the singly 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,
jli$ ./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
jli$ ./a.out sample.txt
Input a SSN:
545309496
Found at: 7
List contents:
038249140
138863035
151682139
173421651
364523152
478362801
493582998
545309496
652802112
766434955
815904565
912741495
926654750
In this lab, you can use fstream library to read file. An example code is listed as follows:
#include
#include
using namespace std;
int main(int argc, char* argv[]){
int x, y, z;
fstream input(argv[1]);
while(!input.eof()){
input >> x >> y >> z;
}
input.close();
}
If you do not remember how to read the contents from a file, refer to your lab4
implementation.
Hints & Requirements
• Only when the current list is empty or the first node has a larger value than the
new SSN, you insert the new node as the first node of the list.
• Use a Node pointer p start from head pointer, make it go through the list until p-
>next is NULL or p->next has the SSN larger than the new SSN. Insert the new
node after p.
• The Node defined in the slides has only one user variable, val. You can define
multiple variables inside one node. For example, you can define string SSN; and
string name; inside the node.
• Do NOT define any array in this lab. You do not need array in this lab.
Wrap up
When you’re done, jar three files to lab6.jar
jar –cvf lab7.jar *
Submit lab7.jar to Canvas.
Make sure you logout before you leave!
If you cannot finish lab in class, please save all your files. Next time you login the
computer in the lab, you can continue work on your files. Please save them before you
logout. If you work in a Linux lab, please save the file to your machine. However, if you
are working in the Mac lab, please save the file to a CLOUD. The Mac machine will
erase everything you saved once you logout.