List Iterators: Implementing Middle Modifications in Linked Lists, Assignments of Data Structures and Algorithms

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

Pre 2010

Uploaded on 08/30/2009

koofers-user-0ou-2
koofers-user-0ou-2 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
worksheet 12: List Iterators Name:
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);
An Active Learning Approach to Data Structures using C 1
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: Implementing Middle Modifications in Linked Lists and more Assignments 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) { } void listIteratorRemove (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).