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

lecture 23
SMART_READER_LITE
LIVE PREVIEW

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:


slide-1
SLIDE 1

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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?

slide-2
SLIDE 2

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 2

Outline

 Review linked list toolkit  Classes and linked lists  Example class: Bag class

 Attributes  Reimplementation of operations  Grab

slide-3
SLIDE 3

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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

slide-4
SLIDE 4

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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

  • perator be implemented.
slide-5
SLIDE 5

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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

slide-6
SLIDE 6

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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)

  • peration, 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).

slide-7
SLIDE 7

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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 numNodes 2 34 15

slide-8
SLIDE 8

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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".

slide-9
SLIDE 9

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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.

slide-10
SLIDE 10

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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.

slide-11
SLIDE 11

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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

  • ne for the Node class.

 Demo program is exactly the same as the last

  • ne (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.

slide-12
SLIDE 12

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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.

slide-13
SLIDE 13

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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.
slide-14
SLIDE 14

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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
slide-15
SLIDE 15

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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.

slide-16
SLIDE 16

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 16

Finding the Next Occurrence

 We can use ListSearch to find the next

  • ccurrence 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); }

slide-17
SLIDE 17

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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.

slide-18
SLIDE 18

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 18

  • perator+=

 operator+= uses CopyList to make a copy of

the addend's linked list.

 Once again, since order does not matter, the

  • riginal 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.

slide-19
SLIDE 19

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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.

slide-20
SLIDE 20

Monday, October 18 CS 215 Fundamentals of Programming II - Lecture 23 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.