Sequences and Iterators 2004 Goodrich Tamassia Sequences and

  • Slides: 7
Download presentation
Sequences and Iterators © 2004 Goodrich, Tamassia Sequences and Iterators 1

Sequences and Iterators © 2004 Goodrich, Tamassia Sequences and Iterators 1

Sequence ADT (§ 5. 3) The Sequence ADT is the union of the Vector

Sequence ADT (§ 5. 3) The Sequence ADT is the union of the Vector and List ADTs Elements accessed by n n List-based methods: n Rank, or Position Generic methods: n size(), is. Empty() Vector-based methods: n elem. At. Rank(r), replace. At. Rank(r, o), insert. At. Rank(r, o), remove. At. Rank(r) © 2004 Goodrich, Tamassia first(), last(), prev(p), next(p), replace(p, o), insert. Before(p, o), insert. After(p, o), insert. First(o), insert. Last(o), remove(p) Bridge methods: n Sequences and Iterators at. Rank(r), rank. Of(p) 2

Applications of Sequences The Sequence ADT is a basic, generalpurpose, data structure for storing

Applications of Sequences The Sequence ADT is a basic, generalpurpose, data structure for storing an ordered collection of elements Direct applications: n n Generic replacement for stack, queue, vector, or list small database (e. g. , address book) Indirect applications: n Building block of more complex data structures © 2004 Goodrich, Tamassia Sequences and Iterators 3

Linked List Implementation A doubly linked list provides a reasonable implementation of the Sequence

Linked List Implementation A doubly linked list provides a reasonable implementation of the Sequence ADT Nodes implement Position and store: n n n element link to the previous node link to the next node Position-based methods run in constant time Rank-based methods require searching from header or trailer while keeping track of ranks; hence, run in linear time Special trailer and header nodes/positions header trailer elements © 2004 Goodrich, Tamassia Sequences and Iterators 4

Array-based Implementation elements We use a circular array storing positions A position object stores:

Array-based Implementation elements We use a circular array storing positions A position object stores: n n Element Rank Indices f and l keep track of first and last positions 0 1 3 positions S f © 2004 Goodrich, Tamassia 2 Sequences and Iterators l 5

Sequence Implementations Operation size, is. Empty at. Rank, rank. Of, elem. At. Rank first,

Sequence Implementations Operation size, is. Empty at. Rank, rank. Of, elem. At. Rank first, last, prev, next Array 1 1 1 List 1 n 1 replace. At. Rank insert. At. Rank, remove. At. Rank insert. First, insert. Last insert. After, insert. Before remove 1 1 n n 1 1 1 © 2004 Goodrich, Tamassia Sequences and Iterators 6

Iterators (§ 5. 4) An iterator abstracts the process of scanning through a collection

Iterators (§ 5. 4) An iterator abstracts the process of scanning through a collection of elements Methods of the Object. Iterator ADT: n n object() boolean has. Next() object next. Object() reset() Extends the concept of Position by adding a traversal capability Implementation with an array or singly linked list © 2004 Goodrich, Tamassia An iterator is typically associated with an another data structure We can augment the Stack, Queue, Vector, List and Sequence ADTs with method: n Object. Iterator elements() Two notions of iterator: n n Sequences and Iterators snapshot: freezes the contents of the data structure at a given time dynamic: follows changes to the data structure 7