Iterators and Sequences1 © 2010 Goodrich, Tamassia.

Slides:



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

Chapter 3 Lists Dr Zeinab Eid.
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
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.
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.
The List ADT Textbook Sections
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
CSC 212 – Data Structures Lecture 22: PositionList.
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
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.
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.
© 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.
© 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.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
The Java Collections Framework Chapters 7.5. Outline Introduction to the Java Collections Framework Iterators Interfaces, Abstract Classes and Classes.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Hash Tables1   © 2010 Goodrich, Tamassia.
Lists Last Update: Oct 29, 2014 EECS2011: Lists1.
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 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.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
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’
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
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.
© 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.
List Interface and Linked List Mrs. Furman March 25, 2010.
Welcome to CSCE 221 – Data Structures and Algorithms
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,
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees Priority Queues and Heaps Dictionaries.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
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,
Iterators and Sequences
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,
Sequences and Iterators
COMP9024: Data Structures and Algorithms
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
Sequences and Iterators
Array Lists, Node Lists & Sequences
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,
" A list is only as strong as its weakest link. " - Donald Knuth
Doubly Linked Lists or Two-way Linked Lists
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Recall What is a Data Structure Very Fundamental Data Structures
Vectors, Lists and Sequences
Linked Lists & Iterators
Vectors, Lists, and Sequences
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Presentation transcript:

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