使用实例
要想使用该实现的双向链表,还需实现自己的查找操作函数。本博文简单实现如下:
struct stu_node {
struct list_head listhead;
int age;
};
//红黑树最大节点数目
#define STU_NUM 20
struct stu_node *stu_search(struct list_head *head, int age) {
struct stu_node *stu = NULL;
struct list_head *cur;
list_for_each(cur, head) {
stu = container_of(cur, struct stu_node, listhead);
if (stu->age == age) {
return stu;
}
}
return NULL;
}
测试用例
void testlist() {
struct list_head head;
struct stu_node *stu = NULL;
struct list_head *cur;
int i = 0;
INIT_LIST_HEAD(&head);
if (1 == list_empty(&head)) {
printf("start, list is empty \n");
} else {
printf("list is not empty,return \n");
return;
}
//insert
printf("start to insert element to the list \n");
for (i = 0; i < STU_NUM; i = i + 2) {
stu = malloc(sizeof(struct stu_node));
stu->age = i + 10;
list_add(&stu->listhead, &head);
}
for (i = 1; i < STU_NUM; i = i + 2) {
stu = malloc(sizeof(struct stu_node));
stu->age = i + 10;
list_add(&stu->listhead, &head);
}
//travel
printf("start to travel the list \n");
list_for_each(cur, &head) {
stu = container_of(cur, struct stu_node, listhead);
printf("age %d \n", stu->age);
}
printf("end to travel the list \n");
//delete
srand(time(NULL));
int key = rand() % STU_NUM;
stu = stu_search(&head, 10 + key);
if (NULL != stu) {
printf("age %d is found, next delete it\n", stu->age);
list_del(&stu->listhead);
free(stu);
} else {
printf("age %d is not found\n", 10 + key);
}
stu = stu_search(&head, 10 + key);
if (NULL != stu) {
printf("delete age %d failed\n", 10 + key);
} else {
printf("delete age %d succeed\n", 10 + key);
}
return;
}
在main函数中,仅仅需要调用该测试接口即可。