11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

94 Chap. 4 Lists, Stacks, <strong>and</strong> Queues4.1 ListsWe all have an intuitive underst<strong>and</strong>ing of wh<strong>at</strong> we mean by a “list.” Our first step isto define precisely wh<strong>at</strong> is meant so th<strong>at</strong> this intuitive underst<strong>and</strong>ing can eventuallybe converted into a concrete d<strong>at</strong>a structure <strong>and</strong> its oper<strong>at</strong>ions. The most importantconcept rel<strong>at</strong>ed to lists is th<strong>at</strong> of position. In other words, we perceive th<strong>at</strong> thereis a first element in the list, a second element, <strong>and</strong> so on. We should view a list asembodying the m<strong>at</strong>hem<strong>at</strong>ical concepts of a sequence, as defined in Section 2.1.We define a list to be a finite, ordered sequence of d<strong>at</strong>a items known as elements.“Ordered” in this definition means th<strong>at</strong> each element has a position in thelist. (We will not use “ordered” in this context to mean th<strong>at</strong> the list elements aresorted by value.) Each list element has a d<strong>at</strong>a type. In the simple list implement<strong>at</strong>ionsdiscussed in this chapter, all elements of the list have the same d<strong>at</strong>a type,although there is no conceptual objection to lists whose elements have differingd<strong>at</strong>a types if the applic<strong>at</strong>ion requires it (see Section 12.1). The oper<strong>at</strong>ions definedas part of the list ADT do not depend on the elemental d<strong>at</strong>a type. For example, thelist ADT can be used for lists of integers, lists of characters, lists of payroll records,even lists of lists.A list is said to be empty when it contains no elements. The number of elementscurrently stored is called the length of the list. The beginning of the list iscalled the head, the end of the list is called the tail. There might or might not besome rel<strong>at</strong>ionship between the value of an element <strong>and</strong> its position in the list. Forexample, sorted lists have their elements positioned in ascending order of value,while unsorted lists have no particular rel<strong>at</strong>ionship between element values <strong>and</strong>positions. This section will consider only unsorted lists. Chapters 7 <strong>and</strong> 9 tre<strong>at</strong> theproblems of how to cre<strong>at</strong>e <strong>and</strong> search sorted lists efficiently.When presenting the contents of a list, we use the same not<strong>at</strong>ion as was introducedfor sequences in Section 2.1. To be consistent with Java array indexing,the first position on the list is denoted as 0. Thus, if there are n elements in thelist, they are given positions 0 through n − 1 as 〈a 0 , a 1 , ..., a n−1 〉. The subscriptindic<strong>at</strong>es an element’s position within the list. Using this not<strong>at</strong>ion, the empty listwould appear as 〈〉.Before selecting a list implement<strong>at</strong>ion, a program designer should first considerwh<strong>at</strong> basic oper<strong>at</strong>ions the implement<strong>at</strong>ion must support. Our common intuitionabout lists tells us th<strong>at</strong> a list should be able to grow <strong>and</strong> shrink in size as we insert<strong>and</strong> remove elements. We should be able to insert <strong>and</strong> remove elements from anywherein the list. We should be able to gain access to any element’s value, either toread it or to change it. We must be able to cre<strong>at</strong>e <strong>and</strong> clear (or reinitialize) lists. Itis also convenient to access the next or previous element from the “current” one.The next step is to define the ADT for a list object in terms of a set of oper<strong>at</strong>ionson th<strong>at</strong> object. We will use the Java not<strong>at</strong>ion of an interface to formally define the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!