Lecture 19 Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture19/*.* Reminder: Homework 8 due today. Homework 9 is posted (PDF only). They are sample written midterm exam problems. It is due on Monday at the beginning of class (no late work accepted) as part of the midterm exam review. Questions? Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 1
Outline Review node struct, list_length Linked list toolkit build as we go list_write not in the textbook list_head_insert, list_insert list_search const and non-const list_locate const and non-const list_head_remove, list_remove, list_clear list_copy Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 2
Review: node Structure Recall the node structure used in linked lists. struct node { typedef double value_type; // CONSTRUCTOR node(const value_type & init_data = value_type(), node *init_link = 0) { data = init_data; next = init_link; } // note, do not need ';' here // FIELDS value_type data; data next node *next; 3.14 }; Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 3
Review: Building a Linked List Examine file list-examples.cpp The first part is the examples from the last lecture that builds a linked list by hand using pointer variables to each node. The head pointer is r. r q t p 12.1 14.6 9.3-4.8 Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 4
Linked-List Toolkit Examine file node1.h In addition to the node structure, it has the prototypes for some common functions that are used to access and manipulate linked lists. Call this the linked-list toolkit. Last time we looked at list_length, a function that computes the number of nodes in a linked list given its head pointer. size_t list_length (const node *head_ptr); Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 5
Linked List Toolkit Implementations for the toolkit functions are in file node1.cpp. A driver program is in file list-examples.cpp. A makefile is provided. Note that many functions are stubs and output a message saying so. This allows us to compile and run the entire program that uses them without implementing all the functions. Most the code that is missing is in the textbook using a node class; we will build the toolkit using the node struct as we go. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 6
Review: list_length Pattern for accessing every node in a linked list is a for-loop. size_t list_length (const node *head_ptr) { size_t count = 0; const node *cursor; // lcv decl for (cursor = head_ptr; // init lcv cursor!= 0; // lcv end test cursor = cursor >link; // "incr" lcv count++; // loop body return count; } Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 7
In-class Exercise: list_write list_write outputs each item in a linked list to an output stream on a separate line. It receives the head pointer and an output stream. void list_write (const node *head_ptr, std::ostream & out); Write the implementation of list_write in the file node1.cpp. Compile and run the program. All of the calls to list_write should show the same 4 items, since the rest of the toolkit is not implemented. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 8
list_head_insert A common operation on linked lists is to insert a new item at the head of the list. list_head_insert receives and passes back the head pointer (since it uses the head pointer's value and also changes it) and receives the item to be inserted. void list_head_insert (node * & head_ptr, const node::value_type & entry); Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 9
In-class Exercise: list_head_insert Implement this function in node1.cpp The design for this function is 1. Create a new node with entry as its data value and head_ptr as its link. 2. Make head_ptr point to the new node Here is a picture of list_head_insert(r, 1.2): At the beginning of the function headptr 12.1 At the end of the function headptr 12.1 entry 1.2 insertptr 1.2 Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 10
list_insert list_insert is used to insert items anywhere else besides the head of the list. In order to do this, it receives a pointer to the node that will be in front of the new node containing entry. Call this the previous pointer. void list_insert (node * previous_ptr, const value_type & entry); Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 11
In-class Exercise: list_insert Implement this function in node1.cpp The design for this function is 1. Create a new node with entry as its data value and previous_ptr's node's link as its link. 2. Make previous_ptr's node's link point to the new node Note this works even when previous_ptr is pointing the last node in the list. A picture of list_insert(q, -7.8) is shown on the next slide. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 12
list_insert At the beginning of the function previousptr 14.6 9.3 entry 7.8 At the end of the function previousptr 14.6 9.3 insertptr 7.8 Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 13
list_search list_search receives a head pointer and a target value. It returns a pointer to the first node that contains the target value or 0 if the target is not found. As with searching arrays and vectors, the design of this function is to iterate through the list and returning a pointer when the target is found. It has both const and non-const versions. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 14
list_locate list_locate receives a head pointer and a position. It returns a pointer to the node at that position, or 0 if there is no such position. Per the specifications in the textbook, item positions start at 1 (not 0). This function uses an assert to ensure that position > 0. The design of this function is to iterate through the list until the position is found or the end of the list is reached. It has both const and non-const versions Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 15
list_head_remove As with inserting, removing an item at the head of a list is different than removing an item anywhere else in the list. list_head_remove receives and passes back the head pointer of a list. The result of the function is that the head pointer now points to what was the second item in the list, and the removed node is deallocated. void list_head_remove (node * & head_ptr); Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 16
In-class Exercise: list_head_remove Implement this function in node1.cpp The design of this function is 1. Make the head pointer point to the second node in the list 2. Deallocate the first node Here is a picture of list_head_remove(r): At the beginning of the function head_ptr At the end of the function remove_ptr head_ptr 1.2 12.1 1.2 12.1 Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 17
list_remove list_remove is used to remove items anywhere else besides the head of the list. As with list_insert, it receives a previous pointer to the node that is in front of the node being removed. The result of this function is previous_ptr's link now points to the node after the one it originally pointed to, and the removed node is deallocated. Note this assumes that previous_ptr is not pointing to the tail node. void list_remove (node * previous_ptr); Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 18
In-class Exercise: list_remove Implement this function in node1.cpp The design of this function is 1. Make the previous_ptr's link point to the node after the one it currently points to 2. Deallocate the removed node Here is a picture of list_remove(t): At the beginning of the function previousptr At the end of the function previousptr removeptr 9.3 4.8 9.3 4.8 Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 19
list_clear list_clear receives a head pointer. It deallocates all of the nodes in the list and sets the head to 0. This is useful for the destructor and assignment operator of classes using a linked list as an implementation. This function uses list_head_remove repeatedly until the head pointer is 0. Uncomment the implementation of list_clear in node1.cpp. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 20
list_copy The copy constructor and assignment operator of classes using a linked list as an implementation need to be able to make a copy of a linked list. list_copy receives the head pointer for a source list and passes back the head and tail pointers of the copy. Since this is a true copy, the two lists do not share any nodes. void list_copy (const node *source_ptr, node * & head_ptr, // of copy node * & tail_ptr); // of copy Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 21
In-class Exercise: list_copy Implement this function in node1.cpp The design for this function is 1. Initialize head_ptr and tail_ptr to 0. 2. Handle the case of an empty source list. 3. Create the head node of the copy and make head_ptr and tail_ptr point to it. 4. For each of the remaining source nodes, make a copy and add it at the tail of the new list. Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 22