Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 1
Lecture 20
Log into Linux. Copy files on csserver from /home/hwang/cs215/lecture20/*.* into a separate directory.
Midterm exam review sheet has been posted. Reminder: Homework 9 is due at the beginning
of class on Monday, no late work accepted, to be covered during the exam review.
Questions?
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 2
Outline
Review linked list toolkit Classes and linked lists Example class: Bag class
Attributes Reimplementation of operations grab operation
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 3
Review: Linked-List Toolkit
Operations on linked lists. All receive node pointer, often the head pointer. list_length list_write list_head_insert, list_insert list_search, list_locate list_head_remove, list_remove, list_clear list_copy
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 4
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
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 5
In-class Exercise: list_copy
Implement this function in node1.cpp (of last class exercise)
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 a copy of the source's head node and make head_ptr and tail_ptr point to it.
4. For each of the remaining source nodes, make a copy and add at the tail of the new list.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 6
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.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 7
Bag Class Attributes
This bag class will be implemented using a linked list. There are two attributes: head_ptr – a pointer to the head node of the linked
list that contains the bag items num_nodes – an integer that keeps track of the
number of nodes in the linked list as they are inserted and removed
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 8
Bag Class Attributes
Note that num_nodes is not required as we could use list_length( ) to implement the size( ) operation. However, doing so makes size( ) an O(n) operation, since list_length( ) accesses every node in the list.
By keeping track of the nodes as they are inserted and removed, the size( ) operation becomes O(1) as it is for the array implementations of the bag class.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 9
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);
head_ptr
num_nodes 2
34 15
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 10
Node Class Files
Copy your node1.h and node1.cpp files from last in-class exercise to the directory with today's files.
In node1.h, change the typedef to use int (rather than double).
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 11
Bag Class Definition
Examine file bag3.h
Has the same typedefs as the other bag classes, but value_type is the node struct 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.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 12
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. Destructor, copy constructor, and assignment
operator have been added.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 13
Bag Class Usage
Examine files Makefile.Inclass20 and bag3demo.cpp
The makefile now has three .o targets, one for the main program, one for the bag class, and one for the linked-list toolkit (in node1.cpp).
Demo program is exactly the same as the last one (Lecture 15). 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.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 14
Bag Class Implementation
Examine file bag3.cpp
As before, we ignore any bad_alloc exceptions. The default constructor just initializes the head
pointer to null and num_nodes to 0 to represent an empty list/bag.
The size( ) operation returns num_nodes attribute.
The rest of the bag operations use the linked-list operations from the toolkit.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 15
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 list_head_insert( )
2. Increment num_nodes.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 16
erase_one
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. Find a target pointer using list_search( )2. If the target pointer is null, return false3. Move the head node's data to the target node.4. Remove the head node using list_head_remove( )5. Decrement num_nodes6. Return true
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 17
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 list_search( ) function.
Recall that list_search( ) receives a head pointer and returns a pointer to the first node it encounters that contains the target.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 18
Finding the Next Occurrence
We can use list_search( ) to find the next occurrence by giving the pointer to the node after the target's node as the new head pointer. That is, target_ptr->link can be thought of as the head pointer to the rest of the list.
The code idiom is then:target_ptr = list_search (head_ptr, target);while (target_ptr != 0){ // do something with target's node target_ptr = target_ptr>link; target_ptr = list_search (target_ptr, target);}
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 19
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 erase_one( ) to copy the head node's data into the target node, then remove the head node and decrement num_nodes.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 20
operator+=
operator+= uses copy_list( ) 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 copy_tail_pointer, and then copy_head_pointer becomes the new head_ptr.
Finally, num_nodes is updated with the count of the additional nodes.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 21
Destructor, Copy Constructor, Assignment Operator
list_clear( ) is used by the destructor and the assignment operator to delete the linked list nodes.
list_copy( ) 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, num_nodes is set to the appropriate value.
Friday, February 25 CS 215 Fundamentals of Programming II - Lecture 20 22
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 in Project 4 to get a number between 1 and size( ).
list_locate( ) is used to find this node, then the node's data is returned.