List Iterators in Linked Lists: Implementation and Use Cases, Study notes of Data Structures and Algorithms

An implementation guide for list iterators in a linked list data structure using c. It explains the role of iterators in accessing and modifying elements in the middle of a linked list, and discusses functions such as hasnext, current, remove, change, and add. The document also includes examples and an analogy with a cursor in a word processor.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-mhf
koofers-user-mhf 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
worksheet 12: List Iterators Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 12: List Iterators
The careful reader might have noticed that we motivated the concept of a linked
list by saying insertions in the middle were more efficient than for a Vector, but
that subsequent lessons have only considered data structures in which elements
are added to the
end. The problem
is that without an
index it is difficult
to describe a
position in the
middle of a linked
list. One way to do
so is to use an
iterator. The
iterator interface has the definition shown at right. The combination of the
functions next and hasNext allows the user to write a loop that cycles over the
values in a collection. The remove method deletes the value most recently
returned as the current.
The LinkedList abstraction adds a pair of new functions. Through the execution of
these methods the user can modify the value held by a link, or add a new value
into the middle of a linked list. The newly added value will appear just before the
current element (or at the front of the list, if there is no current element).
Your task is to complete the implementation of the list iterator functions. These
build on the linked list abstraction you created in the previous lesson. Recall that
this list maintained sentinels at both the front and back, and used double links.
Be careful with the remove method. You want to make sure that when the next
iteration of the loop is performed the next element in sequence will be produced.
In order to solve this problem, remove must set the current element back to the
previous value before freeing its node. You can assume that the routines
listAddBefore and listRemoveLink that you wrote in an earlier lesson are
available.
struct listIterator {
struct list *list;
struct dlink *current;
};
/* prototypes for functions written in previous Lesson */
void listAddBefore (struct dlink *lnk, double d);
void listRemoveLink (struct dlink *lnk);
void listIteratorCreate (struct list *lst, struct listIterator
*litr);
int listIteratorHasNext (struct listIterator *litr);
EleType listIteratorCurrent (struct listIterator *litr);
void listIteratorRemove (struct listIterator *litr);
void listIteratorChange (struct listIterator *litr, EleType d);
void listIteratorAdd (struct listIterator *litr, EleType d);
pf3
pf4

Partial preview of the text

Download List Iterators in Linked Lists: Implementation and Use Cases and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Worksheet 12: List Iterators

The careful reader might have noticed that we motivated the concept of a linked list by saying insertions in the middle were more efficient than for a Vector, but that subsequent lessons have only considered data structures in which elements are added to the end. The problem is that without an index it is difficult to describe a position in the middle of a linked list. One way to do so is to use an iterator. The iterator interface has the definition shown at right. The combination of the functions next and hasNext allows the user to write a loop that cycles over the values in a collection. The remove method deletes the value most recently returned as the current. The LinkedList abstraction adds a pair of new functions. Through the execution of these methods the user can modify the value held by a link, or add a new value into the middle of a linked list. The newly added value will appear just before the current element (or at the front of the list, if there is no current element). Your task is to complete the implementation of the list iterator functions. These build on the linked list abstraction you created in the previous lesson. Recall that this list maintained sentinels at both the front and back, and used double links. Be careful with the remove method. You want to make sure that when the next iteration of the loop is performed the next element in sequence will be produced. In order to solve this problem, remove must set the current element back to the previous value before freeing its node. You can assume that the routines listAddBefore and listRemoveLink that you wrote in an earlier lesson are available. struct listIterator { struct list *list; struct dlink current; }; / prototypes for functions written in previous Lesson */ void listAddBefore (struct dlink *lnk, double d); void listRemoveLink (struct dlink *lnk); void listIteratorCreate (struct list *lst, struct listIterator *litr); int listIteratorHasNext (struct listIterator *litr); EleType listIteratorCurrent (struct listIterator *litr); void listIteratorRemove (struct listIterator *litr); void listIteratorChange (struct listIterator *litr, EleType d); void listIteratorAdd (struct listIterator *litr, EleType d);

void listIteratorCreate (struct list *lst, struct listIterator *litr) { } int listIteratorHasNext (struct listIterator *litr) { } double listIteratorCurrent (struct listIterator *litr) { }

On Your Own

  1. What functions does the ListIterator provide that were not part of the vector iterator?
  2. Give an example that illustrates how a list iterator can be used to remove a value from the middle of a linked list.
  3. Give an example that illustrates how a list iterator can be used to change the value of a link in the middle of a linked list.
  4. Give an example that illustrates how a list iterator can be used to add a new value into the middle of a linked list. Conceptually, you can think of an iterator as pointing between two elements. This is similar to the way that a cursor in a word processor points between two characters: a b | c d Typing a character is similar to calling add. The new value is placed immediately before the cursor. Hitting the delete key is similar to calling remove. The value removed will be the value to the left of the cursor. The analogy is not perfect, because a word processor allows you to delete several characters, while you are not allowed to call remove twice in succession. Why might you want to add a value into the middle of a list? A common reason is to maintain a list of elements in order, for instance of a list of names in sequence. To insert a name you iterate until you find the correct location, or until the end of the list is reached: void addOrdered (struct list *lst, EleType d) { struct listIterator litr; listIteratorCreate(lst, litr); while (listIteratorHasNext(itr) && LT(listIteratorCurrent(), d)) ; / do nothing / listIteratorAdd(litr, d); / add before current location */ } Note that while the process of adding the new value is O(1), the task of finding the correct location to place the element in this case is O(n).