Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lists and Sequences 5/24/2017 3:26 AM Sequences 1 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) 5/24/2017 3:26 AM Sequences 2 Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores next element link to the next node node elem A 5/24/2017 3:26 AM B C Sequences D 3 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 5/24/2017 3:26 AM Sequences 4 Queue with a Singly Linked List We can implement a queue with a singly linked list 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 5/24/2017 3:26 AM Sequences 5 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 a cell of an array a node of a linked list Member functions: Object& element(): returns the element stored at this position bool isNull(): returns true if this is a null position 5/24/2017 3:26 AM Sequences 6 List ADT The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: Accessor methods: size(), isEmpty() isFirst(p), isLast(p) 5/24/2017 3:26 AM Sequences first(), last() before(p), after(p) Update methods: Query methods: replaceElement(p, o), swapElements(p, q) insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o) remove(p) 7 Example 5.4 (page 219) P1, P2, P3,..., Pi are variables representing positions Operation Output S insertFirst (8) P1 (8) (8) insertAfter (P1 ,5 ) P2 (5) (8,5) insertBefore (P2 ,3 ) P3 (3) (8,3,5) insertFirst (9) p4 (9) (9,8,3,5) before (p3 ) P1 (8) (9,8,3,5) last () P2 (5) (9,8,3,5) remove (p4) - (8,3,5) swapElements (P1 ,P2) - (5,3,8) replaceElement (P3,7) - (5,7,8) insertAfter(first(),2) P5 (2) (5,2,7,8) 5/24/2017 3:26 AM Sequences 8 Doubly Linked List A doubly linked list provides a natural implementation of the List ADT Nodes implement Position and store: element link to the previous node link to the next node prev next elem node Special trailer and header nodes nodes/positions header trailer elements 5/24/2017 3:26 AM Sequences 9 Insertion We visualize operation insertAfter(p, X), which returns position q p A B C p A q B C X p A 5/24/2017 3:26 AM q B Sequences X C 10 Deletion We visualize remove(p), where p = last() A B C A B C p D p D A 5/24/2017 3:26 AM B Sequences C 11 Performance In the implementation of the List ADT by means of a doubly linked list 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 5/24/2017 3:26 AM Sequences 12 Sequence ADT The Sequence ADT is the union of the Vector and List ADTs Elements accessed by List-based methods: Rank, or Position Generic methods: size(), isEmpty() Vector-based methods: elemAtRank(r), replaceAtRank(r, o), insertAtRank(r, o), removeAtRank(r) 5/24/2017 3:26 AM first(), last(), before(p), after(p), replaceElement(p, o), swapElements(p, q), insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o), remove(p) Bridge methods: Sequences atRank(r), rankOf(p) 13 Applications of Sequences The Sequence ADT is a basic, generalpurpose, data structure for storing an ordered collection of elements Direct applications: Generic replacement for stack, queue, vector, or list small database (e.g., address book) Indirect applications: Building block of more complex data structures 5/24/2017 3:26 AM Sequences 14 Array-based Implementation elements We use a circular array storing positions A position object stores: Element Rank Indices f and l keep track of first and last positions 0 1 3 positions S f 5/24/2017 3:26 AM 2 Sequences l 15 Sequence Implementations Operation size, isEmpty atRank, rankOf, elemAtRank first, last, before, after replaceElement, swapElements replaceAtRank insertAtRank, removeAtRank insertFirst, insertLast insertAfter, insertBefore remove 5/24/2017 3:26 AM Sequences Array 1 1 1 1 1 n 1 n n List 1 n 1 1 n n 1 1 1 16 Iterators An iterator abstracts the process of scanning through a collection of elements Methods of the ObjectIterator ADT: boolean hasNext() object next() reset() ObjectIterator 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 5/24/2017 3:26 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 snapshot: freezes the contents of the data structure at a given time dynamic: follows changes to the data structure 17