list_t *list_dup(list_t *orig) { list_iter *iter = list_get_iterator(orig, LIST_START_HEAD); if (iter == NULL) { return NULL; } list_t *copy = list_create(&orig->type); if (copy == NULL) { list_release_iterator(iter); return NULL; } list_node *node; while ((node = list_next(iter)) != NULL) { void *value = node->value; if (copy->type.dup) { value = copy->type.dup(value); if (value == NULL) { list_release_iterator(iter); list_release(copy); return NULL; } } if (list_add_node_tail(copy, value) == NULL) { list_release_iterator(iter); list_release(copy); return NULL; } } list_release_iterator(iter); return copy; }
int main(int argc, char *argv[]) { list_type type; type.dup = node_dup; type.free = node_free; type.compare = node_compare; list_t *list = list_create(&type); for (int i = 0; i < 10; ++i) { sds value = sdsempty(); value = sdscatprintf(value, "%d", i); list_add_node_tail(list, value); printf("add %s\n", value); sdsfree(value); } for (int i = 0; i < 10; ++i) { sds value = sdsempty(); value = sdscatprintf(value, "%d", 10 + i); list_add_node_head(list, value); printf("add %s\n", value); sdsfree(value); } list_node *node = list_index(list, 10); sds value = sdsempty(); value = sdscatprintf(value, "%d", 100); list_insert_node(list, node, value, 1); printf("insert %s\n", value); node = list_find(list, value); printf("search: %s\n", (char *)node->value); sdsfree(value); for (int i = -10; i < 10; ++i) { node = list_index(list, i); if (node) { printf("%d: %s\n", i, (char *)node->value); } } list_t *copy = list_dup(list); list_release(list); printf("len: %ld\n", list_len(copy)); list_rotate(copy); list_rotate(copy); list_rotate(copy); list_iter *iter = list_get_iterator(copy, LIST_START_HEAD); while ((node = list_next(iter)) != NULL) { printf("%s\n", (char *)node->value); list_del(copy, node); } list_release_iterator(iter); list_release(copy); printf("len: %ld\n", list_len(copy)); printf("head: %p, tail: %p\n", copy->head, copy->tail); return 0; }
//在链表的尾部增加新结点 static void* List_Add_Sprite_Block(__u32 sel, __disp_sprite_block_para_t * para) { list_head_t * node = NULL; node = List_New_Sprite_Block(sel, para); if(node != NULL) { list_add_node_tail(node,&gsprite[sel].header); return node; } return NULL; }