/* adlist.c - A generic doubly linked list implementation
*
- * Copyright (c) 2006-2009, Salvatore Sanfilippo <antirez at gmail dot com>
+ * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
return list;
}
+list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
+ listNode *node;
+
+ if ((node = zmalloc(sizeof(*node))) == NULL)
+ return NULL;
+ node->value = value;
+ if (after) {
+ node->prev = old_node;
+ node->next = old_node->next;
+ if (list->tail == old_node) {
+ list->tail = node;
+ }
+ } else {
+ node->next = old_node;
+ node->prev = old_node->prev;
+ if (list->head == old_node) {
+ list->head = node;
+ }
+ }
+ if (node->prev != NULL) {
+ node->prev->next = node;
+ }
+ if (node->next != NULL) {
+ node->next->prev = node;
+ }
+ list->len++;
+ return list;
+}
+
/* Remove the specified node from the specified list.
* It's up to the caller to free the private value of the node.
*
}
/* Create an iterator in the list private iterator structure */
-void listRewind(list *list) {
- list->iter.next = list->head;
- list->iter.direction = AL_START_HEAD;
+void listRewind(list *list, listIter *li) {
+ li->next = list->head;
+ li->direction = AL_START_HEAD;
}
-void listRewindTail(list *list) {
- list->iter.next = list->tail;
- list->iter.direction = AL_START_TAIL;
+void listRewindTail(list *list, listIter *li) {
+ li->next = list->tail;
+ li->direction = AL_START_TAIL;
}
/* Return the next element of an iterator.
* or NULL if there are no more elements, so the classical usage patter
* is:
*
- * iter = listGetItarotr(list,<direction>);
- * while ((node = listNextIterator(iter)) != NULL) {
- * DoSomethingWith(listNodeValue(node));
+ * iter = listGetIterator(list,<direction>);
+ * while ((node = listNext(iter)) != NULL) {
+ * doSomethingWith(listNodeValue(node));
* }
*
* */
return current;
}
-/* List Yield just call listNext() against the list private iterator */
-listNode *listYield(list *list) {
- return listNext(&list->iter);
-}
-
/* Duplicate the whole list. On out of memory NULL is returned.
* On success a copy of the original list is returned.
*