Lists and Sequences 372021 Sequences 1 Outline and

  • Slides: 16
Download presentation
Lists and Sequences 3/7/2021 Sequences 1

Lists and Sequences 3/7/2021 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) 3/7/2021 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 3/7/2021 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 3/7/2021 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 3/7/2021 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 3/7/2021 Object& element(): returns the element stored at this position bool is. Null(): returns true if this is a null position 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() 3/7/2021 n is. First(p), is. Last(p) n 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

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 3/7/2021 Sequences 8

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 3/7/2021 q B Sequences X C 9

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 3/7/2021 B Sequences C 10

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 3/7/2021 Sequences 11

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 3/7/2021 elem. At. Rank(r), replace. At. Rank(r, o), insert. At. Rank(r, o), remove. At. Rank(r) 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) 12

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 3/7/2021 Building block of more complex data structures Sequences 13

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 3/7/2021 2 Sequences l 14

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 3/7/2021 Sequences Array 1 1 1 List 1 n 1 1 1 n n 1 15

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 3/7/2021 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 16