Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Input iterator.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists Geletaw S..
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
CSE Lecture 12 – Linked Lists …
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
M180: Data Structures & Algorithms in Java
List Representation - Chain Fall 2010 CSE, POSTECH.
Maps. Hash Tables. Dictionaries. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
Linear Lists – Linked List Representation CSE, POSTECH.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
CSE 332: C++ Associative Containers II Associative Containers’ Associated Types Associative containers declare additional types –A key_type gives the type.
Data Structures Using C++ 2E
L.Chen1 Chapter 3 DATA REPRESENTATION(2) L.Chen A Chain Iterator Class A Chain Iterator Class ( 遍历器类 )  An iterator permits.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Forward iterator.
1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
ITERATORS. Iterator An iterator in C++ is a concept that refines the iterator design pattern into a specific set of behaviors that work well with the.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists.
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
Lab-12 Keerthi Nelaturu. BitList Stores bit values in Linked List Has to be stored in right to left order Example :
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Circular, Doubly-Linked Lists Node Composition List Class Pushing and Popping Values Insert and Erase at Arbitrary Locations List Implementation.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
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.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Object-Oriented Programming (OOP) Lecture No. 42.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Chapter 16: Linked Lists.
Reminder: Course Policy
C++ Programming:. Program Design Including
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
Design of a HashTable and its Iterators
Design of a HashTable and its Iterators
Lecture 7-2 : STL Iterators
Lectures linked lists Chapter 6 of textbook
Standard Template Library (STL)
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
The Class chain.
Lecture 7-2 : STL Iterators
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Linked Lists [AJ 15].
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Doubly Linked List Implementation
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Templates and Iterators
Containers: Queue and List
The Class chain.
The Class chain.
Doubly Linked List Implementation
The List Container and Iterators
Data Structures & Programming
Presentation transcript:

Iterators & Chain Variants

Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Input iterator Output iterator Forward iterator Bidirectional iterator Reverse iterator

Forward Iterator Allows only forward movement through the elements of a data structure.

Forward Iterator Methods  iterator(T* thePosition) Constructs an iterator positioned at specified element  dereferencing operators * and ->  Post and pre increment and decrement operators ++  Equality testing operators == and !=

Bidirectional Iterator Allows both forward and backward movement through the elements of a data structure.

Bidirectional Iterator Methods  iterator(T* thePosition) Constructs an iterator positioned at specified element  dereferencing operators * and ->  Post and pre increment and decrement operators ++ and –  Equality testing operators == and !=

Iterator Class  Assume that a forward iterator class ChainIterator is defined within the class Chain.  Assume that methods Begin() and End() are defined for Chain. Begin() returns an iterator positioned at element 0 (i.e., leftmost node) of list. End() returns an iterator positioned one past last element of list (i.e., NULL or 0).

Using An Iterator Chain ::iterator xHere = x.Begin(); Chain ::iterator xEnd = x.End(); for (; xHere != xEnd; xHere++) examine( *xHere); vs for (int i = 0; i < x.Size(); i++) examine(x.Get(i));

Merits Of An Iterator it is often possible to implement the ++ and -- operators so that their complexity is less than that of Get. this is true for a chain many data structures do not have a get by index method iterators provide a uniform way to sequence through the elements of a data structure

A Forward Iterator For Chain class ChainIterator { public: // some typedefs omitted // constructor comes here // dereferencing operators * & ->, pre and post // increment, and equality testing operators // come here private: ChainNode *current;

Constructor ChainIterator(ChainNode * startNode = 0) {current = startNode;}

Dereferencing Operators T& operator*() const {return current->data;} T& operator->() const {return &current->data;}

Increment ChainIterator& operator++() // preincrement {current = current->link; return *this;} ChainIterator& operator++(int) // postincrement { ChainIterator old = *this; current = current->link; return old; }

Equality Testing bool operator!=(const ChainIterator right) const {return current != right.current;} bool operator==(const ChainIterator right) const {return current == right.current;}

Chain With Header Node abcde NULL headerNode

Empty Chain With Header Node headerNode NULL

Circular List abcde firstNode

Doubly Linked List abcde NULL firstNode NULL lastNode

Doubly Linked Circular List abcde firstNode

Doubly Linked Circular List With Header Node abce headerNode d

Empty Doubly Linked Circular List With Header Node headerNode

The STL Class list  Linked implementation of a linear list.  Doubly linked circular list with header node.  Has many more methods than our Chain.  Similar names and signatures.