Lists and Sequences 11232020 11 09 AM Sequences

  • Slides: 17
Download presentation
Lists and Sequences 11/23/2020 11: 09 AM Sequences 1

Lists and Sequences 11/23/2020 11: 09 AM Sequences 1

Outline and Reading Singly linked list Position ADT and List ADT (§ 5. 2.

Outline and Reading Singly linked list Position ADT and List ADT (§ 5. 2. 1) Doubly linked list (§ 5. 2. 3) Sequence ADT (§ 5. 3. 1) Implementations of the sequence ADT (§ 5. 3. 3) Iterators (§ 5. 5) 11/23/2020 11: 09 AM Sequences 2

Singly Linked List A singly linked list is a concrete data structure consisting of

Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores n n next element link to the next node elem A 11/23/2020 11: 09 AM B C Sequences D 3

Stack with a Singly Linked List We can implement a stack with a singly

Stack with a Singly Linked List We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time nodes t elements 11/23/2020 11: 09 AM Sequences 4

Queue with a Singly Linked List We can implement a queue with a singly

Queue with a Singly Linked List We can implement a queue with a singly linked list n n The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time r nodes f elements 11/23/2020 11: 09 AM Sequences 5

Position ADT The Position ADT models the notion of place within a data structure

Position ADT The Position ADT models the notion of place within a data structure where a single object is stored A special null position refers to no object. Positions provide a unified view of diverse ways of storing data, such as n n a cell of an array a node of a linked list Member functions: n n Object& element(): returns the element stored at this position bool is. Null(): returns true if this is a null position 11/23/2020 11: 09 AM Sequences 6

List ADT The List ADT models a sequence of positions storing arbitrary objects It

List ADT The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: n Accessor methods: size(), is. Empty() is. First(p), is. Last(p) 11/23/2020 11: 09 AM n n Sequences first(), last() before(p), after(p) Update methods: n Query methods: n n replace. Element(p, o), swap. Elements(p, q) insert. Before(p, o), insert. After(p, o), insert. First(o), insert. Last(o) remove(p) 7

Example 5. 4 (page 219) P 1, P 2, P 3, . . .

Example 5. 4 (page 219) P 1, P 2, P 3, . . . , Pi are variables representing positions Operation Output S insert. First (8) P 1 (8) insert. After (P 1 , 5 ) P 2 (5) (8, 5) insert. Before (P 2 , 3 ) P 3 (3) (8, 3, 5) insert. First (9) p 4 (9) (9, 8, 3, 5) before (p 3 ) P 1 (8) (9, 8, 3, 5) last () P 2 (5) (9, 8, 3, 5) remove (p 4) - (8, 3, 5) swap. Elements (P 1 , P 2) - (5, 3, 8) replace. Element (P 3, 7) - (5, 7, 8) insert. After(first(), 2) P 5 (2) (5, 2, 7, 8) 11/23/2020 11: 09 AM Sequences 8

Doubly Linked List A doubly linked list provides a natural implementation of the List

Doubly Linked List A doubly linked list provides a natural implementation of the List ADT Nodes implement Position and store: n n n element link to the previous node link to the next node prev next elem node Special trailer and header nodes/positions header trailer elements 11/23/2020 11: 09 AM Sequences 9

Insertion We visualize operation insert. After(p, X), which returns position q p A B

Insertion We visualize operation insert. After(p, X), which returns position q p A B C p A q B C X p A 11/23/2020 11: 09 AM q B Sequences X C 10

Deletion We visualize remove(p), where p = last() A B C p D A

Deletion We visualize remove(p), where p = last() A B C p D A 11/23/2020 11: 09 AM B Sequences C 11

Performance In the implementation of the List ADT by means of a doubly linked

Performance In the implementation of the List ADT by means of a doubly linked list n n The space used by a list with n elements is O(n) The space used by each position of the list is O(1) All the operations of the List ADT run in O(1) time Operation element() of the Position ADT runs in O(1) time 11/23/2020 11: 09 AM Sequences 12

Sequence ADT The Sequence ADT is the union of the Vector and List ADTs

Sequence ADT 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) 11/23/2020 11: 09 AM first(), last(), before(p), after(p), replace. Element(p, o), swap. Elements(p, q), insert. Before(p, o), insert. After(p, o), insert. First(o), insert. Last(o), remove(p) Bridge methods: n Sequences at. Rank(r), rank. Of(p) 13

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 11/23/2020 11: 09 AM Sequences 14

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 11/23/2020 11: 09 AM 2 Sequences l 15

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, before, after replace. Element, swap. Elements replace. At. Rank insert. At. Rank, remove. At. Rank insert. First, insert. Last insert. After, insert. Before remove 11/23/2020 11: 09 AM Sequences Array 1 1 1 List 1 n 1 1 1 n n 1 16

Iterators An iterator abstracts the process of scanning through a collection of elements Methods

Iterators An iterator abstracts the process of scanning through a collection of elements Methods of the Object. Iterator ADT: n n n boolean has. Next() object next() reset() n Object. Iterator elements() Two notions of iterator: Extends the concept of position by adding a traversal capability May be implemented with an array or singly linked list 11/23/2020 11: 09 AM An iterator is typically associated with an another data structure We can augment the Stack, Queue, Vector, List and Sequence ADTs with method: Sequences n n snapshot: freezes the contents of the data structure at a given time dynamic: follows changes to the data structure 17