Lab06, CISC181, Fall 2005
Practice with Linked Lists
This lab is designed to give you practice working with linked lists.
Look at the source file: lists.cc (found in the lab06 directory on the course web site.)
In this file, the pointer variable mylist (in the main function) points to the first element in the linked list. A function make_a_list builds the linked list. Three other functions: print_list, add_at_front and add_at_back, are not complete. The calls to the other functions are already given in the main program (some are commented out). Your assignment is to complete these functions.
Understanding each of the List functions
- make_a_list
This function builds the linked list. Note that head points to the head, or start, of the list, and is initially NULL since the list starts out empty.
The functions loops through the integers 20 down to 2. During each iteration, a new node is created. This new node will be inserted at the beginning of the list. The value field of this node gets assigned the current integer (num), and the next field of the node is assigned what head used to point to.
When the loop finishes, the value of head, which points to the first element of the list, automatically goes back to the calling function, since it is itself a reference parameter.
- print_list
Once you understand the previous code, a good place to start is the print_list function. Basically, you want to loop through each node in the list until you reach a NULL value (which indicates the end of the list). As you are looping, you want to print the integer value stored in that node.
- add_at_front
The add_at_front function will insert a new node at the beginning of the list. So, you want to first create a new node. Next, assign to this new node (in its value field) the integer value (addvalue) that was passed to the function. You also want the node's next field to point to the previous first element of the list. Finally, you want the head of the list to point to this new first element.
- add_at_back
In this function, you will need to loop through all elements of the list until you reach the last element of the list. Since we are adding a new node to the list, you will want to create a new node. Then, assign to this new node the integer value that was passed to the function. Since this new node will be the last node in the list, be sure to assign its net field appropriately. Finally, don't forget to have the previous node point to the new node.
What you have to do
Finishing and Submitting
- Upload your three .txt files and your three .cc files to WebCT and submit. Print your three scripts files for your TA.
Grading:
100 pts: 30 pts each for the three files (divided among style and correctness as the TA sees fit), and 10 points for overall correct submission.