Chapter 4If memory is available in an area called the free store (or heap), operator new allocates...
Transcript of Chapter 4If memory is available in an area called the free store (or heap), operator new allocates...
![Page 1: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/1.jpg)
Chapter 4 ADT Sorted List
![Page 2: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/2.jpg)
Sorted Type Class Interface Diagram SortedType class
IsFull
GetLength
ResetList
DeleteItem
PutItem
MakeEmpty
GetItem
Private data: length
info [ 0 ] [ 1 ] [ 2 ]
[MAX_ITEMS-1]
currentPos
GetNextItem
![Page 3: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/3.jpg)
Member functions
Which member function specifications and implementations must change to ensure that any instance of the Sorted List ADT remains sorted at all times?
• PutItem
• DeleteItem
![Page 4: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/4.jpg)
InsertItem algorithm for SortedList ADT
• Find proper location for the new element in the sorted list.
• Create space for the new element by moving down all the list elements that will follow it.
• Put the new element in the list.
• Increment length.
![Page 5: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/5.jpg)
Implementing SortedType member function PutItem
// IMPLEMENTATION FILE (sorted.cpp)
#include “itemtype.h” // also must appear in client code
void SortedType :: PutItem ( ItemType item ) // Pre: List has been initialized. List is not full. // item is not in list. // List is sorted by key member using function ComparedTo. // Post: item is in the list. List is still sorted. { . . .
}
![Page 6: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/6.jpg)
void SortedType :: PutItem ( ItemType item ) { bool moreToSearch; int location = 0; // find proper location for new element moreToSearch = ( location < length ); while ( moreToSearch ) { switch ( item.ComparedTo( info[location] ) ) { case LESS : moreToSearch = false; break; case GREATER : location++; moreToSearch = ( location < length ); break; } } // make room for new element in sorted list for ( int index = length ; index > location ; index-- ) info [ index ] = info [ index - 1 ]; info [ location ] = item; length++; }
![Page 7: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/7.jpg)
DeleteItem algorithm for SortedList ADT
• Find the location of the element to be deleted from the sorted list.
• Eliminate space occupied by the item by moving up all the list elements that follow it.
• Decrement length.
![Page 8: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/8.jpg)
Implementing SortedType member function DeleteItem
// IMPLEMENTATION FILE continued (sorted.cpp)
void SortedType :: DeleteItem ( ItemType item ) // Pre: List has been initialized. // Key member of item is initialized. // Exactly one element in list has a key matching item’s key. // List is sorted by key member using function ComparedTo. // Post: No item in list has key matching item’s key. // List is still sorted. { . . .
}
![Page 9: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/9.jpg)
void SortedType :: DeleteItem ( ItemType item ) { int location = 0; // find location of element to be deleted while ( item.ComparedTo ( info[location] ) != EQUAL ) location++; // move up elements that follow deleted item in sorted list for ( int index = location + 1 ; index < length; index++ ) info [ index - 1 ] = info [ index ];
length--; }
![Page 10: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/10.jpg)
Improving member function GetItem
Recall that with the Unsorted List ADT we examined each list element beginning with info[ 0 ], until we either found a matching key, or we had examined all the elements in the Unsorted List.
How can the searching algorithm be improved for Sorted List ADT?
![Page 11: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/11.jpg)
Retrieving Eliot from a Sorted List
The sequential search for Eliot can stop when Hsing has been examined.
length 4
info [ 0 ] Asad
[ 1 ] Bradley
[ 2 ] Hsing
[ 3 ] Maxwell . . . [MAX_ITEMS-1]
Why?
![Page 12: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/12.jpg)
Binary Seach in a Sorted List• Examines the element in the middle of the array. Is
it the sought item? If so, stop searching. Is the middle element too small? Then start looking in second half of array. Is the middle element too large? Then begin looking in first half of the array.
• Repeat the process in the half of the list that should be examined next.
• Stop when item is found, or when there is nowhere else to look and item has not been found.
![Page 13: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/13.jpg)
ItemType SortedType::GetItem ( ItemType item, bool& found ) // Pre: Key member of item is initialized. // Post: If found, item’s key matches an element’s key in the list // and a copy of that element is returned; otherwise, // original item is returned. { int midPoin; int first = 0; int last = length - 1; bool moreToSearch = ( first <= last );
found = false; while ( moreToSearch && !found ) { midPoint = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT switch ( item.ComparedTo( info [ midPoint ] ) ) { case LESS : . . . // LOOK IN FIRST HALF NEXT case GREATER : . . . // LOOK IN SECOND HALF NEXT case EQUAL : . . . // ITEM HAS BEEN FOUND } } }
![Page 14: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/14.jpg)
Trace of Binary Search
info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
15 26 38 57 62 78 84 91 108 119
item = 45
first midPoint last
info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
15 26 38 57 62 78 84 91 108 119
first midPoint last
LESS last = midPoint - 1
GREATER first = midPoint + 1
![Page 15: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/15.jpg)
Trace continued
info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
15 26 38 57 62 78 84 91 108 119
item = 45
first, midPoint, last
info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
15 26 38 57 62 78 84 91 108 119
first, last midPoint
LESS last = midPoint - 1
GREATER first = midPoint + 1
![Page 16: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/16.jpg)
Trace concludes
info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
15 26 38 57 62 78 84 91 108 119
item = 45
last first
first > last found = false
![Page 17: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/17.jpg)
ItemType SortedType::GetItem ( ItemType item, bool& found ) // ASSUMES info ARRAY SORTED IN ASCENDING ORDER { int midPoint; int first = 0; int last = length - 1; bool moreToSearch = ( first <= last ); found = false;
while ( moreToSearch && !found ) { midPoint = ( first + last ) / 2 ; switch ( item.ComparedTo( info [ midPoint ] ) ) { case LESS : last = midPoint - 1; moreToSearch = ( first <= last ); break; case GREATER : first = midPoint + 1; moreToSearch = ( first <= last ); break; case EQUAL : found = true ; item = info[ midPoint ]; break; } } return item; }
![Page 18: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/18.jpg)
Allocation of memory
STATIC ALLOCATION
Static allocation is the allocation of memory space at compile time.
DYNAMIC ALLOCATION
Dynamic allocation is the allocation of memory space at run time by using operator new.
![Page 19: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/19.jpg)
3 Kinds of Program Data
• STATIC DATA: memory allocation exists throughout execution of program.
static long SeedValue;
• AUTOMATIC DATA: automatically created at function entry, resides in activation frame of the function, and is destroyed when returning from function.
• DYNAMIC DATA: explicitly allocated and deallocated during program execution by C++ instructions written by programmer using unary operators new and delete
![Page 20: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/20.jpg)
Arrays created at run time
If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory allocated.
Otherwise, the NULL pointer 0 is returned.
The dynamically allocated object exists until the delete operator destroys it.
![Page 21: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/21.jpg)
Dynamic Array Allocation
char *ptr; // ptr is a pointer variable that // can hold the address of a char
ptr = new char[ 5 ]; // dynamically, during run time, allocates // memory for 5 characters and places into // the contents of ptr their beginning address
ptr
6000
6000
![Page 22: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/22.jpg)
Dynamic Array Allocation
char *ptr ; ptr = new char[ 5 ]; strcpy( ptr, “Bye” );
ptr[ 1 ] = ‘u’; // a pointer can be subscripted
std::cout << ptr[ 2] ;
ptr
6000
6000 ‘B’ ‘y’ ‘e’ ‘\0’ ‘u’
![Page 23: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/23.jpg)
class SortedType<char>
MakeEmpty
~SortedType
DeleteItem . . .
InsertItem
SortedType
RetrieveItem
GetNextItem
‘C’ ‘L’ ‘X’
Private data: length 3
listData
currentPos ?
![Page 24: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/24.jpg)
• Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc trails behind location.
• Create a new node and place item in it.
• Insert the node by adjusting pointers of predLoc and location .
• Increment length.
![Page 25: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/25.jpg)
Inchworm Effect: moving two pointers
![Page 26: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/26.jpg)
Inserting ‘S’ into a Sorted List
‘C’ ‘L’ ‘X’
Private data: length 3
listData
currentPos ?
predLoc location
moreToSearch
![Page 27: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/27.jpg)
Finding proper position for ‘S’
‘C’ ‘L’ ‘X’
Private data: length 3
listData
currentPos ?
predLoc locationNULL
moreToSearch true
as location->info < ’s’, move forward: predLoc = location; //predLoc catches up location = location->next; //location move one step forward
![Page 28: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/28.jpg)
Finding proper position for ‘S’
‘C’ ‘L’ ‘X’
Private data: length 3
listData
currentPos ?
predLoc location
moreToSearch true
location->info is still less than ’s’ move ahead…
![Page 29: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/29.jpg)
Finding Proper Position for ‘S’
‘C’ ‘L’ ‘X’
Private data: length 3
listData
currentPos ?
predLoc location
moreToSearch false
location->info is still larger than ’s’ stop!
’s’ should be inserted before ‘x’!
![Page 30: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/30.jpg)
Inserting ‘S’ into Proper Position
‘C’ ‘L’ ‘X’
Private data: length 4
listData
currentPos
predLoc location
moreToSearch false
‘S’
![Page 31: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/31.jpg)
How do the SortedList implementations compare?
![Page 32: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/32.jpg)
Why is a destructor needed?
When a local list variable goes out of scope, the memory space for data member listPtr is deallocated. But the nodes to which listPtr points are not deallocated.
A class destructor is used to deallocate the dynamic memory pointed to by the data member.
![Page 33: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/33.jpg)
Implementing the DestructorUnsortedType::~UnsortedType() // Post: List is empty; all items have// been deallocated. { NodeType* tempPtr; while (listData != NULL) { tempPtr = listData; listData = listData->next; delete tempPtr; }}
![Page 34: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/34.jpg)
Object-Oriented Design Methodology
• Four stages to the decomposition process • Brainstorming
• Filtering
• Scenarios
• Responsibility algorithms
![Page 35: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/35.jpg)
Brainstorming
• A group problem-solving technique that involves the spontaneous contribution of ideas from all members of the group • All ideas are potential good ideas • Think fast and furiously first, and ponder later • A little humor can be a powerful force
• Brainstorming is designed to produce a list of candidate classes
![Page 36: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/36.jpg)
Filtering
• Determine which are the core classes in the problem solution
• There may be two classes in the list that have many common attributes and behaviors
• There may be classes that really don’t belong in the problem solution
![Page 37: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/37.jpg)
Scenarios
• Simulate class interactions • Ask “What if?” questions • Assign responsibilities to each class • There are two types of responsibilities
• What a class must know about itself (knowledge)
• What a class must be able to do (behavior)
![Page 38: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/38.jpg)
Responsibility Algorithms
• The algorithms must be written for the responsibilities • Knowledge responsibilities usually just
return the contents of one of an object’s variables
• Action responsibilities are a little more complicated, often involving calculations
![Page 39: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/39.jpg)
Computer Example
• Let’s repeat the problem-solving process for creating an address list
• Brainstorming and filtering • Circling the nouns and underlining the verbs
![Page 40: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/40.jpg)
Computer Example
• First pass at a list of classes
![Page 41: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/41.jpg)
Computer Example
• Filtered list
![Page 42: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/42.jpg)
CRC Cards
![Page 43: Chapter 4If memory is available in an area called the free store (or heap), operator new allocates memory for the object or array and returns the address of (pointer to) the memory](https://reader034.fdocuments.us/reader034/viewer/2022042205/5ea702d4fe0dc617ce40b508/html5/thumbnails/43.jpg)
Responsibility Algorithms