We think you have liked this presentation. If you wish to download it, please recommend it to your friends in any social system. Share buttons are a little bit lower. Thank you!
Presentation is loading. Please wait.
Published byBraulio Hook
Modified about 1 year ago
Iterators and Sequences1 © 2010 Goodrich, Tamassia
Iterators and Sequences2 Iterators An iterator abstracts the process of scanning through a collection of elements It maintains a cursor that sits between elements in the list, or before the first or after the last element Methods of the Iterator ADT: hasNext(): returns true so long as the list is not empty and the cursor is not after the last element next(): returns the next element Extends the concept of position by adding a traversal capability Implementation with an array or singly linked list © 2010 Goodrich, Tamassia
Iterators and Sequences3 Iterable Classes An iterator is typically associated with an another data structure, which can implement the Iterable ADT We can augment the Stack, Queue, Vector, List and Sequence ADTs with method: Iterator iterator(): returns an iterator over the elements In Java, classes with this method extend Iterable Two notions of iterator: snapshot: freezes the contents of the data structure at a given time dynamic: follows changes to the data structure In Java: an iterator will fail (and throw an exception) if the underlying collection changes unexpectedly © 2010 Goodrich, Tamassia
Iterators and Sequences4 The For-Each Loop Java provides a simple way of looping through the elements of an Iterable class: for (type name: expression) loop_body For example: List values; int sum=0 for (Integer i : values) sum += i; // boxing/unboxing allows this © 2010 Goodrich, Tamassia
Iterators and Sequences5 Implementing Iterators Array based array A of the elements index i that keeps track of the cursor Linked list based doubly-linked list L storing the elements, with sentinels for header and trailer pointer p to node containing the last element returned (or the header if this is a new iterator). We can add methods to our ADTs that return iterable objects, so that we can use the for-each loop on their contents © 2010 Goodrich, Tamassia
Iterators and Sequences6 List Iterators in Java Java uses a the ListIterator ADT for node-based lists. This iterator includes the following methods: add(e): add e at the current cursor position hasNext(): true if there is an element after the cursor hasPrevious: true if there is an element before the cursor previous(): return the element e before the cursor and move cursor to before e next(): return the element e after the cursor and move cursor to after e set(e): replace the element returned by last next or previous operation with e remove(): remove the element returned by the last next or previous method © 2010 Goodrich, Tamassia
Iterators and Sequences7 Sequence ADT The Sequence ADT is the union of the Array List and Node List ADTs Elements accessed by Index, or Position Generic methods: size(), isEmpty() ArrayList-based methods: get(i), set(i, o), add(i, o), remove(i) List-based methods: first(), last(), prev(p), next(p), replace(p, o), addBefore(p, o), addAfter(p, o), addFirst(o), addLast(o), remove(p) Bridge methods: atIndex(i), indexOf(p) © 2010 Goodrich, Tamassia
Iterators and Sequences8 Applications of Sequences The Sequence ADT is a basic, general- purpose, 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 © 2010 Goodrich, Tamassia
Iterators and Sequences9 Linked List Implementation A doubly linked list provides a reasonable implementation of the Sequence ADT Nodes implement Position and store: element link to the previous node link to the next node Special trailer and header nodes trailer header nodes/positions elements Position-based methods run in constant time Index-based methods require searching from header or trailer while keeping track of indices; hence, run in linear time © 2010 Goodrich, Tamassia
Iterators and Sequences10 Array-based Implementation We use a circular array storing positions A position object stores: Element Index Indices f and l keep track of first and last positions 0123 positions elements S lf © 2010 Goodrich, Tamassia
Iterators and Sequences11 Comparing Sequence Implementations OperationArrayList size, isEmpty 11 atIndex, indexOf, get 1n first, last, prev, next 11 set(p,e) 11 set(i,e) 1n add, remove(i) nn addFirst, addLast 11 addAfter, addBefore n1 remove(p) n1 © 2010 Goodrich, Tamassia
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
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.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
© 2004 Goodrich, Tamassia Stacks1 Abstract Data Types (ADTs) An abstract data type (ADT) is a contract between the user of a data structure and its implementor.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
© 2004 Goodrich, Tamassia Queues1 5.2 Queues. © 2004 Goodrich, Tamassia Queues2 Definition: a Queue is a collection of objects that are inserted and removed.
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape  v = new Shape; Simple Iterator For( int i=0 ; i< v.length ; i++)
CS 221 Analysis of Algorithms Data Structures Vectors, 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.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
List Interface and Linked List Mrs. Furman March 25, 2010.
Lists Last Update: Oct 29, 2014 EECS2011: Lists1.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
CSC 212 Sequences & Iterators. Announcements Midterm in one week Will cover through chapter 5 of book Midterm will be open book, open note (but, closed.
The Java Collections Framework Chapters 7.5. Outline Introduction to the Java Collections Framework Iterators Interfaces, Abstract Classes and Classes.
Chapter 22 Implementing lists: linked implementations.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
CSC 212 – Data Structures Lecture 22: PositionList.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees Priority Queues and Heaps Dictionaries.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits $0.67 each 40’
Lists - I The List ADT Textbook Sections
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Problem of the Day Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST Winner is one who owns SLOWEST horse.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Chapter 6: Sequences : vectors and lists COL 106 Shweta Agrawal.
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
HOWDY! WELCOME TO CSCE 221 – DATA STRUCTURES AND ALGORITHMS.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
© 2017 SlidePlayer.com Inc. All rights reserved.