Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recall What is a Data Structure Very Fundamental Data Structures

Similar presentations


Presentation on theme: "Recall What is a Data Structure Very Fundamental Data Structures"— Presentation transcript:

1 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

2 Recall too Equivalence Shallow & Deep Cloning a data structure Stacks
Queues

3 Application: Round Robin Schedulers
We can implement a round robin scheduler using a queue Q by repeatedly performing the following steps: e = Q.dequeue() Service element e Q.enqueue(e) Queue Dequeue Enqueue Shared Service © 2014 Goodrich, Tamassia, Goldwasser

4 Lists ADT and Iterators
Presentation for use with the textbook Data Structures and Algorithms in Java, 6 edition , by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 th Lists ADT and Iterators © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 1

5 The java.util.List ADT The java.util.List interface includes the following methods: q   © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 2

6 Example A sequence of List operations: q
© 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 3

7 Array Lists 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 . q   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). q   A 0 1 2 i n © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 4

8 Insertion 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] q   In the worst case ( i = 0), this takes O ( n ) time A q   0 1 2 i n A 0 1 2 i n A o 0 1 2 i n © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 5

9 Element Removal 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] q   In the worst case ( i = 0), this takes O ( n ) time q   A o 0 1 2 i n A 0 1 2 i n A 0 1 2 i n © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 6

10 Performance In an array-based implementation of a dynamic list:
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 n   n   n   In an add operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one … q   © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 7

11 Java Implementation © 2014 Goodrich, Tamassia, Goldwasser
Lists and Iterators 8

12 Java Implementation, 2 © 2014 Goodrich, Tamassia, Goldwasser
Lists and Iterators 9

13 Growable Array-based Array List
Let push(o) be the operation that adds element o at the end of the list q   Algorithm push ( o) if t = S.length - 1 then A ← new array of When the array is full, we replace the array with a larger one q   size … for i ← 0 to n -1 do A [ i ] ← S [ i ] How large should the new array be? S ← A q   n ← n +1 S [ n -1] ← o n   Incremental strategy : increase the size by a constant c Doubling strategy : double the n   size © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 10

14 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 Lists and Iterators 10

15 Positional Lists 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. q   A position acts as a marker or token within the broader positional list. q   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. q   A position instance is a simple object, supporting only the following method: q   P.getElement( ): Return the element stored at position p. n   © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 11

16 Positional List ADT Accessor methods: q
© 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 12

17 Positional List ADT, 2 Update methods: q
© 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 13

18 Example A sequence of Positional List operations: q
© 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 14

19 Positional List Implementation
q   The most natural way to implement a positional list is with a doubly-linked list. prev next element node trailer header nodes/positions elements © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 15

20 Insertion p A B C p q A B C X p q A B X C
Insert a new node, q, between p and its successor. q   p A B C p q A B C X p q A B X C © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 16

21 Deletion p A B C D A B C p D A B C
Remove a node, p, from a doubly-linked list. q   p A B C D A B C p D A B C © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 17

22 Iterators that abstracts the process of scanning
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 Lists and Iterators 18

23 The Iterable Interface
Java defines a parameterized interface, named Iterable , that includes the following single method: q   iterator ( ): Returns an iterator of the elements in the collection. n   An instance of a typical collection class in Java, such as an ArrayList, is iterable (but not itself an iterator); it produces an iterator for its collection as the return value of the iterator ( ) method. q   Each call to iterator ( ) returns a new iterator instance, thereby allowing multiple (even simultaneous) traversals of a collection. q   © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 19

24 The for-each Loop Java ’s Iterable class also plays a fundamental
role in support of the “for-each ” loop syntax: q   is equivalent to: © 2014 Goodrich, Tamassia, Goldwasser Lists and Iterators 20

25 The for-each Loop But to use remove you must : q
© 2014 Goodrich, Tamassia, Goldwasser 20

26 Implementing Iterators
Snapshot iterator Lazy iterator © 2014 Goodrich, Tamassia, Goldwasser

27 The Java Collections Framework
part of the java.util package Children of Collection interface © 2014 Goodrich, Tamassia, Goldwasser

28 The Java Collections Framework
© 2014 Goodrich, Tamassia, Goldwasser Queues

29 See the book for List Iterators in Java
Java way Vs book way List-Based Algorithms in the Java Collections Framework © 2014 Goodrich, Tamassia, Goldwasser

30 Reading [G] Section 7.1 and 7.2
[G] Do not read subsection 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


Download ppt "Recall What is a Data Structure Very Fundamental Data Structures"

Similar presentations


Ads by Google