19
CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

Embed Size (px)

Citation preview

Page 1: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

CS261 – Data Structures

Iterator ADTDynamic Array and Linked List

Page 2: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

Goals

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

Page 3: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

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)

Page 4: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

Traversing a Container – as Developer

For example, within the Linked List container you (the developer) wrote a loop such as the following:

struct LinkedList *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!

Page 5: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

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.

Page 6: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

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

Page 7: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

Iterator ADT

Solution: define an interface that provides methods for writing loops

void createIter(container *l);

int hasNextIter(struct Iter *itr);

TYPE nextIter(struct Iter *itr);

void removeIter(struct Iter *itr);

void changeIter(struct Iter *itr, TYPE val);

void addIter(struct Iter *itr, TYPE val);

Page 8: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

Iterator: Typical Usage

TYPE cur; /* current collection val */

struct linkedList *list;

linkedListIterator *itr;

list = createLinkedList(…)

itr = createLinkedListIter(list)

while (hasNextListIter(itr)) {

cur = nextListIter(itr);

if (cur ...) removeListIter(itr);

}

Page 9: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

Simplifying Assumptions & Properties of the Iterator ADT

• Function next and hasNext must be interleaved

• Call remove after next• Cannot call remove twice in a row without a

calling hasNext

Page 10: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

LinkedListIterator

struct linkedListIterator {struct linkedList *lst;struct dlink *currentLink;

}

struct linkedListIterator *createLinkedListIterator ( struct linkedList *lst)

{

struct linkedListIterator *itr;

itr = malloc(sizeof(struct linkedListIterator));

itr->lst = lst;

itr->current = lst->frontSentinel;

}

Page 11: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

After Initialization

Link …

List

Linknext

list

frontSent

next nextnext

prev prev prevprev

front

next

prev

Itr

backSent

current

Page 12: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

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()

Page 13: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

After Initialization

Link …

List

Linknext

list

frontSent

next nextnext

prev prev prevprev

front

next

prev

Itr

backSent

current

Page 14: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

After One Iteration:hasNext, next

5

List

15next

list

frontSent

next nextnext

prev prev prevprev

front

next

prev

Itr

backSent

current

10

Page 15: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

After Two Iterations

5

List

15next

list

frontSent

next nextnext

prev prev prevprev

front

next

prev

Itr

backSent

current

10

Page 16: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

After Three Iterations

5

List

15next

list

frontSent

next nextnext

prev prev prevprev

front

next

prev

Itr

backSent

current

10

We’ve enumerated all elementsSubsequent calls to HasNext will evaluate to false

Page 17: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

After Two Iterations – then Remove

5

List

15next

list

frontSent

next nextnext

prev prev prevprev

front

next

prev

Itr

backSent

current

10

Remove the last value enumerated (10)Where should current be after the removal?

Page 18: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

After Two Iterations – then Remove

5

List

15next

list

frontSent

nextnext

prev prevprev

front

next

prev

Itr

backSent

current

Remove the last value enumerated (10)Where should current be after the removal?

Page 19: CS261 – Data Structures Iterator ADT Dynamic Array and Linked List

Your Turn

Worksheet#24 Linked List Iterator