

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: DATA STRUCTURES; Subject: Computer Science; University: Oregon State University; Term: Summer 2008;
Typology: Assignments
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Worksheet 23: Introduction to Iterator NAME: An Active Learning approach to Data Structures using C 1 /* conceptual interface */ Boolean hasNext ( ); EleType next ( ); void remove ( );
In Preparation : Read Chapter 8 to learn more about the idea of encapsulation and the iterator. If you have not done it already, complete Lessons 14 and 16 to learn more about adding and removing values from a dynamic array. One of the primary design principles for collection classes is encapsulation. The internal details concerning how an implementation works are hidden behind a simple and easy to remember interface. To use a Bag, for example, all you need know is the basic operations are add, collect and remove. The inner workings of the implementation for the bag are effectively hidden. When using collections a common requirement is the need to loop over all the elements in the collection, for example to print them to a window. Once again it is important that this process be performed without any knowledge of how the collection is represented in memory. For this person the conventional solution is to use a mechanism termed an Iterator. Each collection provides an associated iterator structure. Functions are used to initialize and manipulate this iterator. These functions are used in combination to write a simple loop that will cycle over the values in the collection. For example, the code at left illustrates the use of our dynamic array iterator. Notice that the iterator loop exposes nothing of the structure of the container class. The method remove is used to delete from the collection the value most recently returned by next. Calls on the functions hasNext and next must always be interleaved, as shown. Note that an iterator is an object that is separate from the collection itself. The iterator is a facilitator object , one that provides access to the container values. An iterator for the dynamic array will work by maintaining an index into the array representing the current location. This value is initially zero, and must be incremented by either the function hasNext or the function next? Think about this for a moment. Which one makes more sense? Then think carefully about the third function, remove. You must ensure that when the loop continues after a remove the next value following the removed element is not skipped. You can assume you have access to the dyArrayRemoveAt function you wrote in Lesson 21. void dyArrayRemoveAt (struct dyArray * dy, int index); dyArrayIterator itr; EleType current; dyArray data; … dyArrayIteratorInit (&data,&itr); while (dyArrayHasNext(&itr)) { current = dyArrayNext(&itr); … /* do something with current */ }
Worksheet 23: Introduction to Iterator NAME: An Active Learning approach to Data Structures using C 2 struct dyArrayIterator { struct dyArray * da; int currentIndex; }; void dyArrayIteratorInit (struct dyArray *da, struct dyArrayIterator *itr) { } int dyArrayIteratorHasNext (struct dyArrayIterator *itr) { } EleType dyArrayIteratorNext (struct dyArrayIterator *itr) { } void dyArrayIteratorRemove (struct dyArrayIterator *itr) { }