List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Similar documents
List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

List Iterator Implementation

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Inheritance: The Fundamental Functions

Recursive Linked Lists

Inheritance: The Fundamental Functions

Implementing Linked Lists

Lecture 37 Section 9.4. Wed, Apr 22, 2009

The Critical-Path Algorithm

Minimal Spanning Trees

Lecture 29 Section Wed, Apr 1, 2009

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Friends and Unary Operators

Binary Tree Applications

The Class Construct Part 2

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Binary Tree Implementation

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

The Decreasing-Time Algorithm

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Binary Tree Implementation

The Coefficient of Determination

Recursive Descent Parsers

Scope and Parameter Passing

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Sampling Distribution Examples Sections 15.4, 15.5

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Rotations and Translations

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

The Class Construct Part 1

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Scope and Parameter Passing

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Solving Recursive Sequences by Iteration

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

The Traveling Salesman Problem Nearest-Neighbor Algorithm

The Standard Template Library Classes

The Graphics Pipeline

Scheduling and Digraphs

Density Curves Sections

Magnification and Minification

The Pairwise-Comparison Method

XQuery FLOWR Expressions Lecture 35

The Traveling Salesman Problem Brute Force Method

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Stacks and their Applications

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

The Normal Distribution

LR Parsing - Conflicts

Displaying Distributions - Quantitative Variables

Function Definition Syntax Tree

Dynamic Allocation of Memory

Dynamic Allocation of Memory

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

The Plurality-with-Elimination Method

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

The x86 Architecture

Nondeterministic Programming in C++

The x86 Instruction Set

The Graphics Pipeline

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Street-Routing Problems

Recognition of Tokens

Building the Abstract Syntax Trees

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Ambient and Diffuse Light

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Introduction to Compiler Design

Programming Languages

Programming Languages

LR Parsing - The Items

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

The Projection Matrix

Introduction to Databases

The View Frustum. Lecture 9 Sections 2.6, 5.1, 5.2. Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2011

PHP Queries and HTML Forms Lecture 23

jquery Lecture 34 Robb T. Koether Wed, Apr 10, 2013 Hampden-Sydney College Robb T. Koether (Hampden-Sydney College) jquery Wed, Apr 10, / 29

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

The CYK Parsing Algorithm

The Traveling Salesman Problem Cheapest-Link Algorithm

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

Lecture 25 Sections 8.4, 8.6. Fri, Apr 24, 2009

DTDs and XML Attributes

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

Transcription:

List Iterators Lecture 27 Section 16.5 Robb T. Koether Hampden-Sydney College Wed, Apr 8, 2015 Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 1 / 28

1 Sequential Access 2 List Iterators 3 The Iterator Class 4 Reverse Iterators 5 Assignment Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 2 / 28

Outline 1 Sequential Access 2 List Iterators 3 The Iterator Class 4 Reverse Iterators 5 Assignment Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 3 / 28

Sequential Access of List Members A for Loop for (int i = 0; i < list.size(); i++) list[i] = 0; Consider the for loop above. How efficient is it if list is an ArrayList? How efficient is it if list is a LinkedList? Notice that we are accessing the members of the list sequentially. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 4 / 28

Outline 1 Sequential Access 2 List Iterators 3 The Iterator Class 4 Reverse Iterators 5 Assignment Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 5 / 28

List Iterators Definition (List Iterator) A list iterator is an object that is associated with a list and refers to a position in that list. The iterator uses the most efficient means available to do this, depending on the type of list. An array list iterator uses an index. A linked list iterator uses a node pointer. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 6 / 28

Advantages of Iterators 30 50 60 10 90 70 20 40 80 Iterator Iterator Since the iterator holds a position within the list, it can readily access that position s successor, thereby greatly improving sequential access. Furthermore, as a separate object, we may create as many iterators for a list as we like. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 7 / 28

List Iterator Behavior The iterator begins at one end of the list. The iterator advances one element at a time. The iterator stops when it moves beyond the other end of the list. Forward iterators advance from head to tail. Reverse iterators advance from tail to head. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 8 / 28

List Iterator Behavior The list elements 1st position beyond the list 30 50 60 10 90 70 20 40 80 The list of elements Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 9 / 28

List Iterator Behavior The list elements 1st position beyond the list 30 50 60 10 90 70 20 40 80 Iterator The (forward) iterator begins at the head. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 9 / 28

List Iterator Behavior The list elements 1st position beyond the list 30 50 60 10 90 70 20 40 80 Iterator Then it advances to position 1. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 9 / 28

List Iterator Behavior The list elements 1st position beyond the list 30 50 60 10 90 70 20 40 80 Iterator Then to position 2. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 9 / 28

List Iterator Behavior The list elements 1st position beyond the list 30 50 60 10 90 70 20 40 80 Iterator And so on... Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 9 / 28

List Iterator Behavior The list elements 1st position beyond the list 30 50 60 10 90 70 20 40 80 Iterator And so on... Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 9 / 28

List Iterator Behavior The list elements 1st position beyond the list 30 50 60 10 90 70 20 40 80 Iterator And so on... Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 9 / 28

List Iterator Behavior The list elements 1st position beyond the list 30 50 60 10 90 70 20 40 80 Iterator And so on... Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 9 / 28

List Iterator Behavior The list elements 1st position beyond the list 30 50 60 10 90 70 20 40 80 Iterator And so on... Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 9 / 28

List Iterator Behavior The list elements 1st position beyond the list 30 50 60 10 90 70 20 40 80 Iterator And so on... Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 9 / 28

List Iterator Behavior The list elements 1st position beyond the list 30 50 60 10 90 70 20 40 80 Iterator Until it goes beyond the last position. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 9 / 28

Outline 1 Sequential Access 2 List Iterators 3 The Iterator Class 4 Reverse Iterators 5 Assignment Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 10 / 28

The Iterator Class We create the LinkedListwIter class as a subclass of the LinkedList class. We define the Iterator class within the LinkedListwIter class. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 11 / 28

The Iterator Class The Iterator Class class LinkedListwIter : public LinkedList { // Iterator class definition public: class Iterator { public: Iterator(); };. }; public: // LinkedListwIter member functions private: // LinkedListwIter data members Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 12 / 28

The Iterator Class The Iterator Class class LinkedListwIter : public LinkedList { // Iterator class definition public: class Iterator { public: Iterator(); };. }; public: // LinkedListwIter member functions private: // LinkedListwIter data members Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 13 / 28

The Iterator Class This places the Iterator class within the scope of the LinkedListwIter class. Therefore, the full name if the Iterator class is LinkedListwIter<T>::Iterator Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 14 / 28

List Iterator Data Members List Iterator Data Members const LinkedList<T>* mlist; LinkedListNode<T>* node; mlist A pointer to the associated list. node A pointer to a node in the associated list. The data members have protected access. The mlist data member is a constant. Therefore, it may be set only when the Iterator is constructed. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 15 / 28

List Iterator Member Functions List Iterator Member Functions Iterator(const LinkedListwIter& lst, LinkedListNode* p); bool isequal(const Iterator& it) const; Iterator(LinkedListwIter, LinkedListNode*) Constructs an iterator associated with a specified list. isequal() Determines whether two iterators are equal. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 16 / 28

List Iterator Member Functions List Iterator Member Functions T& operator*(); Iterator& operator++(); operator*() Returns the list value pointed to by the iterator. operator++() Advances the iterator to the next list element. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 17 / 28

List Iterator Member Functions List Iterator Member Functions bool operator==(const Iterator& it) const; bool operator!=(const Iterator& it) const; operator==() Compares two iterators for equality. operator!=() Compares two iterators for inequality. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 18 / 28

LinkedListwIter Member Functions LinkedListwIter Member Functions Iterator begin() const; Iterator end() const; begin() Returns a new iterator set to the beginning of this list. end() Returns a new iterator set to the end of this list. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 19 / 28

Sequential Access with Iterators A for Loop typedef LinkedListwIter<int>::Iterator Iterator; for (Iterator it = list.begin(); it!= list.end(); ++it) *it = 0; Now consider the for loop again. How efficient is it if list is an ArrayListwIter? How efficient is it if list is a LinkedListwIter? Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 20 / 28

Additional List Member Functions Additional List Member Functions T getelement(const Iterator& curr) const; T& getelement(const Iterator& curr); void setelement(const Iterator& curr, const T& value); T getelement() const Returns the list element that the Iterator is pointing to. T& getelement() Returns a reference to the list element that the Iterator is pointing to. setelement() Sets the value of the list element that the Iterator is pointing to. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 21 / 28

Additional List Member Functions Additional List Member Functions T operator[](iterator& curr) const; T& operator[](iterator& curr); T operator[]() const Returns the list element that the Iterator is pointing to. T& operator[]() Returns a reference to the list element that the Iterator is pointing to. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 22 / 28

Additional List Member Functions Additional List Member Functions Iterator searchiter(const T& value); void sortiter(); searchiter() Searches for the specified value and returns an Iterator to it if it is found. If it is not found, then the Iterator is equal to end(). sortiter() Sorts the list by using Iterators rather than indexes. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 23 / 28

Decrementing an Iterator We can use the operator -- to back up to the previous list member. For an ArrayList iterator, How would we do this? What would happen if we were at the head of the list? For a LinkedList iterator, How would we do this? What would happen if we were at the head of the list? Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 24 / 28

Outline 1 Sequential Access 2 List Iterators 3 The Iterator Class 4 Reverse Iterators 5 Assignment Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 25 / 28

Reverse Iterators Definition (Reverse Iterator) A reverse iterator is an iterator that works in the opposite direction. What does it mean for a reverse iterator to be at the beginning of a list? What does it mean for a reverse iterator to be at the end of a list? How would we increment a reverse iterator? How would we decrement a reverse iterator? Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 26 / 28

Outline 1 Sequential Access 2 List Iterators 3 The Iterator Class 4 Reverse Iterators 5 Assignment Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 27 / 28

Assignment Homework Read Section 16.5. Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 8, 2015 28 / 28