List Iterator Implementation

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Stacks, Queues, and Linked Lists
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked list.
Functions Prototypes, parameter passing, return values, activation frams.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Reverse Polish Notation Written by J.J. Shepherd.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
CSE 3358 NOTE SET 8 Data Structures and Algorithms.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
C++ Programming:. Program Design Including
Popping Items Off a Stack Using a Function Lesson xx
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Pointers and Linked Lists
Dynamic Memory Allocation
Dr. Bernard Chen Ph.D. University of Central Arkansas
Homework 4 questions???.
Chapter 4 Linked Lists.
CSCE 210 Data Structures and Algorithms
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Pointers and Linked Lists
Recursion & Linked Lists
Popping Items Off a Stack Lesson xx
Lecture 20 Linked Lists Richard Gesick.
Pointers and Linked Lists
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Linked Lists with Tail Pointers
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Recursive Linked List Operations
Recursive Linked Lists
Binary Tree Traversals
Constructors and Destructors
Inheritance: Polymorphism and Virtual Functions
UNIT V Run Time Environments.
Lists.
Data Structures & Algorithms
Using a Queue Chapter 8 introduces the queue data type.
Chapter 18 Recursion.
Using a Queue Chapter 8 introduces the queue data type.
The Stack.
Recursive Linked Lists
Linked Lists.
Recursive Linked Lists
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

List Iterator Implementation Lecture 33 Fri, Apr 13, 2007

List Traversals To traverse a list is to move from one end to the other, “visiting” each node along the way. Forward traversals go from head to tail Reverse traversals go from tail to head. The meaning of “visiting” a node is left unspecified at this point. The meaning will be specified at the time of the traversal. 5/3/2019 List Iterators

The Traversal Function Introduce a new List member function The parameter, named visit, is a pointer to a function. The function visit() has prototype void traverse(ListIterator& it, void (* visit)(ListIterator&)); void visit(ListIterator&); 5/3/2019 List Iterators

Traversals and Iterators A traversal may be implemented as follows. void traverse(ListIterator& it, void (* visit)(ListIterator&)) { for (it.begin(); !it.atEnd(); ++it) visit(it); return; } 5/3/2019 List Iterators

Example: Print the List Define a visiting function print(). Use print() to print (visit) the members of the List. void print(ListIterator& it) { cout << *it << endl; return; } ListIterator it(list); list.traverse(it, print); 5/3/2019 List Iterators

The Visiting Function Suppose we made print() a member function. The program would not compile because the prototype of print() would be What can be done about this? void print(List* this, ListIterator& it); 5/3/2019 List Iterators

Static Member Functions A static member function does not have the implicit this parameter. It may not invoke any non-static member function. If we make print() a static member function then it will work. static void print(ListIterator& it); 5/3/2019 List Iterators

Implemention of Reverse Iterators To construct a reverse iterator for a linked list, Push NULL onto the stack. Then push the addresses of the nodes onto a stack. To increment the reverse list iterator Pop an address off the stack. Assign it to the node pointer. 5/3/2019 List Iterators

Insertion Sort with Iterators InsertionSortwIter.cpp 5/3/2019 List Iterators