Recall What is a Data Structure Very Fundamental





























![Reading • [G] Section 7. 1 and 7. 2 [G] Do not read subsection Reading • [G] Section 7. 1 and 7. 2 [G] Do not read subsection](https://slidetodoc.com/presentation_image_h/2f64e6601e0c142a9c4c34d5f649e7ee/image-30.jpg)
- Slides: 30
Recall • • What is a Data Structure Very Fundamental Data Structures Arrays • What Arrays are good for • What Arrays do poorly • • Linked Lists Singly Linked List • Add h/t, Remove h/t Doubly Linked List • Add, Remove • • 1
Recall too • Equivalence Shallow & Deep • • Cloning a data structure Shallow & Deep • • Stacks • Queues 2
Application: Round Robin Schedulers We can implement a round robin scheduler using a queue Q by repeatedly performing the following steps: q 1. 2. 3. e = Q. dequeue() Service element e Q. enqueue(e) Queue Dequeue Enqueue Shared Service © 2014 Goodrich, Tamassia, Goldwasser List and Iterator ADTs 3
Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Lists ADT and Iterators © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 1
The java. util. List ADT q The java. util. List interface includes the following methods: © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 2
Example q A sequence of List operations: © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 3
Array Lists q q An obvious choice for implementing the list ADT is to use an array, A, where A[i] stores (a reference to) the element with index i. With a representation based on an array A, the get(i) and set(i, e) methods are easy to implement by accessing A[i] (assuming i is a legitimate index). A 0 1 2 © 2014 Goodrich, Tamassia, Goldwasser i n Listand Iterator ADTs Lists Iterators 4
Insertion q q In an operation add(i, o), we need to make room for the new element by shifting forward the n - i elements A[i], …, A[n - 1] In the worst case (i = 0), this takes O(n) time A 0 1 2 i n 0 1 2 o i A A © 2014 Goodrich, Tamassia, Goldwasser n Listand Iterator ADTs Lists Iterators 5
Element Removal q q In an operation remove(i), we need to fill the hole left by the removed element by shifting backward the n - i - 1 elements A[i + 1], …, A[n - 1] In the worst case (i = 0), this takes O(n) time A 0 1 2 o i n 0 1 2 i A A © 2014 Goodrich, Tamassia, Goldwasser n Listand Iterator ADTs Lists Iterators 6
Performance q In an array-based implementation of a dynamic list: n n n q The space used by the data structure is O(n) Indexing the element at i takes O(1) time add and remove run in O(n) time In an add operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one … © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 7
Java Implementation © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 8
Java Implementation, 2 © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 9
Growable Array-based Array List q q q Let push(o) be the operation that adds element o at the end of the list When the array is full, we replace the array with a larger one How large should the new array be? n n Incremental strategy: increase the size by a constant c Doubling strategy: double the size © 2014 Goodrich, Tamassia, Goldwasser Algorithm push(o) if t = S. length - 1 then A ← new array of size … for i ← 0 to n-1 do A [i ] ← S [i ] S← A n ← n +1 S[n-1] ← o Listand Iterator ADTs Lists Iterators 10
Growable Array-based Array List Algorithm push(o) if t = S. length - 1 then A ← new array of size … for i ← 0 to n-1 do A [i ] ← S [i ] S← A n ← n +1 S[n-1] ← o © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 10
Positional Lists q q To provide for a general abstraction of a sequence of elements with the ability to identify the location of an element, we define a positional list ADT. A position acts as a marker or token within the broader positional list. A position p is unaffected by changes elsewhere in a list; the only way in which a position becomes invalid is if an explicit command is issued to delete it. A position instance is a simple object, supporting only the following method: n P. get. Element( ): Return the element stored at position p. © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 11
Positional List ADT q Accessor methods: © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 12
Positional List ADT, 2 q Update methods: © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 13
Example q A sequence of Positional List operations: © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 14
Positional List Implementation q The most natural way to implement a positional list is with a doubly-linked list. header prev next element nodes/positions node trailer elements © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 15
Insertion q Insert a new node, q, between p and its successor. p A B C p A q B C X p A © 2014 Goodrich, Tamassia, Goldwasser q B X Listand Iterator ADTs Lists Iterators C 16
Deletion q Remove a node, p, from a doubly-linked list. A B C p D A © 2014 Goodrich, Tamassia, Goldwasser B C Listand Iterator ADTs Lists Iterators 17
Iterators q An iterator is a software design pattern that abstracts the process of scanning through a sequence of elements, one element at a time. © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 18
The Iterable Interface q Java defines a parameterized interface, named Iterable, that includes the following single method: n q q iterator( ): Returns an iterator of the elements in the collection. An instance of a typical collection class in Java, such as an Array. List, is iterable (but not itself an iterator); it produces an iterator for its collection as the return value of the iterator( ) method. Each call to iterator( ) returns a new iterator instance, thereby allowing multiple (even simultaneous) traversals of a collection. © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 19
The for-each Loop q Java’s Iterable class also plays a fundamental role in support of the “for-each” loop syntax: is equivalent to: © 2014 Goodrich, Tamassia, Goldwasser Listand Iterator ADTs Lists Iterators 20
The for-each Loop q But to use remove you must : © 2014 Goodrich, Tamassia, Goldwasser List and Iterator ADTs 20
Implementing Iterators q Snapshot iterator q Lazy iterator © 2014 Goodrich, Tamassia, Goldwasser List and Iterator ADTs 26
The Java Collections Framework q part of the java. util package q Children of Collection interface © 2014 Goodrich, Tamassia, Goldwasser List and Iterator ADTs 27
The Java Collections Framework Queues List and Iterator ADTs © 2014 Goodrich, Tamassia, Goldwasser 28
See the book for q List Iterators in Java n q Java way Vs book way List-Based Algorithms in the Java Collections Framework © 2014 Goodrich, Tamassia, Goldwasser List and Iterator ADTs 29
Reading • [G] Section 7. 1 and 7. 2 [G] Do not read subsection 7. 2. 3 yet. We will visit it later. • • [G] Section 7. 3 • [G] Section 7. 4 • [G] Section 7. 5 • [G] Section 7. 6 and subsection 7. 7. 1 30