


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
The concept of list iterators in the context of linked lists, which enable users to modify list elements by adding, changing, or removing values in the middle of the list. The implementation of various list iterator functions, such as create, hasnext, current, remove, change, and add. It also includes examples and explanations of how to use these functions to manipulate linked lists. A part of an active learning approach to data structures using c course.
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



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) { } void listIteratorRemove (struct listIterator *litr) {