Unformatted text preview:

Lecture 19OutlineNode StructureBuilding a Linked ListLinked List Toolkit 1Linked List Toolkit 2list_lengthlist_writelist_head_insert 1list_head_insert 2list_insert 1list_insert 2list_insert 3list_searchlist_locatelist_head_remove 1list_head_remove 2list_remove 1list_remove 2list_clearlist_copy 1list_copy 2Wednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 1Lecture 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 2Outline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_copyWednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 3Review: 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; node *next; }; 3.14data nextWednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 4Review: 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.12.1 14.6 9.3 -4.8r q t pWednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 5Linked-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 6Linked 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 7Review: 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 8In-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 9list_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 10In-class Exercise: list_head_insertImplement this function in node1.cppThe design for this function is1. 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):12.1headPtr1.2entryAt the beginning of the function At the end of the function12.11.2headPtrinsertPtrWednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 11list_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 12In-class Exercise: list_insertImplement this function in node1.cppThe design for this function is1. 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 13list_insert14.6previousPtr-7.8entryAt the beginning of the functionAt the end of the function9.314.6previousPtr-7.89.3insertPtrWednesday, February 23 CS 215 Fundamentals of Programming II - Lecture 19 14list_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 15list_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


View Full Document

UE CS 215 - LECTURE NOTES

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Lecture 7

Lecture 7

28 pages

Load more
Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?