Sequences and Iterators

Slides:



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

Queues and Linked Lists
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
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.
CSC 212 – Data Structures Lecture 22: PositionList.
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
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.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
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.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC 212 – Data Structures Lecture 37: Course Review.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
LINKED LISTS.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Linked Data Structures
Lecture 6 of Computer Science II
Elementary Data Structures
Lists Rem Collier Room A1.02
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
Week 4 - Monday CS221.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Data Structure By Amee Trivedi.
Double-Ended Queues Chapter 5.
The List ADT Reading: Textbook Sections 3.1 – 3.5
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
Lectures linked lists Chapter 6 of textbook
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
COMP9024: Data Structures and Algorithms
EEL 4854 IT Data Structures Linked Lists
Stacks and Queues.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
LINKED LISTS CSCD Linked Lists.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Sequences and Iterators
Array Lists, Node Lists & Sequences
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
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,
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
The List ADT Reading: Textbook Sections 3.1 – 3.5
" A list is only as strong as its weakest link. " - Donald Knuth
Linked Lists.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
CS2013 Lecture 4 John Hurley Cal State LA.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Recall What is a Data Structure Very Fundamental Data Structures
Vectors, Lists and Sequences
Copyright © Aiman Hanna All rights reserved
Linked Lists & Iterators
Vectors, Lists, and Sequences
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
CS210- Lecture 16 July 11, 2005 Agenda Maps and Dictionaries Map ADT
Software Design Lecture : 39.
Dictionaries and Hash Tables
Presentation transcript:

Sequences and Iterators 6/25/2018 9:52 PM Sequences and Iterators Sequences and Iterators

Sequences and Iterators Introduction Consider the problem of printing out the elements in a collection, represented as an array V. So, its contents are easily printed with code like the following: In this loop, i is an iterator object, because it is used to control the iteration. However, using the integer i as an iterator constrains the design: We can only store the collection in an array-like structure. A more flexible alternative is to design an iterator class (object) that encapsulates a position inside a collection. An iterator object controls iteration of a collection of elements (e.g, array, array-list, list, or sequence). for (int i = 0; i < V.length; i++) system.out.println (V[i]); Sequences and Iterators

Sequences and Iterators 6.3.1 The Iterator ADT An iterator is a software design pattern that abstracts the process of scanning through a collection of elements one element at a time. An iterator consists of: A sequence S; A current element in S; and A way of stepping to the next element in S, making it the current element. Thus, an iterator extends the concept of the position ADT, introduced before. In fact, a position can be thought of as an iterator that doesn’t go anyway. An iterator encapsulates the concepts of “place” and “next” in a collection of objects. Sequences and Iterators

The Iterator ADT (Cont.) An iterator abstracts the process of scanning through a collection of elements Methods of the object iterator ADT: object element() to get data or object stored at that position Boolean hasNext() Test whether there are elements left in the iterator object next() Return the next element in the iterator iterator extends the concept of position by adding a traversal capability Implementation can be with an array, singly- or doubly- linked list Sequences and Iterators

The Iterator ADT (Cont.) Note that the iterator ADT has the cursor notion of the “current” element in a traversal of a sequence. The first element in an iterator is returned by the first call to the method next() , assuming that the iterator contains at least one element. Advantages of iterator ADT An iterator provides a unified scheme to access all the elements of a collection of objects, independently of the specific organization of the collection. An iterator for an array list, node list, or sequence should return the elements according to their linear ordering. Sequences and Iterators

The Iterable Abstract Data Type In order to provide a unified generic mechanism for scanning through a data structure, ADTs storing collections of objects should support the following method: iterator(): Return an iterator of the elements in the collection. This method is supported by the java.util.ArrayList class. This method can make it simple for us to specify computations that need to loop through the elements of a list. To guarantee that a node list supports this method, for example, we could add this method to the PositionList interface, as shown below: Sequences and Iterators

Sequences and Iterators Public interface PositionList<E> extends Iterable<E> { //… all the other methods of the list ADT… /* Returns an iterator of all the elements in the list. */ Public Iterator<E> iterator(); } We assume that our array lists and node lists support the iterator() method. Given such a PositionList definition, we could use an iterator to create a string representation of a node list, as shown below: /** Returns a textual representation of a given node list */ Public static <E> StringtoString (PositionList <E> ) l) { Iterator<E> it = l.iterator(); String s = “[“; while (it.hasNext()) { s += it.Next(); // implicit cast of the next element to String if (it.hasNext()) { s += “ , “; } s += “]”; return s; Sequences and Iterators

Sequences and Iterators 6.3.3 Implementing Iterators An iterator is typically associated with another data structure We can augment the Stack, Queue, Vector, Node List and Sequence ADTs with method: Object Iterator elements() Two ways of implementation of iterator: Snapshot: freezes the contents of the data structure at a given time Dynamic: follows changes to the data structure Sequences and Iterators

Sequences and Iterators a. Snapshot Method One way to implement an iterator for a collection of elements is to make a ‘snapshot’ of it and iterate over it. This approach would involve storing the collection in a separate data structure that supports sequential access to its elements. For example, we could insert (freeze) all the elements of the collection into a queue, in which case: Method hasNext() corresponds to !isEmpty() Method next() corresponds to dequeue() With this approach, the method iterator() takes O(n) time for a collection of size n. Since this copying process is costly, we prefer to have iterators operate on the collection itself, not a copy of it. Sequences and Iterators

Sequences and Iterators b. Direct Approach In this method of implementing an iterator , we need only to keep track of where in the collection the iterator’s cursor points. Thus, creating a new iterator in this case simply involves creating an iterator object that represents a cursor placed just before the first element of the collection. Performing the next() method would return the next element, if it exists, and moving the cursor just after this element’s position. Thus, in this implementation way, creating an iterator takes O(1) time, as do each of the iterator’s methods. Sequences and Iterators

Sequences and Iterators A sequence is an ADT that supports all of the methods of the deque ADT, the Array-List ADT (or, vector), and the Node-List ADT. That is, a sequence provides explicit access to the elements in the list either by their indices or by their positions. Methods of a sequence ADT include: Generic methods: size(), isEmpty() Vector-based methods: get(i), set(i, e), add(i, e), remove (i) Sequences and Iterators

Sequences and Iterators Sequences (Cont.) Node List-based methods: first(), last(), prev(p), next(p), set(p, e), addBefore(p, e), addAfter(p, e), addFirst(e), addLast(e), remove(p) Since sequence provides this dual access capability, we also include, in the sequence ADT, the following two “bridging” methods that provide connections between indices and positions: Bridge methods: atIndex(i) : Returns the position of the element with index i; an error condition occurs if i < 0 or i > size() -1. indexOf(p) : Return the index of the element at position p. Sequences and Iterators

Multiple Inheritance in the Sequence ADT The definition of the sequence ADT as including all the methods from three different ADTs is an example of multiple inheritance. That’s, the sequence ADT inherits methods from the three “super” abstract data types. In other words, its methods include the union of the methods of these three “super” ADTs. Sequences and Iterators

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 Sequences and Iterators

Linked List Implementation of a Sequence 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 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 trailer header nodes/positions elements Sequences and Iterators

Array-based Implementation of a Sequence elements We use a circular array S storing positions A position object stores: Element Index Indices f and l keep track of first and last positions 1 2 3 positions S f l Sequences and Iterators

Sequence Implementations Operation Array List size, isEmpty 1 atIndex, indexOf, get n first, last, prev, next Set(p,e) Set(i,e) Add(i,e), remove(i) addFirst, addLast addAfter, addBefore Remove(p) Sequences and Iterators