" A list is only as strong as its weakest link. " - Donald Knuth

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
CS 307 Fundamentals of Computer Science 1 Lists and ArrayList many slides taken from Mike Scott, UT Austin.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Lists Last Update: Oct 29, 2014 EECS2011: Lists1.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
Parasol Lab, Dept. CSE, Texas A&M University
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
CSE373: Data Structures & Algorithms Priority Queues
Elementary Data Structures
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Ch7. List and Iterator ADTs
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
CE 221 Data Structures and Algorithms
Heaps And Priority Queues
Sequences and Iterators
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
COMP9024: Data Structures and Algorithms
Stacks.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
05 Array-Based Sequences
Array Lists, Node Lists & Sequences
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Elementary Data Structures
Ch7. List and Iterator ADTs
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Arrays and Linked Lists
Sequences 11/22/2018 3:25 AM Doubly-Linked Lists Doubly-Linked Lists.
Vectors 11/23/2018 1:03 PM Growing Arrays Vectors.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Stacks.
Vectors 11/29/2018 6:45 PM Vectors 11/29/2018 6:45 PM Vectors.
Array-Based Sequences
Doubly Linked Lists or Two-way Linked Lists
CS2013 Lecture 4 John Hurley Cal State LA.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Problem Understanding
Recall What is a Data Structure Very Fundamental Data Structures
Linked Lists & Iterators
Vectors, Lists, and Sequences
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
Amortized Analysis and Heaps Intro
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Vectors and Array Lists
Stacks and Linked Lists
Problem Understanding
Presentation transcript:

" A list is only as strong as its weakest link. " - Donald Knuth Lists and Iterators " A list is only as strong as its weakest link. " - Donald Knuth

The java.util.List ADT The java.util.List interface includes the following methods: CS 321 - Data Structures

Example A sequence of List operations: CS 321 - Data Structures

Array-Based 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. 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 1 2 i n CS 321 - Data Structures

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] In the worst case (i = 0), this takes O(n) time A 1 2 i n A 1 2 i n A o 1 2 i n CS 321 - Data Structures

Deletion 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 1 2 n o i A 1 2 n i A 1 2 n i CS 321 - Data Structures

Performance In an array-based implementation of a dynamic list: 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 Instead of throwing an exception, when do add operation and the array is full, we can replace the array with a larger one … CS 321 - Data Structures

Growable Array-Based List 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? Incremental strategy: increase the size by a constant c Doubling strategy: double the size 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 CS 321 - Data Structures

Positional Lists 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: P.getElement( ): Return the element stored at position p. CS 321 - Data Structures

Positional List ADT Accessor methods: CS 321 - Data Structures

Positional List ADT Mutator methods: CS 321 - Data Structures

Example A sequence of Positional List operations: CS 321 - Data Structures

Positional List Implementation The most natural way to implement a positional list is with a doubly-linked list. prev next element node tail header nodes/positions elements CS 321 - Data Structures

Insertion Insert a new node, q, between p and its successor. p A B C p X p q A B X C CS 321 - Data Structures

Deletion Remove a node, p, from a doubly-linked list. A B C D p A B C CS 321 - Data Structures

Iterators An iterator is a software design pattern that abstracts the process of scanning through a sequence of elements, one element at a time. CS 321 - Data Structures

The Iterable Interface Java defines a parameterized interface, named Iterable, that includes the following single method: iterator(): Returns an iterator of the elements in the collection. An instance of a typical collection class in Java, such as an ArrayList, is iterable; 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. CS 321 - Data Structures

The for-each Loop Java’s Iterable class also plays a fundamental role in support of the for-each loop syntax: is equivalent to: CS 321 - Data Structures

CS 321 - Data Structures

Comparison of the Strategies Compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n push operations Assume we start with an empty list represented by a growable array of size 1 We call amortized time of a push operation the average time taken by a push operation over the series of operations (i.e. T(n)/n) CS 321 - Data Structures

Incremental Strategy Analysis Over n push operations, we replace the array k = n/c times, where c is a constant The total time T(n) of a series of n push operations is proportional to n + c + 2c + 3c + 4c + … + kc = n + c(1 + 2 + 3 + … + k) = n + ck(k + 1)/2 Since c is a constant, T(n) is O(n + k2), (i.e. O(n2) ) Thus, the amortized time of a push operation is O(n) CS 321 - Data Structures

Doubling Strategy Analysis We replace the array k = log2 n times The total time T(n) of a series of n push operations is proportional to n + 1 + 2 + 4 + 8 + …+ 2k = n + 2k + 1 - 1 = 3n - 1 T(n) is O(n) The amortized time of a push operation is O(1) geometric series 1 2 4 8 CS 321 - Data Structures