Dale - Computer Science Illuminated 305

Dale - Computer Science Illuminated 305 - that the...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
278 Chapter 9 Abstract Data Types and Algorithms Figure 9.1 A list length [0] [1] [2] [3] [length–1] [MAX_LENGTH–1] list saying that an array can be used to hold the items in the container. Items in a container can be unordered or they can be ordered in some fashion. If there is no ordering on the item in the container, we call the container unsorted . If there is an ordering, we call the container sorted . In this discussion we use the term list to mean a generic kind of container. Later we discuss the list as a specific kind of container by describing a set of behaviors. Figure 9.1 shows a list. A list is made up of a length variable and an array to hold the list items. All processing of the logical list is done from the 0 th position in the array variable list through the length – 1 position. In an unsorted list, the component that comes before or after an item has no semantic relationship with it. It only shows the order in which the items were stored. In a sorted list, the items are arranged in such a way
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: that the component that comes before or after an item has a semantic rela-tionship with that item. For example, a grade list can be a random list of numbers or sorted by value. Figures 9.2 and 9.3 show an unsorted list of grades and a sorted list of grades, respectively. In an array-based implementation, accessing the first item is accessing the list[0] position. In order to move through the items in the list, we need an integer variable that starts at and stops when its value is length . Be careful: Keep in mind that the array goes from index through index MAX_LENGTH 1 ; the container goes from index through length 1 . Forgetting this is a common cause for errors in programming. Put item means that given an index, shift the items that follow down one slot in the array and store the item at the index position. Remove the item means that given an index, shift the items that follow up one slot in the array....
View Full Document

Ask a homework question - tutors are online