286Chapter 9Abstract Data Types and Algorithmsappropriate item to print. Thus, theInsertoperation must place the items inthe list in the proper order. The list doesn’t have to know anything about thefields ofItemClass. ThecompareTomethod gives enough information.To see how this works, let’s look at an example. Let’s say we are keepingnumbers in numeric order. The list so far contains the following values:23, 46, 75, 1066, 1492, 2001and we want to insert 998. Compare 998 to 23 and it is larger, so wecompare 998 with the next value. 998 is larger than 46, so we compare 998with the next value. 998 is larger than 75, so we compare 998 with the nextvalue. 998 is less than 1066, so 998 goes in the list just before 1066. Webegin with the first item in the list and compare the value to be inserted. Aslong as the value we are inserting is greater than the next value in the list, wekeep moving down the list. When we find a place where the value to beinserted is less than the value in the list, we have found the insertion place.
This is the end of the preview.
access the rest of the document.
Meaning of life, Equals sign, item, abstract data types