Download presentation
Presentation is loading. Please wait.
Published byΜνήμη Παπαϊωάννου Modified over 6 years ago
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
Bidirectional Iterator
Allows both forward and backward movement through the elements of a data structure.
3
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 != hasNext, next, and remove are the methods of Java’s interface Iterator.
4
Iterator Class Assume that a bidirectional iterator class iterator is defined within the class arrayList. Assume that methods begin() and end() are defined for arrayList. begin() returns an iterator positioned at element 0 of list. end() returns an iterator positioned one past last element of list.
5
Using An Iterator arrayList<int>::iterator xHere = x.begin();
arrayList<int>::iterator xEnd = x.end(); for (; xHere != xEnd; xHere++) examine( *xHere); vs for (int i = 0; i < x.size(); i++) examine(x.get(i));
6
Merits Of An Iterator it is often possible to implement the ++ and -- operators so that their complexity is less than that of get 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
7
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
8
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
9
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
10
Normal Way To Draw A Linked List
b c d e NULL firstNode link or pointer field of node data field of node
11
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.
12
Node Representation template <class T> struct chainNode {
// data members T element; chainNode<T> *next; // constructors come here }; next element
13
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
14
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.
15
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;
16
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;
17
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.
18
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;
19
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;
20
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;
21
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;
22
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);
23
insert(0,’f’) firstNode newNode Step 2: update firstNode
b c d e NULL firstNode f newNode Step 2: update firstNode firstNode = newNode;
24
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);
25
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;
26
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);
27
To-do Practice: Read more on iterators: Read more on linked lists:
Read more on iterators: Read more on linked lists:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.