Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data...
-
date post
21-Dec-2015 -
Category
Documents
-
view
233 -
download
3
Transcript of Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data...
![Page 1: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/1.jpg)
Iterators & Chain Variants
![Page 2: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/2.jpg)
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
![Page 3: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/3.jpg)
Forward Iterator
Allows only forward movement through the elements of a data structure.
![Page 4: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/4.jpg)
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 !
=
![Page 5: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/5.jpg)
Bidirectional Iterator
Allows both forward and backward movement through the elements of a data structure.
![Page 6: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/6.jpg)
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 !
=
![Page 7: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/7.jpg)
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).
![Page 8: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/8.jpg)
Using An Iterator
Chain<int>::iterator xHere = x.Begin();
Chain<int>::iterator xEnd = x.End();
for (; xHere != xEnd; xHere++)
examine( *xHere);
vs
for (int i = 0; i < x.Size(); i++)
examine(x.Get(i));
![Page 9: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/9.jpg)
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
![Page 10: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/10.jpg)
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 hereprivate: ChainNode<T> *current;
![Page 11: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/11.jpg)
Constructor
ChainIterator(ChainNode<T> * startNode = 0)
{current = startNode;}
![Page 12: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/12.jpg)
Dereferencing Operators
T& operator*() const
{return current->data;}
T& operator->() const
{return ¤t->data;}
![Page 13: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/13.jpg)
Increment
ChainIterator& operator++() // preincrement {current = current->link; return *this;}
ChainIterator& operator++(int) // postincrement{ ChainIterator old = *this; current = current->link; return old;}
![Page 14: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/14.jpg)
Equality Testing
bool operator!=(const ChainIterator right) const
{return current != right.current;}
bool operator==(const ChainIterator right) const
{return current == right.current;}
![Page 15: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/15.jpg)
Chain With Header Node
a b c d e
NULL
headerNode
![Page 16: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/16.jpg)
Empty Chain With Header Node
headerNode
NULL
![Page 17: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/17.jpg)
Circular List
a b c d e
firstNode
![Page 18: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/18.jpg)
Doubly Linked List
a b c d e
NULL
firstNode
NULL
lastNode
![Page 19: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/19.jpg)
Doubly Linked Circular List
a b c d e
firstNode
![Page 20: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/20.jpg)
Doubly Linked Circular List With Header Node
a b c e
headerNode
d
![Page 21: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/21.jpg)
Empty Doubly Linked Circular List With Header Node
headerNode
![Page 22: Iterators & Chain Variants. Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator.](https://reader031.fdocuments.in/reader031/viewer/2022032006/56649d6c5503460f94a4c7af/html5/thumbnails/22.jpg)
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.