List Iterators - Lecture 28 Sections 17.1 -...

44
List Iterators Lecture 28 Sections 17.1 - 17.3 Robb T. Koether Hampden-Sydney College Wed, Apr 4, 2018 Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 1 / 33

Transcript of List Iterators - Lecture 28 Sections 17.1 -...

Page 1: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List IteratorsLecture 28

Sections 17.1 - 17.3

Robb T. Koether

Hampden-Sydney College

Wed, Apr 4, 2018

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 1 / 33

Page 2: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

1 Sequential Access

2 List Iterators

3 The Iterator Class

4 Example

5 Reverse Iterators

6 Assignment

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 2 / 33

Page 3: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Outline

1 Sequential Access

2 List Iterators

3 The Iterator Class

4 Example

5 Reverse Iterators

6 Assignment

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 3 / 33

Page 4: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Sequential Access of List Members

A for Loopfor (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 4, 2018 4 / 33

Page 5: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Outline

1 Sequential Access

2 List Iterators

3 The Iterator Class

4 Example

5 Reverse Iterators

6 Assignment

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 5 / 33

Page 6: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterators

Definition (List Iterator)A list iterator is an object that is associated with a list and refers to aposition in that list.

The iterator uses the most efficient means available to do this,which depends 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 4, 2018 6 / 33

Page 7: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

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 readilyaccess that position’s successor, thereby greatly improvingsequential access.Furthermore, as a separate object, we may create as manyiterators for a list as we like.

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 7 / 33

Page 8: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

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 4, 2018 8 / 33

Page 9: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Behavior

The list elements

1st positionbeyond 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 4, 2018 9 / 33

Page 10: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Behavior

The list elements

1st positionbeyond 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 4, 2018 9 / 33

Page 11: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Behavior

The list elements

1st positionbeyond 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 4, 2018 9 / 33

Page 12: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Behavior

The list elements

1st positionbeyond 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 4, 2018 9 / 33

Page 13: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Behavior

The list elements

1st positionbeyond 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 4, 2018 9 / 33

Page 14: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Behavior

The list elements

1st positionbeyond 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 4, 2018 9 / 33

Page 15: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Behavior

The list elements

1st positionbeyond 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 4, 2018 9 / 33

Page 16: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Behavior

The list elements

1st positionbeyond 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 4, 2018 9 / 33

Page 17: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Behavior

The list elements

1st positionbeyond 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 4, 2018 9 / 33

Page 18: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Behavior

The list elements

1st positionbeyond 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 4, 2018 9 / 33

Page 19: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Behavior

The list elements

1st positionbeyond 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 4, 2018 9 / 33

Page 20: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Outline

1 Sequential Access

2 List Iterators

3 The Iterator Class

4 Example

5 Reverse Iterators

6 Assignment

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 10 / 33

Page 21: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

The Iterator Class

We create the LinkedListwIter class as a subclass of theLinkedList class.We define the Iterator class within the LinkedListwIterclass.Thus, the Iterator class is a dependent class, depending onthe LinkedListwIter class.

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 11 / 33

Page 22: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

The Iterator Class

The Iterator Classclass LinkedListwIter : public LinkedList{// Iterator class definition

public:class Iterator{

public:Iterator();

...};

// LinkedListwIter class definitionpublic:// LinkedListwIter member functionsprivate:// LinkedListwIter data members

};

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 12 / 33

Page 23: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

The Iterator Class

The Iterator Classclass LinkedListwIter : public LinkedList{// Iterator class definition

public:class Iterator{

public:Iterator();

...};

// LinkedListwIter class definitionpublic:// LinkedListwIter member functionsprivate:// LinkedListwIter data members

};

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 13 / 33

Page 24: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

The Iterator Class

This places the Iterator class within the scope of theLinkedListwIter class.Therefore, the full name if the Iterator class isLinkedListwIter<T>::Iterator

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 14 / 33

Page 25: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Data Members

List Iterator Data Membersconst LinkedList<T>* m_list;LinkedListNode<T>* m_node;

m_list – A pointer to the associated list.m_node – A pointer to a node in the associated list.The data members have protected access.Note that m_list is constant.Therefore, once it is set by the Iterator constructor, it cannot bechanged.

Why is that a good idea?

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 15 / 33

Page 26: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Data Members

List Iterator Data Membersconst LinkedList<T>* m_list;LinkedListNode<T>* m_node;

m_list – A pointer to the associated list.m_node – A pointer to a node in the associated list.The data members have protected access.Note that m_list is constant.Therefore, once it is set by the Iterator constructor, it cannot bechanged.Why is that a good idea?

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 15 / 33

Page 27: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Constructors

List ConstructorsIterator(const LinkedListwIter& lst,

LinkedListNode* p);

Iterator(LinkedListwIter, LinkedListNode*) –Constructs an iterator associated with a specified list.

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 16 / 33

Page 28: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Facilitators

List Iterator Facilitatorsbool isEqual(const Iterator& it) const;

isEqual() – Determines whether two iterators are equal.

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 17 / 33

Page 29: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Member Functions

List Iterator Member FunctionsT& 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 4, 2018 18 / 33

Page 30: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

List Iterator Member Functions

List Iterator Member Functionsbool 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 4, 2018 19 / 33

Page 31: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

LinkedListwIter Member Functions

LinkedListwIter Member FunctionsIterator 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 4, 2018 20 / 33

Page 32: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Sequential Access with Iterators

A for Looptypedef 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 4, 2018 21 / 33

Page 33: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Additional List Member Functions

Additional List Member FunctionsT element(const Iterator& curr);T& element(const Iterator& curr);

T element() const – Returns a copy of the list element thatthe Iterator is pointing to.T& element() – Returns a reference to the list element that theIterator is pointing to.

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 22 / 33

Page 34: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Additional List Member Functions

Additional List Member FunctionsT operator[](Iterator& curr) const;T& operator[](Iterator& curr);

T operator[]() const – Returns a copy of the list elementthat the Iterator is pointing to.T& operator[]() – Returns a reference to the list element thatthe Iterator is pointing to.

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 23 / 33

Page 35: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Outline

1 Sequential Access

2 List Iterators

3 The Iterator Class

4 Example

5 Reverse Iterators

6 Assignment

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 24 / 33

Page 36: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Iterator Example

Selection Sort with Indexesvoid sort(){

int base = 0;while (base < size){

int min = base;int curr = min;while (++curr < size)

if (element[curr] < element[min])min = curr;

T temp = element[base];element[base] = element[min];element[min] = temp;++base;

}}

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 25 / 33

Page 37: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Iterator Example

Selection Sort with Pointersvoid sort(){

LinkedListNode<T>* base = m_head;while (base != NULL){

LinkedListNode<T>* min = base;LinkedListNode<T>* curr = min;while ((curr = curr->m_next) != NULL)if (curr->m_value < min->m_value)

min = curr;T temp = base->m_value;base->m_value = min->m_value;min = temp;base = base->m_next;

}}

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 26 / 33

Page 38: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Iterator Example

Selection Sort with Iteratorsvoid sort(){

Iterator base.begin();while (base != end()){

Iterator min = base;Iterator curr = min;while (++curr != end())

if (*curr < *min)min = curr;T temp = *base;

*base = *min;

*min = temp;++base;

}}

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 27 / 33

Page 39: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Additional List Member Functions

Additional List Member FunctionsIterator searchIter(const T& value);void sortIter();

searchIter() – Searches for the specified value and returns anIterator to it if it is found. If it is not found, then the Iterator is equalto end().sortIter() – Sorts the list by using Iterators rather thanindexes.

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 28 / 33

Page 40: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Decrementing an Iterator

We can use the operator -- to back up to the previous listmember.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 4, 2018 29 / 33

Page 41: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Outline

1 Sequential Access

2 List Iterators

3 The Iterator Class

4 Example

5 Reverse Iterators

6 Assignment

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 30 / 33

Page 42: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

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” ofa 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 4, 2018 31 / 33

Page 43: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Outline

1 Sequential Access

2 List Iterators

3 The Iterator Class

4 Example

5 Reverse Iterators

6 Assignment

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 32 / 33

Page 44: List Iterators - Lecture 28 Sections 17.1 - 17people.hsc.edu/faculty-staff/robbk/Coms262/Lectures... · position in that list. The iterator uses the most efficient means available

Assignment

HomeworkRead Sections 17.1 - 17.3.

Robb T. Koether (Hampden-Sydney College) List Iterators Wed, Apr 4, 2018 33 / 33