lecture 23
play

Lecture 23 Log into Linux. Copy files on csserver from - PowerPoint PPT Presentation

Lecture 23 Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture23/*.* into a separate directory. Submit in-class exercise from Friday, if you have not done so already. Homework 6 posted; due next Monday. Reminder:


  1. Lecture 23  Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture23/*.* into a separate directory.  Submit in-class exercise from Friday, if you have not done so already.  Homework 6 posted; due next Monday. Reminder: Project 4 due on Wednesday.  Questions? Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 1

  2. Outline  Review linked list toolkit  Classes and linked lists  Example class: Bag class  Attributes  Reimplementation of operations  Grab Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 2

  3. Review: Linked List Toolkit  Operations on linked lists. All receive Node pointer, often the head pointer.  ListDisplay  ListHeadInsert, ListInsert  ListSearch  ListLocate  ListHeadRemove, ListRemove  ListClear, ListCopy Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 3

  4. Classes and Linked Lists  Linked lists are rarely used for their own sakes. Instead, they are like arrays, used as an implementation technique.  As with dynamic arrays, the use of a linked list requires that class operations create and delete (Node) objects as they execute, and that the destructor, copy constructor, and assignment operator be implemented. Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 4

  5. Bag Class Attributes  This bag class will be implemented using a linked list. There are two attributes:  headPtr – a pointer to the head node of the linked list that contains the bag items  numNodes – an integer that keeps track of the number of nodes in the linked list as they are inserted and removed Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 5

  6. Bag Class Attributes  Note that numNodes is not required as we can use ListLength to implement the Size operation. However, doing so makes Size an O(n) operation, since ListLength access every node in the list.  By keeping track of the nodes as they are inserted and removed, the Size operation becomes O(1). Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 6

  7. Bag Object  Here is a way to think about how this works. (Insert operation inserts at the head of the list.) Bag b; b.Insert(15); b.Insert(34); headPtr 34 15 numNodes 2 Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 7

  8. Node Class Files  Copy your node1.h and node1.cpp files from last in-class exercise to the directory with today's files. If you did not complete the in- class exercise you may copy the files from /home/hwang/cs215/linked-list-toolkit/*.*  In node1.h, change the namespace to "Lecture23Example" and the typedef to use int (rather than double).  In node1.cpp, change the namespace to "Lecture23Example". Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 8

  9. Bag Class Definition  Examine file bag3.h  Has the same typedefs as the other Bag classes, but value_type is the Node class value_type. Does not have a static constant member.  Since the underlying linked list is built one node at a time, there is only a default constructor that creates an empty bag object. Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 9

  10. Bag Class Definition  Otherwise the Bag operation prototypes are the exactly the same as the previous Bag class. Note that a Bag only receives and returns data items; never pointers to the nodes.  New operation Grab has been added. Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 10

  11. Bag Class Usage  Examine files Makefile.Inclass23 and bag3demo.cpp  The makefile now has three .o targets, one for the main program, one for the Bag class, and one for the Node class.  Demo program is exactly the same as the last one (Lecture 16). It just asks user for integers, adds them to a bag, and then asks the user for the integers again and removes them from the bag. Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 11

  12. Bag Class Implementation  Examine file bag3.cpp  As before, we will ignore any bad_alloc exceptions.  The default constructor just initializes the head pointer to null and numNodes to 0 to represent an empty list.  The Size operation returns numNodes attribute.  The rest of the Bag operations use the linked list operations from the toolkit. Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 12

  13. Insert  Since the order of the elements does not matter, the Insert operation simply does the following: 1. Insert the entry at the head of the list using ListHeadInsert 2. Increment numNodes. Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 13

  14. EraseOne  To erase one value, we must remove one node. Once again, since the order does not matter, we can choose to remove the head node. (It is the easiest to remove. Why?) So the algorithm for this operation is: 1. Compute a target pointer using ListSearch 2. If the target pointer is null, return false 3. Move the head node's data to the target node. 4. Remove the head node using ListHeadRemove 5. Decrement numNodes 6. Return true Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 14

  15. Finding the Next Occurrence  A common task is to find all the occurrences of a target in a linked list. This could be implemented by scanning each item and explicitly comparing them with the target.  However, we would like to separate this kind of linked list manipulation from the container semantics by using the ListSearch function.  Recall that ListSearch receives a head pointer and returns a pointer to the first node it encounters that contains the target. Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 15

  16. Finding the Next Occurrence  We can use ListSearch to find the next occurrence by giving the pointer to the node after the target's node as the new head pointer. That is, targetPtr->Link( ) can be thought of as the head pointer to the rest of the list.  The code idiom is then: targetPtr = ListSearch (headPtr, target); while (targetPtr != 0) { // do something with target's node targetPtr = targetPtr->Link(); targetPtr = ListSearch (targetPtr, target); } Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 16

  17. Count, Erase  The Count and Erase operations use the finding next occurrence idiom.  Count just increments a counter each time through the loop.  Erase uses the same technique as EraseOne to copy the head node's data into the target node, then remove the head node and decrement numNodes. Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 17

  18. operator+=  operator+= uses CopyList to make a copy of the addend's linked list.  Once again, since order does not matter, the original list is hooked up to the end of the copy via the copyTailPointer, and then copyHeadPointer becomes the new headPtr.  Finally, numNodes is updated with the count of the additional nodes. Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 18

  19. Destructor, Copy Constructor, Assignment Operator  ListClear is used by the destructor and the assignment operator to delete the linked list nodes.  ListCopy is used by the copy constructor and the assignment operator to create a copy of the source's linked list that is hooked up to the head pointer.  In all three, numNodes is set to the appropriate value. Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 19

  20. Grab  The Grab operation returns a randomly selected item from the bag.  Since this operation only makes sense for a Bag that is not empty, an assert is used to ensure this.  This function assumes that the random number generator has been seeded and use rand( ) as we have seen in Project 4 to get a number between 1 and Size( ).  ListLocate is used to find this node, then the node's data is returned. Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 20

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend