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 byEarl Lovern
Modified about 1 year ago
Iterators and Sequences1 © 2010 Goodrich, Tamassia
Iterators and Sequences2 Containers and Iterators An iterator abstracts the process of scanning through a collection of elements A container is an abstract data structure that supports element access through iterators begin(): returns an iterator to the first element end(): return an iterator to an imaginary position just after the last element An iterator behaves like a pointer to an element *p: returns the element referenced by this iterator ++p: advances to the next element Extends the concept of position by adding a traversal capability © 2010 Goodrich, Tamassia
Iterators and Sequences3 Containers Data structures that support iterators are called containers Examples include Stack, Queue, Vector, List Various notions of iterator: (standard) iterator: allows read-write access to elements const iterator: provides read-only access to elements bidirectional iterator: supports both ++p and –p random-access iterator: supports both p+i and p-i © 2010 Goodrich, Tamassia
Iterating through a Container The conventional way to iterate through an STL vector © 2010 Goodrich, TamassiaIterators and Sequences4
5 Iterating through a Container Let C be a container and p be an iterator for C for (p = C.begin(); p != C.end(); ++p) loop_body Use of an iterator to compute the sum of an STL vector vector ::iterator p; int sum = 0; for (p=V.begin(); p!=V.end(); p++) sum += *p; return sum; © 2010 Goodrich, Tamassia
Iterators and Sequences6 Implementing Iterators Array-based array A of the n elements index i that keeps track of the cursor begin() = 0 end() = n (index following the last element) © 2010 Goodrich, Tamassia
Iterators and Sequences7 Implementing Iterators Linked list-based doubly-linked list L storing the elements, with sentinels for header and trailer pointer to node containing the current element begin() = front node end() = trailer node (just after last node) © 2010 Goodrich, Tamassia
Iterators and Sequences8 STL Iterators in C++ Each STL container type C supports iterators: C::iterator – read/write iterator type C::const_iterator – read-only iterator type C.begin(), C.end() – return start/end iterators This iterator-based operators and methods: *p: access current element ++p, --p: advance to next/previous element C.assign(p, q): replace C with contents referenced by the iterator range [p, q) (from p up to, but not including, q) insert(p, e): insert e prior to position p erase(p): remove element at position p erase(p, q): remove elements in the iterator range [p, q) © 2010 Goodrich, Tamassia
Iterators and Sequences9 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(), empty() ArrayList-based methods: at(i), set(i, o), insert(i, o), erase(i) List-based methods: begin(), end() insertFront(o), insertBack(o) eraseFront(), eraseBack() insert (p, o), erase(p) Bridge methods: atIndex(i), indexOf(p) © 2010 Goodrich, Tamassia
Iterators and Sequences10 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 Sequences11 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 Sequences12 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 Sequences13 Comparing Sequence Implementations OperationArrayList size, empty 11 atIndex, indexOf, at 1n begin, end 11 set(p,e) 11 set(i,e) 1n insert(i,e), erase(i) nn insertBack, eraseBack 11 insertFront, eraseFront n1 insert(p,e), erase(p) n1 © 2010 Goodrich, Tamassia
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
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 Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored
© 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.
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)
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 6: Sequences : vectors and lists COL 106 Shweta Agrawal.
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.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
1. 2 Stacks Queues Deque Adapters and the Adapter Design Pattern.
HOWDY! WELCOME TO CSCE 221 – DATA STRUCTURES AND ALGORITHMS.
C o n f i d e n t i a l NextHome Subject Name: Data Structure Using C Title : Linked Lists.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
Chapter 4: Stacks & Queues Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from.
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 22 Implementing lists: linked implementations.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
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.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
© 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 Queues1 5.2 Queues. © 2004 Goodrich, Tamassia Queues2 Definition: a Queue is a collection of objects that are inserted and removed.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
M180: Data Structures & Algorithms in Java Linked Lists – Part 1 Arab Open University 1.
CSCE , SPRING 2016 INSTRUCTOR: DR. NANCY M. AMATO 1.
Maps and Dictionaries Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
© 2017 SlidePlayer.com Inc. All rights reserved.