list.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // 双向链表结构
  2. #ifndef __LIB_KERNEL_LIST_H
  3. #define __LIB_KERNEL_LIST_H
  4. #include "../stdint.h"
  5. #define offset(struct_type, member) (int)(&((struct_type *)0)->member)
  6. #define node2entry(struct_type, struct_member_name, node_ptr) \
  7. (struct_type *)((int)node_ptr - offset(struct_type, struct_member_name))
  8. // 链表节点, 节点中不需要数据成员, 只要求前驱和后继指针
  9. struct list_elem
  10. {
  11. struct list_elem *prev; // 前驱节点
  12. struct list_elem *next; // 后继节点
  13. };
  14. // 链表结构, 用来实现队列
  15. struct list
  16. {
  17. struct list_elem head; // 链表头, 链表起始元素, 不是第一个元素
  18. struct list_elem tail; // 链表尾, 链表终止元素, 不是最后一个元素
  19. };
  20. // 自定义函数类型 trav_func, 用于在list_traversal中做回调函数
  21. typedef bool trav_func(struct list_elem *, int arg);
  22. /****************************** function声明 ******************************/
  23. // 初始化双向链表list
  24. void list_init(struct list *);
  25. // 将链表元素elem 插入在元素before之前
  26. void list_insert_before(struct list_elem *before, struct list_elem *elem);
  27. // 将元素elem添加到链表plist队首,类似栈push操作
  28. void list_push(struct list *plist, struct list_elem *elem);
  29. void list_iterate(struct list *plist);
  30. void list_append(struct list *plist, struct list_elem *elem);
  31. // 从链表中删除元素pelem
  32. void list_remove(struct list_elem *pelem);
  33. // 将链表第一个元素弹出并返回
  34. struct list_elem *list_pop(struct list *plist);
  35. // 判断链表是否为空,空返回true,否则返回false
  36. bool list_empty(struct list *plist);
  37. // 返回链表长度
  38. uint32_t list_len(struct list *plist);
  39. // 把列表plist中的每个元素elem和arg传给func,
  40. // func 判断是否符合条件
  41. // 如果找到符合条件的元素返回元素指针,否则返回NULL
  42. struct list_elem *list_traversal(struct list *plist, trav_func func, int arg);
  43. // 查找元素是否在链表中,成功返回true,失败返回false
  44. bool elem_find(struct list *plist, struct list_elem *obj_elem);
  45. /****************************** function声明 END ******************************/
  46. #endif // __LIB_KERNEL_LIST_H