t, 10);
printf(" node 10 was removed, check node 10 again:\n");
bn = list->at(list, 10);
node = NODE_T(bn, struct int_node);
printf(" now node 10 is: %d\n------\n", node->id);
printf("replace test, replace node 12 with id 1200:\n");
bn = list->at(list, 12);
node = NODE_T(bn, struct int_node);
printf(" now node 12 is : %d\n", node->id);
node = (struct int_node*)malloc(sizeof(struct int_node));
node->id = 1200;
list->replace(list, 12, node);
bn = list->at(list, 12);
node = NODE_T(bn, struct int_node);
printf(" replaced, now node 12 is : %d\n----\n", node->id);
printf("test remove:\n");
ST_CALL(list, remove, bn);
bn = ST_CALL(list, find_by_key, (void*)1200);
assert(bn == 0);
printf("test remove ok\n----\n");
printf("test remove_by_key(90):\n");
ST_CALL(list, remove_by_key, (void*)90);
bn = ST_CALL(list, find_by_key, (void*)90);
assert(bn == 0);
printf("test remove_by_key(90) end\n----\n");
printf("test take_at(80):\n");
bn = ST_CALL(list, take_at, 80);
printf(" node 80 is: %d\n", NODE_T(bn, struct int_node)->id);
free(bn);
printf("test take_at(80) end\n");
int_free_flag = 0;
printf("delete list && nodes:\n");
list->deletor(list);
printf("delete list && nodes end\n");
printf("\n test add/insert/remove/delete/find_by_key/replace...\n");
}
void string_slist_test()
{
struct single_list * list = new_single_list(_string_child_free, _string_slist_hittest);
}
void slist_test()
{
int_slist_test();
string_slist_test();
}
测试代码里主要演示了:
自定义链表节点类型定义释放回调定义用于查找的 hit test 回调如何创建链表如何使用( add 、remove 、 take 、find 、 insert 等)
相信到这里,单链表的使用已经不成问题了。
以单链表为基础,可以进一步实现很多数据结构,比如树(兄弟孩子表示法),比如 key-value 链表等等。接下来根据例子的需要,会择机进行展示。
|