List Iterator Implementation

Similar documents
Lecture 37 Section 9.4. Wed, Apr 22, 2009

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

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

Recursive Linked Lists

Implementing Linked Lists

Binary Tree Applications

Friends and Unary Operators

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

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

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

Minimal Spanning Trees

Lecture 29 Section Wed, Apr 1, 2009

Dynamic Allocation of Memory

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

The x86 Instruction Set

Scope and Parameter Passing

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

The Standard Template Library Classes

Stacks and their Applications

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

The Critical-Path Algorithm

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

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

Scope and Parameter Passing

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

The Traveling Salesman Problem Nearest-Neighbor Algorithm

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

The x86 Architecture

The Class Construct Part 1

The Traveling Salesman Problem Brute Force Method

The Decreasing-Time Algorithm

XQuery FLOWR Expressions Lecture 35

LR Parsing - Conflicts

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

Recursive Descent Parsers

Dynamic Allocation of Memory

The Plurality-with-Elimination Method

Solving Recursive Sequences by Iteration

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

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

Magnification and Minification

Function Definition Syntax Tree

The Graphics Pipeline

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

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

Scheduling and Digraphs

LR Parsing - The Items

The Pairwise-Comparison Method

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

Programming Languages

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

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

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

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

Sampling Distribution Examples Sections 15.4, 15.5

Rotations and Translations

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

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

Inheritance: The Fundamental Functions

Inheritance: The Fundamental Functions

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

Building the Abstract Syntax Trees

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

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Binary Tree Implementation

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

Binary Tree Implementation

Dynamic Data Structures

The Graphics Pipeline

The x87 Floating-Point Unit

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

Ambient and Diffuse Light

Programming Languages

Density Curves Sections

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

Street-Routing Problems

Fundamental Data Types

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

The Coefficient of Determination

Introduction to Compiler Design

The CYK Parsing Algorithm

Recognition of Tokens

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

The Class Construct Part 2

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

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

Lecture 23: Pointer Arithmetic

DTDs and XML Attributes

The Normal Distribution

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

CSCE 2014 Final Exam Spring Version A

Displaying Distributions - Quantitative Variables

The Projection Matrix

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Data Structure Advanced

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

Lecture 20 Sections Wed, Apr 1, 2009

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Basic PHP Lecture 17

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

Transcription:

List Iterator Implementation Lecture 28 Section 14.6 Robb T. Koether Hampden-Sydney College Fri, Apr 10, 2015 Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 1 / 18

1 LinkedList Iterators 2 List Traversals 3 Reverse Iterators 4 Assignment Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 2 / 18

Outline 1 LinkedList Iterators 2 List Traversals 3 Reverse Iterators 4 Assignment Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 3 / 18

A Sort Function Sorting with Iterators void LinkedListwIter<T>::sortIter() { Iterator base = begin(); while (base!= end()) { Iterator min = base; Iterator curr = min; ++curr; while (curr!= end()) { if (*curr < *min) min = curr; ++curr; } T temp = *base; *base = *min; *min = temp; ++base; } return; } Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 4 / 18

Outline 1 LinkedList Iterators 2 List Traversals 3 Reverse Iterators 4 Assignment Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 5 / 18

List Traversals Definition (Traverse) To traverse a list is to move systematically through its nodes, visiting each node along the way. Forward traversals go from head to tail. Reverse traversals go from tail to head. The meaning of visiting a node is left unspecified at this point. The meaning will be specified at the time of the traversal. Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 6 / 18

The Traversal Function The traverse() Function void traverse(void (*visit)(iterator&)); Introduce a new List member function traverse(). The parameter visit is a pointer to a function. The visit() function has prototype void visit(iterator& it); Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 7 / 18

Traversals and Iterators Traversal Implementation void traverse(void (*visit)(iterator&)) { for (Iterator it = begin(); it!= end(); ++it) visit(it); return; } The traverse() function is implemented as a for loop. Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 8 / 18

Example - Print the List The print() Function list.traverse(print);. void print(iterator& it) { cout << *it << endl; return; } For example, we could write a print() function and then use traverse() to print all the values in the list. Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 9 / 18

Outline 1 LinkedList Iterators 2 List Traversals 3 Reverse Iterators 4 Assignment Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 10 / 18

Reverse Iterators A reverse iterator is an iterator that advances in the opposite direction, from tail to head. It is initialized to the last element in the list. It advances until it has gone beyond the head of the list. Because a reverse iterator is an iterator, we will derive the ReverseIterator class from the Iterator class. Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 11 / 18

ReverseIterator Member Functions Additional ReverseIterator Member Functions ReverseIterator(const LinkedListwIter<T>* lst, LinkedListNode<T>* p); ReverseIterator& operator++(); ReverseIterator(LinkedListwIter<T>*, LinkedListNode<T>*) Construct a ReverseIterator. operator++() Advance the ReverseIterator to the next node. Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 12 / 18

LinkedListwIter Member Functions Additional LinkedListwIter Member Functions ReverseIterator rbegin() const; ReverseIterator rend() const; rbegin() Create a ReverseIterator set to the beginning (tail) of the list. rend() Create a ReverseIterator set to the end (beyond the head) of the list. Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 13 / 18

LinkedListwIter Member Functions The other LinkedListwIter member functions that use iterators, such as the iterator version of getelement(), can accept reverse iterators as well because A ReverseIterator IS-A Iterator. Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 14 / 18

Implemention of Reverse Iterators To construct a reverse iterator for a linked list, Introduce a stack data member. Push NULL onto the stack. Then push the addresses of the nodes onto the stack as the list is traversed from head to tail. Stop with all but the final NULL pointer on the stack. Now the reverse iterator is initialized. Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 15 / 18

Implemention of Reverse Iterators To increment a reverse list iterator Pop an address off the stack. Assign it to the node pointer. To decrement a reverse list iterator Push the current address onto the stack. Use the next pointer as the current address. Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 16 / 18

Outline 1 LinkedList Iterators 2 List Traversals 3 Reverse Iterators 4 Assignment Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 17 / 18

Assignment Assignment Read Section 14.6. Robb T. Koether (Hampden-Sydney College) List Iterator Implementation Fri, Apr 10, 2015 18 / 18