1 / 19

CS261 – Data Structures

CS261 – Data Structures. Iterator ADT Dynamic Array and Linked List. Goals. Why do we need iterators? Iterator ADT Linked List and Dynamic Array Iterators. Iterator Concept. Problem: How do you provide a user of a container access to the elements, without exposing the inner structure?

stan
Download Presentation

CS261 – Data Structures

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. CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

  2. Goals • Why do we need iterators? • Iterator ADT • Linked List and Dynamic Array Iterators

  3. Iterator Concept • Problem: How do you provide a user of a container access to the elements, without exposing the inner structure? • Think of two developers: one writing the container (that’s you!!!) , the other using the container (that’s someone using your library to build an application where they need, for example, a stack implementation)

  4. Traversing a Container – as Developer For example, within the Linked List container you (the developer) wrote a loop such as the following: structLinkedList *list; struct Link *l; ... /* Initialize list. */ l = list->frontSentinel->next; while(l!=list->backSentinel){ …do something… l=l->next; This is fine within the container library code itself, but we don’t want users of the container library to have to know about links…or worse yet, be able to manipulate links!

  5. Encapsulation • Chapter 5: Hide the implementation details behind a simple and easy to remember interface (ie. abstraction mechanism) • Users should not know about links, arrays, size, capacity, etc. • Users should know and use: push, pop, contains, remove, etc.

  6. Iterators to the Rescue • So, how do we allow them to loop through the data without manipulating links? • Provide a “facilitator object” • Maintain encapsulation • Hide the details away from the user. Allow them to work at an abstract level. • Also helps with code maintenance: a loop is essentially the same for a user whether using a LinkedList or DynamicArray

  7. Iterator ADT Solution: define an interface that provides methods for writing loops void createIter(container *l); inthasNextIter(structIter *itr); TYPE nextIter(structIter *itr); void removeIter(structIter *itr); void changeIter(structIter *itr, TYPE val); void addIter(structIter *itr, TYPE val);

  8. Iterator: Typical Usage TYPE cur; /* current collection val*/ structlinkedList *list; linkedListIterator *itr; list = createLinkedList(…) itr = createLinkedListIter(list) while (hasNextListIter(itr)) { cur = nextListIter(itr); if (cur ...) removeListIter(itr); }

  9. Simplifying Assumptions & Properties of the Iterator ADT • Function next and hasNextmust beinterleaved • Call remove after next • Cannot call remove twice in a row without a calling hasNext

  10. LinkedListIterator structlinkedListIterator { structlinkedList *lst; structdlink *currentLink; } structlinkedListIterator*createLinkedListIterator ( structlinkedList *lst) { structlinkedListIterator *itr; itr = malloc(sizeof(structlinkedListIterator)); itr->lst = lst; itr->current = lst->frontSentinel; }

  11. After Initialization Itr list current backSent List frontSent prev prev prev prev prev … Link Link next next next next next front

  12. Strategy • HasNext • Returns T (F) to the user if there is (is not) another value remaining to be enumerated • Updates current • Next • Return the next value to be enumerated • Remove • Removes the value (and link) that was last returned by call to Next()

  13. After Initialization Itr list current backSent List frontSent prev prev prev prev prev … Link Link next next next next next front

  14. After One Iteration:hasNext, next Itr list current backSent List frontSent prev prev prev prev prev 5 10 15 next next next next next front

  15. After Two Iterations Itr list current backSent List frontSent prev prev prev prev prev 5 10 15 next next next next next front

  16. After Three Iterations We’ve enumerated all elements Subsequent calls to HasNext will evaluate to false Itr list current backSent List frontSent prev prev prev prev prev 5 10 15 next next next next next front

  17. After Two Iterations – then Remove Remove the last value enumerated (10) Where should current be after the removal? Itr list current backSent List frontSent prev prev prev prev prev 5 10 15 next next next next next front

  18. After Two Iterations – then Remove Remove the last value enumerated (10) Where should current be after the removal? Itr list current backSent List frontSent prev prev prev prev 5 15 next next next next front

  19. Your Turn Worksheet#24 Linked List Iterator

More Related