Chapter 12 Lists and Iterators. List Its an abstract concept not a vector, array, or linked structure. Those are implementations of a List. A list is a.

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
COMP171 Fall 2005 Lists.
Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
ITEC200 Week04 Lists and the Collection Interface.
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
C/c++ 4 Yeting Ge.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Beginning C++ Through Game Programming, Second Edition
Chapter 14 Operator Overloading: What does + mean? Consider the code below. Note the multiple uses of “+”. #include using namespace std; int main() { int.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and.
COMP171 Data Structure & Algorithm Tutorial 1 TA: M.Y.Chan.
. Templates. Example… A useful routine to have is void Swap( int& a, int &b ) { int tmp = a; a = b; b = tmp; }
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Trees Main and Savitch Chapter 10. Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Object Oriented Data Structures
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
C++ Classes and Data Structures Jeffrey S. Childs
CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
Containers and Iterators The safe array class VectorInfo is an example of a “container” class: #include typedef int Integer; typedef Integer * IntegerArray;
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Chapter 3 Templates. Objective In Chapter 3, we will discuss: The concept of a template Function templates Class templates vector and matrix classes Fancy.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Linked List ADT used to store information in a list
CMSC 202 Lesson 22 Templates I.
Data Abstraction: The Walls
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Templates I CMSC 202.
Data Structures & Programming
Presentation transcript:

Chapter 12 Lists and Iterators. List Its 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 dont know and we dont 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 dont 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 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 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.

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 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() { int i, 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 Book also notes that sometimes the older notation Template 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. T his 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;

NOTE: All of the code is in the header file, contrasting with what weve done previously with using separate header and implementation files. Thats 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.

List5: Linked List implementation of a generic list.

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