Chapter 12 Lists and Iterators .

24
Chapter 12 Lists and Iterators. List It’s an abstract concept not a vector, array, or linked structure. Those are implementations of a List. A list is a Container. It has a Beginning End and stuff in between.

description

Chapter 12 Lists and Iterators . List It’s an abstract concept n ot a vector, array, or linked structure. Those are implementations of a List. A list is a Container. It has a Beginning End and stuff in between. Can scroll from the beginning to the end - PowerPoint PPT Presentation

Transcript of Chapter 12 Lists and Iterators .

Page 1: Chapter 12 Lists and  Iterators .

Chapter 12 Lists and Iterators.

ListIt’s an abstract conceptnot a vector, array, or linked structure. Those are implementations of a List.

A list is a Container.It has a

BeginningEndand stuff in between.

Page 2: Chapter 12 Lists and  Iterators .

Can scroll from the beginning to the endusually in the reverse direction as well.Usually has an indicator for a “current” position.Has the ability to insert at the end or the ability to insert near the current position.Has the ability to remove the current node.

Page 3: Chapter 12 Lists and  Iterators .

Iterator

An object used to track a position in a list.Different iterators may be used to identify the first, last, and current element in the list.Similar to a subscript if list is an array.Similar to a pointer if list is a linked list.Hides details of the list implementation from the user.

Page 4: Chapter 12 Lists and  Iterators .

Use of multiple iterators objects allow concurrent scrolling through a list.Useful for sorts or more complex queries from a listE.g. find all elements in the list having the same something as the current element.

Page 5: Chapter 12 Lists and  Iterators .

Demo program from Chapter 12

list1.cpp uses the CPP list and iterator classes and shows how to insert to, delete from, and scroll through the list.You can see list elements in the debug window.Note that strings could be replaced by int types and the test harness would largely be the same.

Page 6: Chapter 12 Lists and  Iterators .

How is the list implemented?

At one level, we don’t know and we don’t care.There is NO knowledge of how this list is implemented. Could be a linked list, an array of objects or pointers, a vector of objects or pointers, or some other structure not yet studied.

Page 7: Chapter 12 Lists and  Iterators .

Should we care about the list implementation?

Yes!!!! Some often stated reasons are:

You many encounter languages that do not support such facilities.You may be called upon to develop more complex solutions to problems. These are a good base for study.However, as languages and frameworks evolve, these often cited reasons become weaker.

Page 8: Chapter 12 Lists and  Iterators .

There are more fundamental questions:Do you want to understand the technologies that you use? If you don’t understand them, who will?

Page 9: Chapter 12 Lists and  Iterators .

Do you know the ramifications of using a list for large amounts of data?Will finding an element be quick or take a lot of time?Will inserting or removing an element be quick or take a lot of time?These questions cannot be answered without knowledge of how a list is implemented.

Page 10: Chapter 12 Lists and  Iterators .

Goals:

Create lists so that apps that use them are not dependent on the implementation.Create lists to handle multiple data types.

Page 11: Chapter 12 Lists and  Iterators .

List2: Linked List Implementation

List2.h, list2.cpp, and listTest.cpp.Remove <string> from the declaration in the test harness. Strings are the default.The list class has a first and last pointer.Each node contains a previous pointer, next pointer, and a string.

Page 12: Chapter 12 Lists and  Iterators .

The Iterator Next() method is from the book. I added the ++ operator.Reflects diagrams starting on page 479.Insertion and deletion is quick – independent of the list size.Finding elements can be very slow – requires a linear search. A real problem for large lists.Harder to use the debug window.

Page 13: Chapter 12 Lists and  Iterators .

Iterator Class

Identifies a current and last position in the list.Allows implementation details of the List to remain hidden. Nowhere in the test harness is there a hint that this is a linked list.Allows multiple iterations over the same list.Understand the mechanics of this implementation.

Page 14: Chapter 12 Lists and  Iterators .

List3: Vector pointer Implementation

Replace list2.h and list2.cpp with list3.h and list3.cpp. Uses a vector of pointers to Nodes.Nodes contain Strings only.Test harness is identical to that in list2 (except include list3.h instead). This is evidence that details are hidden and that careful design of a class allows independence from the applications that use it.

Page 15: Chapter 12 Lists and  Iterators .

Insertions and deletions take more time if the list must remain sorted.Finding things can be done more quickly if the list is sorted.Easier to use the debug window.

Page 16: Chapter 12 Lists and  Iterators .

Chapter 16 Templates:

Note motivation for code reuse on page 642.Syntax for Template Function Definition on page 644.Java (starting with 1.5) calls them generics.

Page 17: Chapter 12 Lists and  Iterators .

Example#include <iostream>#include <string>using namespace std;

template<typename T>T max1(const T& left, const T& right) //use max1; max is a key word{

if (left < right)return right;return left;

}void main(){

int i, j, k;i=78;j=23;k=max1(i,j);cout << k;

}

Page 18: Chapter 12 Lists and  Iterators .

I can change int to any type for which “<“ is defined and it will work (double, float, char, string -- any class having “<“ as an overloaded operator.NOTE: would have a problem if I wrote k=max1(3, 5.0). Compiler does not know what to do since there are multiple conversions possible.This can be addressed by using multiple generic types

Page 19: Chapter 12 Lists and  Iterators .

The book uses template <typename T>

Book also notes that sometimes the older notation

Template <class T> is usedBook recommends the former for

reasons on page 644

Page 20: Chapter 12 Lists and  Iterators .

Classes can be defined using templates (See example from the code snippet file.)A template is sometimes called a factory for classes.

For example, the Pair template from the notes can be used to produce objects (and methods) containing pairs of different types.

Page 21: Chapter 12 Lists and  Iterators .

List4: Vector pointer implementation of a generic list.

This is the same as list3, except the type is not specified in the class definitions.The test harness should specify the type to be used. For example:

List<string> staff;List<string> pos;

Page 22: Chapter 12 Lists and  Iterators .

NOTE: All of the code is in the header file, contrasting with what we’ve done previously with using separate header and implementation files. That’s because there is no implementation until the compiler encounters a reference to the template using a specific class. As such, it needs to include the template definition in order to build the object code. Building the actual code for the template occurs during compile time and, as such, some refer to this as compile time polymorphism.

Page 23: Chapter 12 Lists and  Iterators .

List5: Linked List implementation of a generic list.

Page 24: Chapter 12 Lists and  Iterators .

List6: Linked List implementation of a generic list containing base and derived node objects.