linux内核list使用demo(待调试)
1. list.h需要拷贝到/usr/local/include, https://www.cnblogs.com/muahao/p/8109733.html 这里有个list.h可以放到该目录下
2. 结构体中添加list_head成员
//
// Created by gxf on 2020/2/7.
//
#include <string.h>
#include <stdio.h>
#include <list.h>
#include <stdlib.h>
typedef struct {
int age;
struct list_head list;
}person;
int main(int argc, char **argv){
person *tmp, personlist;
INIT_LIST_HEAD(&personlist.list);
// add
for (int i = 0; i < 3; i++) {
tmp = (person*) malloc(sizeof(person));
scanf("%d", &tmp->age);
// list_add(&(tmp->list), &(personlist.list));
list_add_tail(&(tmp->list), &(personlist.list));
}
// travel
list_for_each_entry(tmp, &personlist.list, list)
printf("age:%d\t", tmp->age);
printf("\n");
// update
tmp = (person*) malloc(sizeof(person));
tmp->age = 10;
list_replace(&(personlist.list.next), &(tmp->list));
tmp = NULL;
list_for_each_entry(tmp, &personlist.list, list)
printf("age:%d\t", tmp->age);
printf("\n");
// delete
list_del(personlist.list.next->next);
list_for_each_entry(tmp, &personlist.list,list)
printf("age:%d\t", tmp->age);
return 0;
}
CMakelist.txt
include_directories(/usr/local/include) add_executable(list list-demo.c)
Please call me JiangYouDang!