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
d1055347
提交
d1055347
authored
6月 26, 2021
作者:
zhoush
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
代码格式化: Linux Kernel
上级
8c5b9efa
流水线
#3106
已失败 于阶段
变更
3
流水线
1
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
142 行增加
和
107 行删除
+142
-107
AWList.c
AWList.c
+92
-68
AWList.h
AWList.h
+20
-21
tests.c
tests.c
+30
-18
没有找到文件。
AWList.c
浏览文件 @
d1055347
...
...
@@ -6,105 +6,129 @@
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
=
calloc
(
1
,
sizeof
(
AWNode
));
list
->
tail
=
list
->
head
;
list
->
iter
=
list
->
head
;
list
->
cnt
=
0
;
return
list
;
AWList
*
AWList_init
(
AWList
*
list
)
{
if
(
list
==
NULL
)
{
list
=
calloc
(
1
,
sizeof
(
AWList
));
list
->
free_me
=
TRUE
;
}
else
{
list
->
free_me
=
FALSE
;
}
list
->
head
=
calloc
(
1
,
sizeof
(
AWNode
));
list
->
tail
=
list
->
head
;
list
->
iter
=
list
->
head
;
list
->
cnt
=
0
;
return
list
;
}
void
AWList_destroy
(
AWList
*
list
)
{
if
(
!
list
)
{
return
;
}
void
AWList_destroy
(
AWList
*
list
)
{
if
(
!
list
)
{
return
;
}
AWList_clear
(
list
);
AWList_clear
(
list
);
free
(
list
->
head
);
if
(
list
->
free_me
)
{
free
(
list
);
}
free
(
list
->
head
);
if
(
list
->
free_me
)
{
free
(
list
);
}
}
void
AWList_clear
(
AWList
*
list
)
{
if
(
!
list
)
{
return
;
}
for
(;
list
->
tail
!=
list
->
head
;
AWList_delete_node
(
list
,
list
->
tail
))
;
void
AWList_clear
(
AWList
*
list
)
{
if
(
!
list
)
{
return
;
}
for
(;
list
->
tail
!=
list
->
head
;
AWList_delete_node
(
list
,
list
->
tail
))
;
}
AWNode
*
AWNode_init
(
void
*
data
,
void
(
*
delete_func
)(
void
*
))
{
AWNode
*
node
=
malloc
(
sizeof
(
AWNode
));
node
->
data
=
data
;
node
->
delete_func
=
delete_func
;
AWNode
*
AWNode_init
(
void
*
data
,
void
(
*
delete_func
)(
void
*
))
{
AWNode
*
node
=
malloc
(
sizeof
(
AWNode
));
node
->
data
=
data
;
node
->
delete_func
=
delete_func
;
node
->
prev
=
NULL
;
node
->
next
=
NULL
;
node
->
prev
=
NULL
;
node
->
next
=
NULL
;
return
node
;
return
node
;
}
void
AWNode_destroy
(
AWNode
*
node
)
{
if
(
node
->
next
)
{
node
->
next
->
prev
=
node
->
prev
;
}
node
->
prev
->
next
=
node
->
next
;
void
AWNode_destroy
(
AWNode
*
node
)
{
if
(
node
->
next
)
{
node
->
next
->
prev
=
node
->
prev
;
}
node
->
prev
->
next
=
node
->
next
;
if
(
node
&&
node
->
delete_func
)
{
node
->
delete_func
(
node
->
data
);
}
free
(
node
);
if
(
node
&&
node
->
delete_func
)
{
node
->
delete_func
(
node
->
data
);
}
free
(
node
);
}
void
AWList_delete_node
(
AWList
*
list
,
AWNode
*
node
)
{
if
(
!
list
||
!
list
->
head
||
!
node
)
return
;
void
AWList_delete_node
(
AWList
*
list
,
AWNode
*
node
)
{
if
(
!
list
||
!
list
->
head
||
!
node
)
return
;
if
(
list
->
tail
==
node
)
{
list
->
tail
=
node
->
prev
;
}
if
(
list
->
iter
==
node
)
{
list
->
iter
=
node
->
prev
;
}
if
(
list
->
tail
==
node
)
{
list
->
tail
=
node
->
prev
;
}
if
(
list
->
iter
==
node
)
{
list
->
iter
=
node
->
prev
;
}
AWNode_destroy
(
node
);
list
->
cnt
-=
1
;
AWNode_destroy
(
node
);
list
->
cnt
-=
1
;
}
void
AWList_add_head
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
))
{
if
(
!
list
||
!
list
->
head
)
return
;
void
AWList_add_head
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
))
{
if
(
!
list
||
!
list
->
head
)
return
;
AWNode
*
node
=
AWNode_init
(
data
,
delete_func
);
AWNode
*
dummy
=
list
->
head
;
AWNode
*
node
=
AWNode_init
(
data
,
delete_func
);
AWNode
*
dummy
=
list
->
head
;
node
->
prev
=
dummy
;
node
->
next
=
dummy
->
next
;
node
->
prev
=
dummy
;
node
->
next
=
dummy
->
next
;
dummy
->
next
->
prev
=
node
;
dummy
->
next
=
node
;
dummy
->
next
->
prev
=
node
;
dummy
->
next
=
node
;
list
->
iter
=
node
;
list
->
cnt
+=
1
;
list
->
iter
=
node
;
list
->
cnt
+=
1
;
}
void
AWList_add_tail
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
))
{
AWNode
*
node
=
AWNode_init
(
data
,
delete_func
);
void
AWList_add_tail
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
))
{
AWNode
*
node
=
AWNode_init
(
data
,
delete_func
);
node
->
prev
=
list
->
tail
;
node
->
next
=
NULL
;
node
->
prev
=
list
->
tail
;
node
->
next
=
NULL
;
list
->
tail
->
next
=
node
;
list
->
tail
=
node
;
list
->
tail
->
next
=
node
;
list
->
tail
=
node
;
list
->
cnt
+=
1
;
list
->
cnt
+=
1
;
}
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
;
}
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
;
return
NULL
;
}
AWNode
*
AWList_iterator
(
AWList
*
list
)
...
...
AWList.h
浏览文件 @
d1055347
...
...
@@ -17,10 +17,10 @@
* @param delete_func function to free data
*/
typedef
struct
_node
{
struct
_node
*
next
;
struct
_node
*
prev
;
void
*
data
;
void
(
*
delete_func
)(
void
*
);
struct
_node
*
next
;
struct
_node
*
prev
;
void
*
data
;
void
(
*
delete_func
)(
void
*
);
}
AWNode
;
/**
...
...
@@ -31,11 +31,11 @@ typedef struct _node {
* @param free_me need to free itself or not
*/
typedef
struct
_linked_list
{
AWNode
*
head
;
/* dummy node */
AWNode
*
tail
;
/* last node */
AWNode
*
iter
;
/* iterator */
int
free_me
;
int
cnt
;
AWNode
*
head
;
/* dummy node */
AWNode
*
tail
;
/* last node */
AWNode
*
iter
;
/* iterator */
int
free_me
;
int
cnt
;
}
AWList
;
/**
...
...
@@ -44,8 +44,8 @@ typedef struct _linked_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)
#define AWList_for_each(pos, list)
\
for (pos = list->head; pos != NULL; pos = pos->next)
/**
* @brief allocate memory for list
...
...
@@ -54,7 +54,7 @@ typedef struct _linked_list {
*
* @return AWList *
*/
AWList
*
AWList_init
(
AWList
*
list
);
AWList
*
AWList_init
(
AWList
*
list
);
/**
* @brief delete the whole list
...
...
@@ -62,12 +62,12 @@ AWList* AWList_init(AWList* list);
* @param list pointer of list
*
*/
void
AWList_destroy
(
AWList
*
list
);
void
AWList_destroy
(
AWList
*
list
);
/**
* @brief free node, but retain list
*/
void
AWList_clear
(
AWList
*
list
);
void
AWList_clear
(
AWList
*
list
);
/**
* @brief delete node
...
...
@@ -76,7 +76,7 @@ void AWList_clear(AWList* list);
* @param node list node pointer
*
*/
void
AWList_delete_node
(
AWList
*
list
,
AWNode
*
node
);
void
AWList_delete_node
(
AWList
*
list
,
AWNode
*
node
);
/**
* @brief insert node to the HEAD of list
...
...
@@ -86,7 +86,7 @@ void AWList_delete_node(AWList* list, AWNode* node);
* @param delete_func function pointer to free data
*
*/
void
AWList_add_head
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
));
void
AWList_add_head
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
));
/**
* @brief append node to tail of the list
...
...
@@ -96,7 +96,7 @@ void AWList_add_head(AWList* list, void* data, void (*delete_func)(void*));
* @param delete_func function pointer to free data
*
*/
void
AWList_add_tail
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
));
void
AWList_add_tail
(
AWList
*
list
,
void
*
data
,
void
(
*
delete_func
)(
void
*
));
/**
* @brief find assigned node via callback function
...
...
@@ -109,9 +109,8 @@ void AWList_add_tail(AWList* list, void* data, void (*delete_func)(void*));
*
* @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
*
));
AWNode
*
AWList_find_node
(
AWList
*
list
,
void
*
data
,
int
(
*
cmp_callback
)(
void
*
,
void
*
));
/**
* @brief list iterator
...
...
@@ -122,6 +121,6 @@ AWNode* AWList_find_node(AWList* list,
*
* @return next node address or NULL
*/
AWNode
*
AWList_iterator
(
AWList
*
list
);
AWNode
*
AWList_iterator
(
AWList
*
list
);
#endif
/* AWLIST_H */
tests.c
浏览文件 @
d1055347
...
...
@@ -8,27 +8,39 @@ static inline void mfree(void* arg) {
arg
=
NULL
;
}
int
main
()
{
AWList
list
=
{
0
}
;
AWList_init
(
&
list
)
;
struct
st
{
char
*
str
;
}
;
int
*
num
[
10
]
=
{
NULL
};
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
num
[
i
]
=
calloc
(
1
,
sizeof
(
int
)
);
*
num
[
i
]
=
i
;
int
main
()
{
AWList
*
list
=
AWList_init
(
NULL
);
AWNode
*
node
=
NULL
;
AWList_add_tail
(
&
list
,
num
[
i
],
mfree
);
}
int
num
[
10
]
=
{
0
};
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
/* num[i] = calloc(1, sizeof(int)); */
num
[
i
]
=
i
;
AWNode
*
node
=
NULL
;
for
(
node
=
list
.
head
->
next
;
node
!=
list
.
tail
->
next
;
node
=
node
->
next
)
{
printf
(
"%d "
,
*
(
int
*
)
node
->
data
);
}
printf
(
"
\n
"
);
AWList_add_tail
(
list
,
&
num
[
i
],
0
);
}
AWList_clear
(
&
list
);
/* while ((node = AWList_iterator(&list))) { printf("%d ", *(int*)node->data); } */
/* printf("\n"); */
while
((
node
=
AWList_iterator
(
list
)))
{
if
(
!
node
)
break
;
if
(
!
node
->
data
)
break
;
AWList_destroy
(
&
list
);
int
n
=
*
(
int
*
)
node
->
data
;
printf
(
"%d "
,
n
);
}
/* printf("\n"); */
/* AWList_clear(list); */
while
((
node
=
AWList_iterator
(
list
)))
{
printf
(
"%d "
,
*
(
int
*
)
node
->
data
);
}
printf
(
"
\n
"
);
AWList_destroy
(
list
);
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论