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
9c984848
提交
9c984848
authored
5月 10, 2021
作者:
zhoush
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
改为双向链表, 使用哑头
上级
cd16bd7a
流水线
#1899
已失败 于阶段
变更
4
流水线
1
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
153 行增加
和
138 行删除
+153
-138
AWList.c
AWList.c
+89
-123
AWList.h
AWList.h
+15
-15
tests.c
tests.c
+34
-0
valgrind.log
valgrind.log
+15
-0
没有找到文件。
AWList.c
浏览文件 @
9c984848
...
...
@@ -3,146 +3,112 @@
#include <stdio.h>
#include <stdlib.h>
typedef
enum
{
TRUE
=
1
,
FALSE
=
0
}
BOOL
;
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
;
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
)
{
for
(;
list
->
head
;
AWList_delete_node
(
list
,
list
->
head
))
;
if
(
list
->
free_me
==
TRUE
)
{
free
(
list
);
list
=
NULL
;
}
}
void
AWList_destroy
(
AWList
*
list
)
{
AWList_clear
(
list
);
free
(
list
->
head
);
if
(
list
->
free_me
)
{
free
(
list
);
}
}
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_clear
(
AWList
*
list
)
{
if
(
!
list
)
{
return
;
}
for
(;
list
->
tail
!=
list
->
head
;
AWList_delete_node
(
list
,
list
->
tail
))
;
}
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
;
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
;
return
node
;
}
void
AWList_delete_node
(
AWList
*
list
,
AWNode
*
node
)
{
if
(
list
->
head
==
NULL
||
node
==
NULL
||
node
->
delete_func
==
NULL
)
return
;
if
(
list
->
head
==
node
)
{
list
->
curr
=
list
->
head
=
list
->
head
->
next
;
}
else
{
AWNode
*
tmp
=
list
->
head
;
for
(;
tmp
->
next
&&
tmp
->
next
!=
node
;
tmp
=
tmp
->
next
)
{
}
if
(
tmp
->
next
==
node
)
tmp
->
next
=
node
->
next
;
else
return
;
}
node
->
delete_func
(
node
->
data
);
free
(
node
);
node
=
NULL
;
list
->
cnt
-=
1
;
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
);
}
void
AWList_clear
(
AWList
*
list
)
{
int
freeme
=
list
->
free_me
;
list
->
free_me
=
FALSE
;
void
AWList_delete_node
(
AWList
*
list
,
AWNode
*
node
)
{
if
(
!
list
||
!
list
->
head
||
!
node
)
return
;
AWList_destroy
(
list
);
if
(
list
->
tail
==
node
)
{
list
->
tail
=
node
->
prev
;
}
if
(
list
->
iter
==
node
)
{
list
->
iter
=
node
->
prev
;
}
list
->
free_me
=
freeme
;
AWNode_destroy
(
node
);
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
;
}
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
;
node
->
prev
=
dummy
;
node
->
next
=
dummy
->
next
;
dummy
->
next
->
prev
=
node
;
dummy
->
next
=
node
;
return
NULL
;
list
->
iter
=
node
;
list
->
cnt
+=
1
;
}
AWNode
*
AWList_iterator
(
AWList
*
list
)
{
if
(
list
->
curr
)
{
AWNode
*
curr
=
list
->
curr
;
list
->
curr
=
list
->
curr
->
next
;
return
curr
;
}
else
return
NULL
;
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
;
list
->
tail
->
next
=
node
;
list
->
tail
=
node
;
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
;
}
return
NULL
;
}
AWNode
*
AWList_iterator
(
AWList
*
list
)
{
if
(
!
list
||
list
->
iter
==
list
->
tail
->
next
)
return
NULL
;
if
(
list
->
iter
==
list
->
head
)
list
->
iter
=
list
->
head
->
next
;
AWNode
*
iter
=
list
->
iter
;
list
->
iter
=
list
->
iter
->
next
;
return
iter
;
}
AWList.h
浏览文件 @
9c984848
...
...
@@ -16,11 +16,11 @@
* @param data data of node
* @param delete_func function to free data
*/
typedef
struct
_node
{
struct
_node
*
next
;
void
*
data
;
void
(
*
delete_func
)(
void
*
);
typedef
struct
_node
{
struct
_node
*
next
;
struct
_node
*
prev
;
void
*
data
;
void
(
*
delete_func
)(
void
*
);
}
AWNode
;
/**
...
...
@@ -30,13 +30,12 @@ typedef struct _node
* @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
;
typedef
struct
_linked_list
{
AWNode
*
head
;
/* dummy node */
AWNode
*
tail
;
/* last node */
AWNode
*
iter
;
/* iterator */
int
free_me
;
int
cnt
;
}
AWList
;
/**
...
...
@@ -45,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
...
...
@@ -110,7 +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
,
AWNode
*
AWList_find_node
(
AWList
*
list
,
void
*
data
,
int
(
*
cmp_callback
)(
void
*
,
void
*
));
/**
...
...
tests.c
0 → 100644
浏览文件 @
9c984848
#include <stdio.h>
#include <stdlib.h>
#include "AWList.h"
static
inline
void
mfree
(
void
*
arg
)
{
free
(
arg
);
arg
=
NULL
;
}
int
main
()
{
AWList
list
=
{
0
};
AWList_init
(
&
list
);
int
*
num
[
10
]
=
{
NULL
};
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
num
[
i
]
=
calloc
(
1
,
sizeof
(
int
));
*
num
[
i
]
=
i
;
AWList_add_tail
(
&
list
,
num
[
i
],
mfree
);
}
AWNode
*
node
=
NULL
;
for
(
node
=
list
.
head
->
next
;
node
!=
list
.
tail
->
next
;
node
=
node
->
next
)
{
printf
(
"%d "
,
*
(
int
*
)
node
->
data
);
}
printf
(
"
\n
"
);
AWList_clear
(
&
list
);
/* while ((node = AWList_iterator(&list))) { printf("%d ", *(int*)node->data); } */
/* printf("\n"); */
AWList_destroy
(
&
list
);
}
valgrind.log
0 → 100644
浏览文件 @
9c984848
==240== Memcheck, a memory error detector
==240== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==240== Using Valgrind-3.16.0 and LibVEX; rerun with -h for copyright info
==240== Command: ./tests
==240== Parent PID: 84
==240==
==240==
==240== HEAP SUMMARY:
==240== in use at exit: 0 bytes in 0 blocks
==240== total heap usage: 22 allocs, 22 frees, 1,416 bytes allocated
==240==
==240== All heap blocks were freed -- no leaks are possible
==240==
==240== For lists of detected and suppressed errors, rerun with: -s
==240== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论