Programming in C++ Before Inserting 64 into an Unsorted List length 3 data [01 15 39 The item will be placed 90 into the length location, 3 and length will be incremented item 64 MAX LENGTH-1
11 Before Inserting 64 into an Unsorted List length 3 data [ 0 ] 15 [ 1 ] 39 [ 2 ] -90 [ 3 ] . . . [MAX_LENGTH-1] The item will be placed into the length location, and length will be incremented. item 64
Programming in C++ After Inserting 64 into an Unsorted List length data [01 15 39 90 64 item 64 MAX LENGTH-1 12
12 After Inserting 64 into an Unsorted List length 4 data [ 0 ] 15 [ 1 ] 39 [ 2 ] -90 [ 3 ] 64 . . . [MAX_LENGTH-1] item 64
Programming in C++ bool List: IsEmpty const // Post: Function value = true, if length ==0 = false. otherwise return( length ==0); bool List : IsPresent(/*in */ItemType item)const // Searches the list for item, reporting whether it was found // Post: Function value = true, if item is in data[0. length-1] false. otherwise int index =0 while( index length & item data index]) index++ return( index length); 13
13 bool List :: IsEmpty ( ) const // Post: Function value == true, if length == 0 // == false, otherwise { return ( length == 0 ) ; } bool List :: IsPresent ( /* in */ ItemType item ) const // Searches the list for item, reporting whether it was found // Post: Function value == true, if item is in data [ 0 . . length-1 ] // == false, otherwise { int index = 0 ; while ( index < length && item != data [ index ] ) index++ ; return ( index < length ) ; }
Programming in C++ void List Delete( /in */Item Type item // Pre: length>0&& item is assigned // Post: F item is in data array at entry ∥∥∥∥ First occurrence of item is no longer in array & length = length @entry-1 ELSE length and data array are unchanged int index =0; while( index length & item I= data index 1) index++, //if item found, move last element into item's place if( index length data index ]= data [length-11: length-: 14
14 void List :: Delete ( /* in */ ItemType item ) // Pre: length > 0 && item is assigned // Post: IF item is in data array at entry // First occurrence of item is no longer in array // && length == length@entry - 1 // ELSE // length and data array are unchanged { int index = 0 ; while ( index < length && item != data [ index ] ) index++; // if item found, move last element into item’s place if ( index < length ) { data [ index ] = data [length - 1 ] ; length-- ; } }
Programming in C++ Deleting 39 from an Unsorted List length index 0 data [01 15 III 39 90 39 has not been matched item 39 MAX LENGTH-1 15
15 Deleting 39 from an Unsorted List index : 0 39 has not been matched. item 39 length 4 data [ 0 ] 15 [ 1 ] 39 [ 2 ] -90 [ 3 ] 64 . . . [MAX_LENGTH-1]