240 likes | 351 Views
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
E N D
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.
Can scroll from the beginning to the end • usually 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.
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.
Use of multiple iterators objects allow concurrent scrolling through a list. • Useful for sorts or more complex queries from a list • E.g. find all elements in the list having the same something as the current element.
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.
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.
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.
There are more fundamental questions: • Do you want to understand the technologies that you use? • If you don’t understand them, who will?
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.
Goals: • Create lists so that apps that use them are not dependent on the implementation. • Create lists to handle multiple data types.
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.
The IteratorNext() 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.
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.
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.
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.
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.
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() { inti, j, k; i=78; j=23; k=max1(i,j); cout << k; }
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
The book uses template <typename T> Book also notes that sometimes the older notation Template <class T> is used Book recommends the former for reasons on page 644
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.
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;
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.
List6: Linked List implementation of a generic list containing base and derived node objects.