Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 Bidirectional iterator Reverse iterator Quite often, an application needs to examine all elements of an instance. The elements of a linear list may be examined by doing examine(get(i)), 0 <= i < size(). This technique has much unnecessary overhead associated with it. For example, the index i is checked each time get() is invoked. What if the data structure we are dealing with doesn’t have a get(index) method? Iterators provide a uniform way to examine all elements of a data structure instance, and often with much smaller overhead than using other techniques.

2 Linked Representation
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

3 Layout of L = (a,b,c,d,e) using an array representation.
Memory Layout Layout of L = (a,b,c,d,e) using an array representation. a b c d e The figures show computer memory. An array uses a contiguous chunk of memory. A linked representation uses an arbitrary layout. c a e d b

4 Linked Representation
c a e d b firstNode pointer (or link) in e is NULL use a variable firstNode to get to the first element a

5 Normal Way To Draw A Linked List
b c d e NULL firstNode link or pointer field of node data field of node

6 Chain firstNode NULL a b c d e A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a NULL pointer.

7 Node Representation template <class T> struct chainNode {
// data members T element; chainNode<T> *next; // constructors come here }; next element

8 Constructors Of chainNode
? chainNode() {} ? element chainNode(const T& element) {this->element = element;} chainNode(const T& element, chainNode<T>* next) {this->element = element; this->next = next;} next element

9 get(0) a b c d e firstNode checkIndex(0);
NULL firstNode checkIndex(0); desiredNode = firstNode; // gets you to first node return desiredNode->element; Do a checkIndex first. Then move to desired node. Once you are at the desired node, you can return the element in that node as in return desiredNode.element; When firstNode = null, there is no element whose index is 0.

10 get(1) a b c d e firstNode checkIndex(1);
NULL firstNode checkIndex(1); desiredNode = firstNode->next; // gets you to second node return desiredNode->element;

11 get(2) a b c d e firstNode checkIndex(2);
NULL firstNode checkIndex(2); desiredNode = firstNode->next->next; // gets you to third node return desiredNode->element;

12 get(5) a b c d e firstNode checkIndex(5); // throws exception
NULL firstNode checkIndex(5); // throws exception desiredNode = firstNode->next->next->next->next->next; // desiredNode = NULL return desiredNode->element; // NULL.element Note that checkIndex would throw an exception. Other two statements executed only when checkIndex not done.

13 Erase An Element erase(0) deleteNode = firstNode;
b c d e NULL firstNode erase(0) First do a checkIndex. deleteNode = firstNode; firstNode = firstNode->next; delete deleteNode;

14 first get to node just before node to be removed
erase(2) a b d e NULL firstNode c b beforeNode c first get to node just before node to be removed beforeNode = firstNode->next;

15 save pointer to node that will be deleted
erase(2) firstNode null a b c d e beforeNode save pointer to node that will be deleted deleteNode = beforeNode->next;

16 now change pointer in beforeNode
erase(2) firstNode null a b c d e beforeNode now change pointer in beforeNode beforeNode->next = beforeNode->next->next; delete deleteNode;

17 insert(0,’f’) firstNode newNode
a b c d e NULL firstNode f newNode Step 1: get a node, set its data and link fields First, do a checkIndex. newNode = new chainNode<char>(theElement, firstNode);

18 insert(0,’f’) firstNode newNode Step 2: update firstNode
b c d e NULL firstNode f newNode Step 2: update firstNode firstNode = newNode;

19 firstNode = new chainNode<char>(‘f’, firstNode);
One-Step insert(0,’f’) a b c d e NULL firstNode f newNode firstNode = new chainNode<char>(‘f’, firstNode);

20 first find node whose index is 2
insert(3,’f’) a b c d e NULL firstNode f newNode beforeNode c first find node whose index is 2 next create a node and set its data and link fields chainNode<char>* newNode = new chainNode<char>( ‘f’, beforeNode->next); finally link beforeNode to newNode beforeNode->next = newNode;

21 Two-Step insert(3,’f’) beforeNode = firstNode->next->next;
a b c d e NULL firstNode f newNode beforeNode beforeNode = firstNode->next->next; beforeNode->next = new chainNode<char> (‘f’, beforeNode->next);

22 To-do Practice: Read more on iterators: Read more on linked lists:
Read more on iterators: Read more on linked lists:


Download ppt "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."

Similar presentations


Ads by Google