

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Lab; Class: Computer Science I; Subject: Computer Science; University: University of Mary Washington; Term: Unknown 1989;
Typology: Lab Reports
1 / 2
This page cannot be seen from the preview
Don't miss anything!
Lab 13 Working with linked lists We’ve started looking at linked lists in class. Chapter 16 of the text gives a good introduction, and
The directory needed for today’s lab is ~ernie/220/lab13. Copy it. Displaying a linked list Take a look at linkedList1.cpp. You can see from the code that it is written to take input from stdin, put values on a list, and then display the list. Compile the program using the file input13_1.data as input. You’ll see the program doesn’t terminate! Find the problem and fix it so the program outputs the data and displays it to stdout. Create links on the Web page with links for your lab work and assignments to the source for your modified version of linkedList1.cpp , and to a script of it executing using input13_1.data as input.
We want to use the same program with data that holds a user name in one field and an id number in another. The user name is at most 8 characters and the id number is an integer. You’re your program linkedList1.cpp into linkedList2.cpp. Modify the definition typedef int ComponentType; so it can be used in this situation. Want a hint? Define a struct type that has two fields, one for the name and the other for the user id. Then replace int with the name of that struct type. Test your program using input13_2.data. Create links on the Web page with links for your lab work and assignments to the source for your modified version of linkedList2.cpp , and to a script of it executing using input13_2.data as input. Writing functions for the input and output Copy linkedList2.cpp into linkedList3.cpp , and modify linkedList3.cpp so that the input and output is done using functions. Specifically, implement a function for input defined as NodePtr getList( ) The function returns an object of type NodePtr that points to the first element if a list read standard in using cin. Note that input is performed on items of type CompnentType. You’ll likely call the function in a program as head =getList(); Implement a function for output defined as void showList(NodePtr head) This function prints the type Component Type field of each item, one per line, on the list pointed to by head. You’ll likely call this program as showList(head); Create links on the Web page with links for your lab work and assignments to the source for your modified version of linkedList3.cpp , and to a script of it executing using input13_2.data as input.
Writing a deep-copy function The idea of a deep copy is to create a clone of an existing list. That is, a new list that has exactly the same items of as the original list, but there is no connection between the lists or list items. Copy linkedList3.cpp into linkedList4.cpp , and modify linkedList4.cpp so that it contains a function that clones or does a deep-copy of the original list. The function you are to implement is defined as NodePtr clone( NodePtr head) Clone returns a pointer to a list that is an exact copy of the list pointed to by head. the lsit pointed to by head is not modified and there is no connection between the two lists other than the contents of the component fields of corresponding elements are equal. When you are able to use the function with this program, modify the program so the body if main() contains head = getList(); showList(head); notherHead = clone(head); head = head -> link; //removes first item of the list pointed to by head cout << “ Head with one item removed “ <<endl; showList(head); cout <<endl << ** Clone of original list **<< endl; showList(notherHead); Create links on the Web page with links for your lab work and assignments to the source for your modified version of linkedList4.cpp , and to a script of it executing using input13_2.data as input.
Copy linkedList3.cpp into linkedList5.cpp , and modify linkedList5.cpp so that it contains the statements necessary to display the user name and user id of the item on the list for which the user id is a maximum. Modify the program so that two new pointers are declared NodePtr ptrToMax; NodePtr ptrToMin; and the program includes the output cout << endl << “ Listus Maximus “”<< endl; cout << prtToMax->component.username <<” “<<ptrToMax ->component.uid << endl; cout << endl << “** Listus Minimus “”<< endl; cout << prtToMin->component.username <<” “<<ptrToMin ->component.uid << endl;** Create links on the Web page with links for your lab work and assignments to the source for your modified version of linkedList5.cpp , and to a script of it executing using input13_2.data as input.