Unformatted text preview:

Lecture 22OutlineNode ClassBuilding a Linked ListLinked List Toolkit 1Linked List Toolkit 2ListLengthListDisplay 1ListHeadInsert 1ListHeadInsert 2ListInsert 1ListInsert 2ListInsert 3ListSearchListLocateListHeadRemove 1ListHeadRemove 2ListRemove 1ListRemove 2ListClearListCopy 1ListCopy 2SubmissionFriday, October 15 CS 215 Fundamentals of Programming II - Lecture 22 1Lecture 22Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture22/*.*The instructor will send email when the submission system is ready to accept Project 4 submissions. This is expected to happen by Saturday morning.Questions?Friday, October 15 CS 215 Fundamentals of Programming II - Lecture 22 2OutlineReview Node class, ListLengthLinked list toolkit – build as we goListDisplayListHeadInsert, ListInsertListSearch – const and non-constListLocate – const and non-constListHeadRemove, ListRemove, ListClearListCopyFriday, October 15 CS 215 Fundamentals of Programming II - Lecture 22 3Review: Node ClassRecall the Node class used in linked lists.class Node { public: typedef double value_type; Node (const value_type & initData = value_type(), node *initLink = 0) {...} void SetData (const value_type & newData) {...} void SetLink (Node *newLink){...} value_type Data () const {...} const Node *Link() const {...} Node *Link () {...} private: value_type dataItem; // Note name change Node *nextLink;}; 3.14dataItem nextLinkFriday, October 15 CS 215 Fundamentals of Programming II - Lecture 22 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 -4.8 9.3r q t pFriday, October 15 CS 215 Fundamentals of Programming II - Lecture 22 5Linked List ToolkitExamine file node1.hIn addition to the Node class, 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 ListLength, a function that computes the number of nodes in a linked list given its head pointer.size_t ListLength (const Node *headPointer);Friday, October 15 CS 215 Fundamentals of Programming II - Lecture 22 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 some are not implemented and output a message saying so. This technique is called writing function stubs. It allow you to compile and run an entire program that uses the functions without implementing all the functions.Most the code that is missing is in the textbook, but we will build the toolkit as we go.Wednesday, October 13 CS 215 Fundamentals of Programming II - Lecture 21 7Review: ListLengthPattern for accessing every node in a linked list is a for-loop.size_t ListLength (const Node *headPtr){ size_t count = 0; const Node *cursor; // lcv decl for (cursor = headPtr; // init lcv cursor != 0; // lcv end test cursor = cursor->Link())// "incr" lcv count++; // loop body return count;}Wednesday, October 13 CS 215 Fundamentals of Programming II - Lecture 21 8In-class Exercise: ListDisplayListDisplay outputs each item in a linked list to the screen on a separate line. It receives the head pointer.void ListDisplay (const Node *headPtr);Write the implementation of ListDisplay in the file node1.cpp. Compile and run the program. All of the list displays should show the same 4 items, since the rest of the toolkit is not implemented.Wednesday, October 13 CS 215 Fundamentals of Programming II - Lecture 21 9ListHeadInsertA common operation on linked lists is to insert a new item at the head of the list.ListHeadInsert 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 ListHeadInsert (Node * & headPtr, const Node::value_type & entry);Wednesday, October 13 CS 215 Fundamentals of Programming II - Lecture 21 10In-class Exercise: ListHeadInsertImplement this function in node1.cppThe design for this function is1. Create a new node with entry as its data value and headPtr as its link.2. Make headPtr point to the new nodeHere is a picture of ListHeadInsert(r, 1.2):12.1headPtr1.2entryAt the beginning of the function At the end of the function12.11.2headPtrinsertPtrWednesday, October 13 CS 215 Fundamentals of Programming II - Lecture 21 11ListInsertListInsert 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 ListInsert (Node * previousPtr, const value_type & entry);Friday, October 15 CS 215 Fundamentals of Programming II - Lecture 22 12In-class Exercise: ListInsertImplement this function in node1.cppThe design for this function is1. Create a new node with entry as its data value and previousPtr's node's link as its link.2. Make previousPtr's node's link point to the new nodeNote this works even when previousPtr is pointing the last node in the list.A picture of ListInsert(q, -7.8) is shown on the next slide.Friday, October 15 CS 215 Fundamentals of Programming II - Lecture 22 13ListInsert14.6previousPtr-7.8entryAt the beginning of the functionAt the end of the function-4.814.6previousPtr-7.8-4.8insertPtrFriday, October 15 CS 215 Fundamentals of Programming II - Lecture 22 14ListSearchListSearch 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 if the target is found.It has both const and non-const versions.Friday, October 15 CS 215 Fundamentals of Programming II - Lecture 22 15ListLocateListLocate 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


View Full Document

UE CS 215 - Lecture 22 SLIDES

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 22 SLIDES
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 22 SLIDES 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 22 SLIDES 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?