9. Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem SortedType RetrieveItem GetNextItem Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos
10.
11.
12. Sorted Type Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos GetNextItem
13.
14.
15.
16.
17. Implementing SortedType me mber function InsertItem // IMPLEMENTATION FILE (sorted.cpp) #include “itemtype.h” // also must appear in client code void SortedType :: InsertItem ( 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. { . . . }
18.
19. InsertItem void SortedType :: InsertItem ( ItemType item ) { int location = 0 ; // find proper location for new element while ( location < length ) { if (item.key < info[location].key) break; else location++; } // make room for new element in sorted list for ( int index = length ; index > location ; index-- ) info [ index ] = info [ index - 1 ] ; info [ location ] = item ; length++ ; }
20.
21.
22.
23.
24.
25.
26.
27. void SortedType::RetrieveItem ( Item Type& 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 has been stored in item; otherwise, item is unchanged. { int midPoint ; int first = 0; int last = length - 1 ; found = false ; while ( first <= last && !found ) { midPoint = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT if (item.key < info[midPoint].key) last = midPoint – 1; else if (item.key > info[midPoint].key) first = midPoint + 1; else { found = true; item = info[midPoint]; } } }
28.
29.
30.
31.
32.
33.
Notes de l'éditeur
switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } ound
switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } ound
switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } ound
switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } ound
switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } ound