chapter 12 lists and iterators
Download
Skip this Video
Download Presentation
Chapter 12 Lists and Iterators .

Loading in 2 Seconds...

play fullscreen
1 / 24

Chapter 12 Lists and Iterators . - PowerPoint PPT Presentation


  • 85 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 12 Lists and Iterators .' - umay


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 12 lists and iterators
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.
slide2
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
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.
slide4
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
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
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
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
There are more fundamental questions:
  • Do you want to understand the technologies that you use?
  • If you don’t understand them, who will?
slide9
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
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: Linked List Implementation
  • List2.h, list2.cpp, and listTest.cpp.
  • Remove 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.
slide12
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
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
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.
slide15
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
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
Example

#include

#include

using namespace std;

template

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;

}

slide18
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
slide19
The book uses

template

Book also notes that sometimes the older notation

Template is used

Book recommends the former for reasons on page 644

slide20
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
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 staff;

List pos;

slide22
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
List6: Linked List implementation of a generic list containing base and derived node objects.
ad