修改头文件的核心操作和基础操作顺序,方便一眼看出

This commit is contained in:
建峰 2025-04-27 02:29:09 +08:00
parent 4c6387427b
commit 313406970c
5 changed files with 53 additions and 41 deletions

View File

@ -30,10 +30,18 @@
## 接口函数原型
```c
// 对外接口
// -------------------- 初始化 --------------------
struct* new(...); // 创建
void free(struct**); // 释放
// 内部接口
// init 初始化 <构造函数>
// destory 销毁 <析构函数>
// 外部实现
int compare(void* obj1, void* obj2);// 比较函数new后立刻配置树、图必须
// -------------------- 核心功能 --------------------
// 核心操作
bool push(const void* obj); // [栈、队列] 入栈/入队
bool push_front(const void* obj); // [双端队列] 头部入队
@ -47,6 +55,10 @@ bool peek(void* obj); // [栈] 查看栈顶元素
bool front(void* obj); // [队列、双端队列] 查看头部元素
bool back(void* obj); // [队列、双端队列] 查看尾部元素
// bool add_(const void* obj); // 添加元素 <顶点>
// bool del_(const void* obj); // 删除元素 <顶点>
// bool find_(const void* obj); // 查找元素 <顶点>
// 基础操作
uint32_t size(); // 获取大小
bool empty(); // 判断是否为空
@ -59,21 +71,21 @@ iterator_t iter(...); // 返回迭代器
bool iter_hasnext(); // 是否有下一个元素
void* iter_next(); // 迭代器下一个元素
// 其他操作
bool append(const void* obj); // 追加元素 <push_back>
bool delete(const void *obj); // 删除元素
// -------------------- 扩展功能 --------------------
// 元素相关操作
bool append(const void* obj); // 追加元素 <push_back> 一般用于list
bool delete(const void *obj); // 删除元素
bool find(const void* obj); // 查找元素
uint32_t count(const void* obj); // 统计元素个数
// 扩展操作 <带索引>
// 索引相关操作
uint32_t index(void *obj); // 获取元素索引
bool insert(uint32_t index, const void* obj); // 插入元素
bool remove(uint32_t index); // 删除元素
// bool erase(uint32_t index); // 删除元素 <删除元素但不释放内存> 暂未考虑实现
// bool erase(uint32_t index); // 删除元素 <删除元素但不释放内存> 暂未考虑实现
bool set(uint32_t index, const void* obj); // 设置元素
bool get(uint32_t index, void* obj); // 获取元素
uint32_t index(void *obj); // 获取元素索引
void* find(const void* obj); // 查找元素
// 外部接口
int compare(void* obj1, void* obj2); // 比较函数,用于排序等操作
```
## 特点

View File

@ -78,12 +78,11 @@ struct _graph
bool (*del_edge)(struct _graph* self, void* from, void* to);
bool (*find_edge)(struct _graph* self, void* from, void* to);
bool (*empty)(struct _graph* self);
bool (*full)(struct _graph* self);
// base
uint32_t(*size)(struct _graph* self);
uint32_t(*capacity)(struct _graph* self);
bool (*empty)(struct _graph* self);
bool (*full)(struct _graph* self);
bool (*clear)(struct _graph* self);
// iter

View File

@ -36,12 +36,12 @@ struct _heap
// -------------------- public --------------------
// kernel
bool (*peek)(struct _heap* self, void* obj);
bool (*push)(struct _heap* self, void* obj);
bool (*pop)(struct _heap* self, void* obj);
bool (*empty)(struct _heap* self);
bool (*peek)(struct _heap* self, void* obj);
// base
bool (*empty)(struct _heap* self);
uint32_t(*size)(struct _heap* self);
bool (*clear)(struct _heap* self);

View File

@ -1,12 +1,12 @@
/**
* @file list.h
* @author wenjf (Orig5826@163.com)
* @brief
* @brief
* @version 0.1
* @date 2024-06-23
*
*
* @copyright Copyright (c) 2024
*
*
*/
#ifndef _LIST_H_
#define _LIST_H_
@ -16,8 +16,8 @@
struct _list
{
// -------------------- private --------------------
void * obj;
// -------------------- private --------------------
void *obj;
uint32_t _obj_size;
uint32_t _size;
@ -27,27 +27,28 @@ struct _list
struct _iterator _iter;
void (*_destory)(struct _list* self);
void (*_destory)(struct _list *self);
// -------------------- public --------------------
// -------------------- public --------------------
// kernel
bool (*append)(struct _list* self, void* obj); // Append object to the end of the list.
bool (*insert)(struct _list* self, int index, void* obj); // Insert object before index.
bool (*pop)(struct _list* self, int index, void* obj); // Remove and return item at index.
int (*index)(struct _list* self, void* obj); // Return first index of obj. Return -1 if the obj is not present.
bool (*remove)(struct _list* self, void *obj); // Remove first occurrence of obj.
bool (*append)(struct _list *self, void *obj); // Append object to the end of the list.
bool (*insert)(struct _list *self, int index, void *obj); // Insert object before index.
bool (*pop)(struct _list *self, int index, void *obj); // Remove and return item at index.
bool (*get)(struct _list* self, int index, void* obj);
bool (*set)(struct _list* self, int index, void* obj);
int (*index)(struct _list *self, void *obj); // Return first index of obj. Return -1 if the obj is not present.
bool (*remove)(struct _list *self, void *obj); // Remove first occurrence of obj.
bool (*get)(struct _list *self, int index, void *obj);
bool (*set)(struct _list *self, int index, void *obj);
// base
uint32_t(*size)(struct _list* self);
bool (*empty)(struct _list* self);
bool (*clear)(struct _list* self);
uint32_t (*size)(struct _list *self);
uint32_t (*capacity)(struct _list *self);
bool (*empty)(struct _list *self);
bool (*clear)(struct _list *self);
// iter
iterator_t (*iter)(struct _list* self);
iterator_t (*iter)(struct _list *self);
// sort
// bool (*reverse)(struct _list* self); // Reverse *IN PLACE*.
@ -59,15 +60,15 @@ struct _list
*/
// bool (*sort)(struct _list* self, uint8_t reserve, int (*compare)(void* obj, void* obj2));
// -------------------- debug --------------------
void (*print)(struct _list* self);
void (*print_obj)(void* obj);
// -------------------- debug --------------------
void (*print)(struct _list *self);
void (*print_obj)(void *obj);
};
typedef struct _list* list_t;
typedef struct _list *list_t;
// create and free list
list_t list_new2(uint32_t obj_size, uint32_t capacity);
void list_free(list_t* list);
void list_free(list_t *list);
#endif // _LIST_H_

View File

@ -43,11 +43,11 @@ struct _stack
bool (*push)(struct _stack* self, void* obj);
bool (*pop)(struct _stack* self, void* obj);
bool (*peek)(struct _stack* self, void* obj);
bool (*empty)(struct _stack* self);
// base
uint32_t (*size)(struct _stack* self);
uint32_t (*capacity)(struct _stack* self);
bool (*empty)(struct _stack* self);
bool (*clear)(struct _stack* self);
// iter