typedefs
This commit is contained in:
@ -4,21 +4,21 @@
|
||||
|
||||
#include "binarytree.h"
|
||||
|
||||
void bintree_init(struct BinTree *tree, void (*destroy)(void *data)) {
|
||||
void bintree_init(BinTree *tree, void (*destroy)(void *data)) {
|
||||
tree->size = 0;
|
||||
tree->destroy = destroy;
|
||||
tree->compare = NULL;
|
||||
tree->root = NULL;
|
||||
}
|
||||
|
||||
void bintree_destroy(struct BinTree *tree) {
|
||||
void bintree_destroy(BinTree *tree) {
|
||||
bintree_rem_left(tree, NULL);
|
||||
memset(tree, 0, sizeof(struct BinTree));
|
||||
memset(tree, 0, sizeof(BinTree));
|
||||
}
|
||||
|
||||
int bintree_ins_left(struct BinTree *tree, struct BinTreeNode *node, void *data) {
|
||||
struct BinTreeNode *new_node;
|
||||
struct BinTreeNode **pos;
|
||||
int bintree_ins_left(BinTree *tree, BinTreeNode *node, void *data) {
|
||||
BinTreeNode *new_node;
|
||||
BinTreeNode **pos;
|
||||
|
||||
if (node == NULL) {
|
||||
if (tree->size > 0) {
|
||||
@ -32,7 +32,7 @@ int bintree_ins_left(struct BinTree *tree, struct BinTreeNode *node, void *data)
|
||||
pos = &node->left;
|
||||
}
|
||||
|
||||
if ((new_node = malloc(sizeof(struct BinTreeNode))) == NULL) {
|
||||
if ((new_node = malloc(sizeof(BinTreeNode))) == NULL) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@ -45,9 +45,9 @@ int bintree_ins_left(struct BinTree *tree, struct BinTreeNode *node, void *data)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bintree_ins_right(struct BinTree *tree, struct BinTreeNode *node, void *data) {
|
||||
struct BinTreeNode *new_node;
|
||||
struct BinTreeNode **pos;
|
||||
int bintree_ins_right(BinTree *tree, BinTreeNode *node, void *data) {
|
||||
BinTreeNode *new_node;
|
||||
BinTreeNode **pos;
|
||||
|
||||
if (node == NULL) {
|
||||
if (tree->size > 0) {
|
||||
@ -61,7 +61,7 @@ int bintree_ins_right(struct BinTree *tree, struct BinTreeNode *node, void *data
|
||||
pos = &node->right;
|
||||
}
|
||||
|
||||
if ((new_node = malloc(sizeof(struct BinTreeNode))) == NULL) {
|
||||
if ((new_node = malloc(sizeof(BinTreeNode))) == NULL) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@ -74,12 +74,12 @@ int bintree_ins_right(struct BinTree *tree, struct BinTreeNode *node, void *data
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bintree_rem_left(struct BinTree *tree, struct BinTreeNode *node) {
|
||||
void bintree_rem_left(BinTree *tree, BinTreeNode *node) {
|
||||
if (tree->size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct BinTreeNode **pos;
|
||||
BinTreeNode **pos;
|
||||
if (node == NULL) {
|
||||
pos = &tree->root;
|
||||
} else {
|
||||
@ -98,12 +98,12 @@ void bintree_rem_left(struct BinTree *tree, struct BinTreeNode *node) {
|
||||
}
|
||||
}
|
||||
|
||||
void bintree_rem_right(struct BinTree *tree, struct BinTreeNode *node) {
|
||||
void bintree_rem_right(BinTree *tree, BinTreeNode *node) {
|
||||
if (tree->size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
struct BinTreeNode **pos;
|
||||
BinTreeNode **pos;
|
||||
if (node == NULL) {
|
||||
pos = &tree->root;
|
||||
} else {
|
||||
@ -122,7 +122,7 @@ void bintree_rem_right(struct BinTree *tree, struct BinTreeNode *node) {
|
||||
}
|
||||
}
|
||||
|
||||
int bintree_merge(struct BinTree *merge, struct BinTree *left, struct BinTree *right, void *data) {
|
||||
int bintree_merge(BinTree *merge, BinTree *left, BinTree *right, void *data) {
|
||||
bintree_init(merge, left->destroy);
|
||||
if (bintree_ins_left(merge, NULL, data) != 0) {
|
||||
bintree_destroy(merge);
|
||||
@ -140,7 +140,7 @@ int bintree_merge(struct BinTree *merge, struct BinTree *left, struct BinTree *r
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_node(char* prefix, struct BinTreeNode *node, int is_left, void (*pfunc)(void* data)) {
|
||||
void print_node(char* prefix, BinTreeNode *node, int is_left, void (*pfunc)(void* data)) {
|
||||
if (node != NULL) {
|
||||
printf("%s%s", prefix, (is_left ? "├──" : "└──" ));
|
||||
pfunc(node->data);
|
||||
@ -158,6 +158,6 @@ void bintree_debug_pfunc_int(void* data) {
|
||||
printf("%d\n", i);
|
||||
}
|
||||
|
||||
void bintree_debug_print(struct BinTree *tree) {
|
||||
void bintree_debug_print(BinTree *tree) {
|
||||
print_node("", tree->root, 0, bintree_debug_pfunc_int);
|
||||
}
|
||||
|
@ -3,29 +3,29 @@
|
||||
|
||||
#include "linkedlist.h"
|
||||
|
||||
void ll_init(struct List* list, void (*destroy)(void *data)) {
|
||||
void ll_init(List* list, void (*destroy)(void *data)) {
|
||||
list->size = 0;
|
||||
list->destroy = destroy;
|
||||
list->head = NULL;
|
||||
list->tail = NULL;
|
||||
}
|
||||
|
||||
void ll_destroy(struct List* list) {
|
||||
void ll_destroy(List* list) {
|
||||
void* data;
|
||||
while (list->size > 0) {
|
||||
if (ll_remove(list, list->tail, (void**)&data) == 0 && list->destroy != NULL) {
|
||||
list->destroy(data);
|
||||
}
|
||||
}
|
||||
memset(list, 0, sizeof(struct List));
|
||||
memset(list, 0, sizeof(List));
|
||||
}
|
||||
|
||||
int ll_ins_next(struct List* list, struct ListNode* node, const void* data) {
|
||||
struct ListNode* new_node;
|
||||
int ll_ins_next(List* list, ListNode* node, const void* data) {
|
||||
ListNode* new_node;
|
||||
if (node == NULL && list->size != 0) {
|
||||
return -1;
|
||||
}
|
||||
if ((new_node = malloc(sizeof(struct ListNode))) == NULL) {
|
||||
if ((new_node = malloc(sizeof(ListNode))) == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -50,12 +50,12 @@ int ll_ins_next(struct List* list, struct ListNode* node, const void* data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ll_ins_prev(struct List* list, struct ListNode* node, const void* data) {
|
||||
struct ListNode* new_node;
|
||||
int ll_ins_prev(List* list, ListNode* node, const void* data) {
|
||||
ListNode* new_node;
|
||||
if (node == NULL && list->size != 0) {
|
||||
return -1;
|
||||
}
|
||||
if ((new_node = malloc(sizeof(struct ListNode))) == NULL) {
|
||||
if ((new_node = malloc(sizeof(ListNode))) == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -80,7 +80,7 @@ int ll_ins_prev(struct List* list, struct ListNode* node, const void* data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ll_remove(struct List* list, struct ListNode* node, void** data) {
|
||||
int ll_remove(List* list, ListNode* node, void** data) {
|
||||
if (node == NULL || list->size == 0) {
|
||||
return -1;
|
||||
}
|
||||
@ -106,14 +106,14 @@ int ll_remove(struct List* list, struct ListNode* node, void** data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ll_remove_next(struct List* list, struct ListNode* node, void** data) {
|
||||
int ll_remove_next(List* list, ListNode* node, void** data) {
|
||||
if (node->next == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return ll_remove(list, node->next, data);
|
||||
}
|
||||
|
||||
int ll_remove_prev(struct List* list, struct ListNode* node, void** data) {
|
||||
int ll_remove_prev(List* list, ListNode* node, void** data) {
|
||||
if (node->prev == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
32
src/set.c
32
src/set.c
@ -1,24 +1,24 @@
|
||||
#include "set.h"
|
||||
|
||||
void set_init(struct Set* set, int (*match)(const void* a, const void* b),
|
||||
void set_init(Set* set, int (*match)(const void* a, const void* b),
|
||||
void (*destroy)(void* data)) {
|
||||
ll_init(set, destroy);
|
||||
set->match = match;
|
||||
}
|
||||
|
||||
void set_destroy(struct Set* set) {
|
||||
void set_destroy(Set* set) {
|
||||
ll_destroy(set);
|
||||
}
|
||||
|
||||
int set_insert(struct Set* set, const void* data) {
|
||||
int set_insert(Set* set, const void* data) {
|
||||
if (set_is_member(set, data)) {
|
||||
return 1;
|
||||
}
|
||||
return ll_ins_next(set, set->tail, data);
|
||||
}
|
||||
|
||||
int set_remove(struct Set* set, void** data) {
|
||||
struct ListNode* node = NULL;
|
||||
int set_remove(Set* set, void** data) {
|
||||
ListNode* node = NULL;
|
||||
|
||||
for (node = set->head; node != NULL; node = node->next) {
|
||||
if (set->match(*data, node->data)) {
|
||||
@ -32,8 +32,8 @@ int set_remove(struct Set* set, void** data) {
|
||||
return ll_remove_next(set, node, data);
|
||||
}
|
||||
|
||||
int set_union(struct Set* setu, const struct Set* a, const struct Set* b) {
|
||||
struct ListNode* node;
|
||||
int set_union(Set* setu, const Set* a, const Set* b) {
|
||||
ListNode* node;
|
||||
void* data;
|
||||
|
||||
set_init(setu, a->match, NULL);
|
||||
@ -59,8 +59,8 @@ int set_union(struct Set* setu, const struct Set* a, const struct Set* b) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_intersection(struct Set* seti, const struct Set* a, const struct Set* b) {
|
||||
struct ListNode* node;
|
||||
int set_intersection(Set* seti, const Set* a, const Set* b) {
|
||||
ListNode* node;
|
||||
void* data;
|
||||
|
||||
set_init(seti, a->match, NULL);
|
||||
@ -77,8 +77,8 @@ int set_intersection(struct Set* seti, const struct Set* a, const struct Set* b)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_difference(struct Set* setd, const struct Set* a, const struct Set* b) {
|
||||
struct ListNode* node;
|
||||
int set_difference(Set* setd, const Set* a, const Set* b) {
|
||||
ListNode* node;
|
||||
void* data;
|
||||
|
||||
set_init(setd, a->match, NULL);
|
||||
@ -95,8 +95,8 @@ int set_difference(struct Set* setd, const struct Set* a, const struct Set* b) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_is_member(const struct Set* set, const void* data) {
|
||||
for (struct ListNode* node = set->head; node != NULL; node = node->next) {
|
||||
int set_is_member(const Set* set, const void* data) {
|
||||
for (ListNode* node = set->head; node != NULL; node = node->next) {
|
||||
if (set->match(data, node->data)) {
|
||||
return 1;
|
||||
}
|
||||
@ -104,11 +104,11 @@ int set_is_member(const struct Set* set, const void* data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_is_subset(const struct Set* a, const struct Set* b) {
|
||||
int set_is_subset(const Set* a, const Set* b) {
|
||||
if (a->size > b->size) {
|
||||
return 0;
|
||||
}
|
||||
for (struct ListNode* node = a->head; node != NULL; node = node->next) {
|
||||
for (ListNode* node = a->head; node != NULL; node = node->next) {
|
||||
if (!set_is_member(b, node->data)) {
|
||||
return 0;
|
||||
}
|
||||
@ -116,7 +116,7 @@ int set_is_subset(const struct Set* a, const struct Set* b) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int set_is_equal(const struct Set* a, const struct Set* b) {
|
||||
int set_is_equal(const Set* a, const Set* b) {
|
||||
if (a->size == b->size) {
|
||||
return 0;
|
||||
}
|
||||
|
10
src/stack.c
10
src/stack.c
@ -1,14 +1,14 @@
|
||||
#include "stack.h"
|
||||
|
||||
void stack_init(struct Stack* stack, void (*destroy)(void* data)) {
|
||||
void stack_init(Stack* stack, void (*destroy)(void* data)) {
|
||||
ll_init(stack, destroy);
|
||||
}
|
||||
|
||||
void stack_destroy(struct Stack* stack) {
|
||||
void stack_destroy(Stack* stack) {
|
||||
ll_destroy(stack);
|
||||
}
|
||||
|
||||
int stack_push(struct Stack* stack, void *data) {
|
||||
int stack_push(Stack* stack, void *data) {
|
||||
if (stack->size == 0) {
|
||||
return ll_ins_next(stack, NULL, data);
|
||||
} else {
|
||||
@ -16,10 +16,10 @@ int stack_push(struct Stack* stack, void *data) {
|
||||
}
|
||||
}
|
||||
|
||||
void *stack_peek(struct Stack *stack) {
|
||||
void *stack_peek(Stack *stack) {
|
||||
return stack->tail == NULL ? NULL : stack->tail->data;
|
||||
}
|
||||
|
||||
int stack_pop(struct Stack *stack, void **data) {
|
||||
int stack_pop(Stack *stack, void **data) {
|
||||
return ll_remove(stack, stack->tail, data);
|
||||
}
|
||||
|
Reference in New Issue
Block a user