提交 ebd95907 authored 作者: 周尚's avatar 周尚

v1.0.0

上级
#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;
}
/**
* @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 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论