Iterator in a doubly linked list in C
09:45 07 Jan 2026

Working on an assignment for a Data Structures course I started thinking about some implementation differences for iterators. Working in C. Let's say my iterator looks like this:

struct list_iter {
    list_t *list;
    lnode_t *node;
};

And the interface has the following methods:

list_iter_t *list_createiter(list_t *list);
void list_destroyiter(list_iter_t *iter);
int list_hasnext(list_iter_t *iter); // returns 0 if iterator is exhausted, otherwise 1
void *list_next(list_iter_t *iter);
void list_resetiter(list_iter_t *iter);

In my list_next method. What would the best practice be? Do I:

  1. save the current node, advance the iterator and return the saved node.

  2. have a special case for head, advanced the iterator and return that node.

  3. The list_next shouldn't return anything, I should have another method for this and list_next should just handle advancing the iterator.

Or whichever, it's the same?

Thanks!

c linked-list iterator doubly-linked-list