Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
A
AW-List
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
规范查询wiki:
http://gitlab.anweitech.com/root/AW-Project-Manage/wikis/pages
Open sidebar
周尚
AW-List
Commits
ebd95907
提交
ebd95907
authored
11月 26, 2020
作者:
周尚
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
v1.0.0
上级
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
276 行增加
和
0 行删除
+276
-0
AWList.c
AWList.c
+149
-0
AWList.h
AWList.h
+127
-0
没有找到文件。
AWList.c
0 → 100644
浏览文件 @
ebd95907
#include "AWList.h"
#include <stdio.h>
#include <stdlib.h>
typedef
enum
{
TRUE
=
1
,
FALSE
=
0
}
BOOL
;
// ================> SOURCE CODE BELOW <================
AWList
*
AWList_init
(
AWList
*
list
)
{
if
(
list
==
NULL
)
{
list
=
calloc
(
1
,
sizeof
(
AWList
));
list
->
free_me
=
TRUE
;
}
else
{
list
->
free_me
=
FALSE
;
}
list
->
head
=
NULL
;
list
->
tail
=
NULL
;
list
->
curr
=
NULL
;
list
->
cnt
=
0
;
return
list
;
}
void
AWList_destroy
(
AWList
*
list
)
{
if
(
list
)
{
for
(;
list
->
head
;
AWList_delete_node
(
list
,
list
->
head
))
;
if
(
list
->
free_me
==
TRUE
)
{
free
(
list
);
list
=
NULL
;
}
}
}
void
AWList_add_head
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
))
{
AWNode
*
node
=
(
AWNode
*
)
malloc
(
sizeof
(
AWNode
));
node
->
data
=
data
;
node
->
delete_func
=
delete_func
;
if
(
list
->
head
==
NULL
)
{
node
->
next
=
NULL
;
list
->
tail
=
node
;
}
else
{
node
->
next
=
list
->
head
;
}
list
->
curr
=
list
->
head
=
node
;
list
->
cnt
+=
1
;
}
void
AWList_add_tail
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
))
{
AWNode
*
node
=
(
AWNode
*
)
malloc
(
sizeof
(
AWNode
));
node
->
data
=
data
;
node
->
next
=
NULL
;
node
->
delete_func
=
delete_func
;
if
(
list
->
head
==
NULL
)
{
list
->
curr
=
list
->
head
=
node
;
}
else
{
list
->
tail
->
next
=
node
;
}
list
->
tail
=
node
;
list
->
cnt
+=
1
;
}
void
AWList_delete_node
(
AWList
*
list
,
AWNode
*
node
)
{
if
(
list
->
head
==
node
)
{
list
->
curr
=
list
->
head
=
list
->
head
->
next
;
if
(
list
->
head
==
NULL
)
{
list
->
curr
=
list
->
head
=
list
->
tail
=
NULL
;
return
;
}
}
else
{
AWNode
*
tmp
=
list
->
head
;
for
(;
tmp
->
next
&&
tmp
->
next
!=
node
;
tmp
=
tmp
->
next
)
{
}
tmp
->
next
=
node
->
next
;
}
if
(
node
->
delete_func
)
{
node
->
delete_func
(
node
->
data
);
}
free
(
node
);
node
=
NULL
;
list
->
cnt
-=
1
;
}
void
AWList_clear
(
AWList
*
list
)
{
int
freeme
=
list
->
free_me
;
list
->
free_me
=
FALSE
;
AWList_destroy
(
list
);
list
->
free_me
=
freeme
;
}
AWNode
*
AWList_find_node
(
AWList
*
list
,
void
*
data
,
int
(
*
cmp_callback
)(
void
*
,
void
*
))
{
AWNode
*
node
;
AWList_for_each
(
node
,
list
)
{
if
(
cmp_callback
(
node
->
data
,
data
)
==
0
)
return
node
;
}
return
NULL
;
}
AWNode
*
AWList_iterator
(
AWList
*
list
)
{
if
(
list
->
curr
)
{
AWNode
*
curr
=
list
->
curr
;
list
->
curr
=
list
->
curr
->
next
;
return
curr
;
}
else
return
NULL
;
}
AWList.h
0 → 100644
浏览文件 @
ebd95907
/**
* @file AWList.h
* @date 2020-10-31
* @author whiothes <whiothes81 @gmail.com>
* @version 1.0
* @brief list operations
*/
#ifndef AWLIST_H
#define AWLIST_H
/**
* @brief node of list
*
* @param next pointer to next
* @param data data of node
* @param delete_func function to free data
*/
typedef
struct
_node
{
struct
_node
*
next
;
void
*
data
;
void
(
*
delete_func
)(
void
*
);
}
AWNode
;
/**
* @brief list structure
* @param head head of list
* @param tail tail of list
* @param curr current position of list
* @param free_me need to free itself or not
*/
typedef
struct
_linked_list
{
AWNode
*
head
;
AWNode
*
tail
;
AWNode
*
curr
;
int
free_me
;
int
cnt
;
}
AWList
;
/**
* @brief loop the list
*
* @param pos AWNode *, Iterator variable
* @param list List head
*/
#define AWList_for_each(pos, list) \
for (pos = list->head; pos != NULL; pos = pos->next)
/**
* @brief allocate memory for list
*
* @param list pointer to init, could be NULL
*
* @return AWList *
*/
AWList
*
AWList_init
(
AWList
*
list
);
/**
* @brief delete the whole list
*
* @param list pointer of list
*
*/
void
AWList_destroy
(
AWList
*
list
);
/**
* @brief free node, but retain list
*/
void
AWList_clear
(
AWList
*
list
);
/**
* @brief delete node
*
* @param list pointer of list
* @param node list node pointer
*
*/
void
AWList_delete_node
(
AWList
*
list
,
AWNode
*
node
);
/**
* @brief insert node to the HEAD of list
*
* @param list pointer of list
* @param data data to insert
* @param delete_func function pointer to free data
*
*/
void
AWList_add_head
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
));
/**
* @brief append node to tail of the list
*
* @param list pointer of list
* @param data data to insert
* @param delete_func function pointer to free data
*
*/
void
AWList_add_tail
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
));
/**
* @brief find assigned node via callback function
*
* @details
*
* @param list pointer of list
* @param data key data to find
* @param cmp_callback callback to use data
*
* @return the pointer of the node if exists, NULL if not found
*/
AWNode
*
AWList_find_node
(
AWList
*
list
,
void
*
data
,
int
(
*
cmp_callback
)(
void
*
,
void
*
));
/**
* @brief list iterator
*
* @details always return next node after each call
*
* @param list list pointer
*
* @return next node address or NULL
*/
AWNode
*
AWList_iterator
(
AWList
*
list
);
#endif
/* AWLIST_H */
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论