< previous page page_806 next page >

Page 806
// Inserts item into its proper place in the sorted list

// Precondition;
//    length < MAX_FRIENDS
//  && list[0..length-1] are in ascending order
//  && item is assigned
// Postcondition:
//     item is in list
//  && length == length@entry + 1
//  && list[0..length-1] are in ascending order
//  && IF item was already in list@entry
//         item has been inserted before the one that was there

{
    Boolean placeFound;    // True if item is already in the list
    int     index;         // Position where item belongs
    int     count;         // Loop control variable

    SearchOrd(list, item, length, index, placeFound);

    // Shift list[index..length-1] down one

    for (count = length 1; count >= index; count==)

            // Invariant (prior to test):
            //     list[length-1..count+1] have been shifted down
            //  && length - 1 >= count >= index - 1

        list[count+1] = list[count];

    // Insert item

    list [ index] = item;

    // Increment length of list

    length++;
}

//******************************************************************

void SearchOrd(
         /* inout */ EntryType list[],     // List to be searched
         /* in */    EntryType item,       // Item to be found
         /* in */    int       length,     // Length of list
         /* out */   int&      index,      // Item location if found
         /* out */   Boolean&  found  )    // True if item is found

 
< previous page page_806 next page >