cosc 1p03 data structures and abstraction 10.1 the list if a is success in life, then a equals x...

12
COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut. Albert Einstein, Observer, Jan. 15, 1950

Upload: raegan-overall

Post on 30-Mar-2015

222 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.1

The List

If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut. Albert Einstein, Observer, Jan. 15, 1950

Page 2: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.2

List

· most general of list-oriented collections· an ordered collection (initially empty) of items (of some

type) to which items may be added and removed.· cursored-list

- cursor- operations relative to cursor- off list

· errors- off list- list overflow

Page 3: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.3

List Operations

· insertion?- before or after cursor?- at front- at end?

· access- at cursor- off list?

· deletion- at cursor

° off list?- cursor after deletion

° inverse of addition

Page 4: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.4

· traversal- moving to front- advancing- end of list

· search- key- from where- exhaustive search

· list as stack- insert without advance

· list as queue- insert with traverse to end

· sequential order- insert with advance

· sorted- traverse to find insertion point

Page 5: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.5

List Interface· generic

- E° getKey° Comparable from java.lang

· operations- insertion- deletion- access- length- traversal- search- off end?- iterator

· exceptions- NoSpaceException- NoItemException

Page 6: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.6

Iterators

· a device that allows traversal through a collection ADT· ADT extends Iterable<E> (from java.util)

- iterator returns an Iterator over the ADT· interface Iterator<E> in java.util

- methods° UnsupportedOperationException

· extended for· implementation

- must have intimate knowledge of ADT representation- place in same package & use package visibility in ADT

Page 7: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.7

List ADTContiguous

Representation· “variable-sized” array

- contiguity° reorganization (O(n))

- cursor is an index· instance variables· constructors

- empty list· operations

- insertion° create opening

×shift items RL° cursor

Page 8: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.8

- deletion

° fill gap×shift items LR

° cursor° removal of reference at length-1

- find° goal: move cursor° check each item in turn until found or off list

×short circuit operator×negation using de Morgan’s law

- iterator° create an iterator on this ADT

- remaining operations

Page 9: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.9

ConListIterator

· generic in item type (same as ConList)· iteration involves sequencing through index positions

from 0 to length-1· instance variables

- list it is iterating- has its own cursor

° must keep track of current position· constructor

- package visibility° only classes in package can create

- initialize cursor· methods

- access ConList instance variables directly (package visibility)

Page 10: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.10

List ADTLinked

Implementation· sequentially-linked structure

- sequential processing natural· cursor?

- cursor is Node reference- off list

· insertion- in front of cursor- precursor

° cursor pairs° header node

- at end?

Page 11: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.11

· representation- sequentially linked structure with header and two

cursors· empty list

- off list?· length

- keep count· comparison with contiguous

- insert/remove O(1) vs O(n)

Page 12: COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping

COSC 1P03

Data Structures and Abstraction 10.27

The End