1 / 10

CS 261 - Winter 2009

CS 261 - Winter 2009. Iterators - Both Vector and LinkedList. Iterator - basic concept. Problem: How do you provide a user of a container access to the elements, without exposing the inner structure?

ronald
Download Presentation

CS 261 - Winter 2009

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 261 - Winter 2009 Iterators - Both Vector and LinkedList

  2. Iterator - basic concept • Problem: How do you provide a user of a container access to the elements, without exposing the inner structure? Think of binding times, or multiple programmers. One writing the container, the other using the collection

  3. A Linked List Loop For example, within the container you wrote a loop such as the following: for (Link p = firstLink; p != null; p = p->next) { This is fine within the container class itself, but we don’t want users of the class to have to know about links

  4. Solution - the Iterator • Solution: define an interface for writing loops. Only provide the methods necessary for writing simple loops: Iterator itr; listIteratorInit (&itr, &aList); While (listIteratorHasNext(&itr)) { current = listIteratorCurrent(&itr); if (current …) listIteratorRemove(&itr); }

  5. Information Hiding • Notice that the iterator loop says nothing about the inner workings of the container • The inner structure of the container is effectively encapsulated, the information is hidden

  6. The Iterator Interface void listIteratorInit (sturct list *l, struct listIterator *litr); int listIteratorHasNext (struct listIterator *litr); eleType listIteratorCurrent (struct listIterator *litr); void listIteratoRemove (struct listIterator *litr); void listIteratorChange (struct listIterator *litr, EleType newValue); void listIteratorAdd (struct listIterator *litr, EleTYpe newValue);

  7. Some simplifying assumptions • Next and hasNext are interleaved • Remove called after next • Cannot call remove twice in a row without a call on hasNext

  8. Iterators are more OOP • Iterators common in OOP languages, where you have polymorphism, interfaces, etc • But idea can be used in any language. • Very intuitive and easy to understand interface, easy to adapt

  9. Your Turn: • OK. Your turn. Todays worksheet is to implement the Dynamic Array Iterator.

  10. Oh. that exam on Monday? • Questions on big-Oh (what is the running time of …) • Conceptual questions related to big-Oh • Implementation questions like we have been doing (how to implement … )

More Related