outline and reading
play

Outline and Reading Singly linked list Lists and Sequences - PDF document

Outline and Reading Singly linked list Lists and Sequences Position ADT and List ADT (2.2.2) Doubly linked list ( 2.2.2) Sequence ADT ( 2.2.3) Implementations of the sequence ADT ( 2.2.3) Iterators (2.2.3) 6/8/2002 2:15 PM


  1. Outline and Reading Singly linked list Lists and Sequences Position ADT and List ADT (§2.2.2) Doubly linked list (§ 2.2.2) Sequence ADT (§ 2.2.3) Implementations of the sequence ADT (§ 2.2.3) Iterators (2.2.3) 6/8/2002 2:15 PM Sequences 1 6/8/2002 2:15 PM Sequences 2 Singly Linked List Stack with a Singly Linked List A singly linked list is a We can implement a stack with a singly linked list concrete data structure next The top element is stored at the first node of the list consisting of a sequence The space used is O ( n ) and each operation of the of nodes Stack ADT takes O (1) time Each node stores node � element elem nodes � link to the next node t ∅ ∅ A B C D elements 6/8/2002 2:15 PM Sequences 3 6/8/2002 2:15 PM Sequences 4 Queue with a Singly Linked List Position ADT We can implement a queue with a singly linked list The Position ADT models the notion of � The front element is stored at the first node place within a data structure where a � The rear element is stored at the last node single object is stored The space used is O ( n ) and each operation of the It gives a unified view of diverse ways Queue ADT takes O (1) time r of storing data, such as nodes � a cell of an array � a node of a linked list f ∅ Just one method: � object element(): returns the element stored at the position elements 6/8/2002 2:15 PM Sequences 5 6/8/2002 2:15 PM Sequences 6

  2. List ADT Doubly Linked List A doubly linked list provides a natural prev next The List ADT models a Accessor methods: implementation of the List ADT sequence of positions Nodes implement Position and store: � first(), last() storing arbitrary objects � element � before(p), after(p) � link to the previous node elem It establishes a Update methods: node � link to the next node before/after relation � replaceElement(p, o), Special trailer and header nodes between positions swapElements(p, q) trailer � insertBefore(p, o), nodes/positions header Generic methods: insertAfter(p, o), � size(), isEmpty() � insertFirst(o), Query methods: insertLast(o) � isFirst(p), isLast(p) � remove(p) elements 6/8/2002 2:15 PM Sequences 7 6/8/2002 2:15 PM Sequences 8 Insertion Deletion We visualize remove(p), where p = last() We visualize operation insertAfter(p, X), which returns position q p p A B C A B C D p A B C p q A B C X D p q A B X C A B C 6/8/2002 2:15 PM Sequences 9 6/8/2002 2:15 PM Sequences 10 Performance Sequence ADT In the implementation of the List ADT The Sequence ADT is the List-based methods: union of the Vector and by means of a doubly linked list � first(), last(), List ADTs before(p), after(p), � The space used by a list with n elements is Elements accessed by replaceElement(p, o), O ( n ) swapElements(p, q), � Rank, or insertBefore(p, o), � Position � The space used by each position of the list insertAfter(p, o), Generic methods: is O (1) insertFirst(o), � size(), isEmpty() insertLast(o), � All the operations of the List ADT run in Vector-based methods: remove(p) O (1) time � elemAtRank(r), Bridge methods: � Operation element() of the replaceAtRank(r, o), � atRank(r), rankOf(p) insertAtRank(r, o), Position ADT runs in O (1) time removeAtRank(r) 6/8/2002 2:15 PM Sequences 11 6/8/2002 2:15 PM Sequences 12

  3. Applications of Sequences Array-based Implementation elements We use a The Sequence ADT is a basic, general- circular array purpose, data structure for storing an ordered storing collection of elements positions Direct applications: A position object stores: � Generic replacement for stack, queue, vector, or � Element 0 1 2 3 list � Rank � small database (e.g., address book) positions Indices f and l keep track of Indirect applications: first and last � Building block of more complex data structures S positions f l 6/8/2002 2:15 PM Sequences 13 6/8/2002 2:15 PM Sequences 14 Sequence Implementations Iterators Operation Array List An iterator abstracts the An iterator is typically process of scanning through associated with an another size, isEmpty 1 1 a collection of elements data structure atRank, rankOf, elemAtRank 1 n Methods of the ObjectIterator We can augment the Stack, ADT: first, last, before, after 1 1 Queue, Vector, List and � object object() Sequence ADTs with method: replaceElement, swapElements 1 1 � boolean hasNext() � ObjectIterator elements() � object nextObject() replaceAtRank 1 n Two notions of iterator: � reset() insertAtRank, removeAtRank � snapshot: freezes the n n Extends the concept of contents of the data Position by adding a traversal insertFirst, insertLast 1 1 structure at a given time capability � dynamic: follows changes to insertAfter, insertBefore n 1 Implementation with an array the data structure or singly linked list remove n 1 6/8/2002 2:15 PM Sequences 15 6/8/2002 2:15 PM Sequences 16

Recommend


More recommend