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
Chapter 22 Implementing lists: linked implementations.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
1 Chapter 2. Linked Lists - Singly linked lists - Doubly linked lists aka lecture 3.
Pointers. BY SIVAKUMAR.P Pointers OBJECTIVES INTRODUCTION POINTERS DYNAMIC MEMORY ALLOCATION LINKED LISTS APPLICATION.
Linked Lists. Please Read These slides are provided for the use of students enrolled in James Durbanos Data Structures class (CISC 220). They are the.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 25 Lists, Stacks, Queues,
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Chapter 9: Data Structures I Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
Chapter 20 The STL (containers, iterators, and algorithms) Bjarne Stroustrup
Linked Lists Geletaw S.. Objective At the end of the session students should have basic understanding of – Linked lists – Basic operations of linked lists.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Introduction to Computation and Problem Solving Class 26: Linked Lists Prof. Steven R. Lerman and Dr. V. Judson Harward.
Lists - I The List ADT Textbook Sections
Linked Lists. 2 Anatomy of a linked list A linked list consists of: A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
Chapter 4: Stacks and Queues. Objectives Looking ahead – in this chapter, we’ll consider Stacks Queues Priority Queues Stacks in the Standard Template.
Review 1 Pseudo Code Basic elements of Pseudo code Basic operations of Pseudo code Flow Chart Symbols used in flow charts Examples.
SAK 3117 Data Structures Chapter 1: INTRODUCTION TO DATA STRUCTURE.
TK1924 Program Design & Problem Solving Session 2011/2012 L4: Linked List.
Data Structures Through C B.Padmaja Assoc. Professor Department of IT Vardhaman College of Engineering.
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Fundamentals of Python: From First Programs Through Data Structures Chapter 19 Unordered Collections: Sets and Dictionaries.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Java Programming: Advanced Topics 1 Collections and Utilities.
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Introduction to Computation and Problem Solving Class 32: The Java® Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward.
© 2016 SlidePlayer.com Inc. All rights reserved.