mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-05-17 19:41:36 +08:00
vertex的添加代码实现并调试通过
This commit is contained in:
parent
46f1f365fb
commit
163f3c55da
@ -20,18 +20,18 @@ struct _graph_edge
|
|||||||
void *target;
|
void *target;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _graph_vertex
|
struct _graph_node
|
||||||
{
|
{
|
||||||
void* obj;
|
void* obj;
|
||||||
struct _graph_vertex* next;
|
struct _graph_node* next;
|
||||||
struct _graph_edge* edges;
|
struct _graph_edge* edge;
|
||||||
bool visited;
|
bool visited;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _graph
|
struct _graph
|
||||||
{
|
{
|
||||||
// -------------------- private --------------------
|
// -------------------- private --------------------
|
||||||
struct _graph_vertex* _head;
|
struct _graph_node* _head;
|
||||||
|
|
||||||
uint32_t _size;
|
uint32_t _size;
|
||||||
uint32_t _obj_size;
|
uint32_t _obj_size;
|
||||||
@ -70,6 +70,9 @@ struct _graph
|
|||||||
// iter
|
// iter
|
||||||
iterator_t (*iter)(struct _graph* self);
|
iterator_t (*iter)(struct _graph* self);
|
||||||
|
|
||||||
|
// config
|
||||||
|
compare_fun_t compare; // !!! you have to implement this function
|
||||||
|
|
||||||
// others
|
// others
|
||||||
bool (*from_matrix)(struct _graph* self, void* obj, uint32_t* edges, uint32_t size);
|
bool (*from_matrix)(struct _graph* self, void* obj, uint32_t* edges, uint32_t size);
|
||||||
|
|
||||||
|
107
src/graph.c
107
src/graph.c
@ -387,6 +387,14 @@ static void graph_print(struct _graph *self)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("vertex : \n");
|
||||||
|
struct _graph_node * cur = self->_head->next;
|
||||||
|
while(cur != NULL)
|
||||||
|
{
|
||||||
|
self->print_obj(cur->obj);
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
|
||||||
// printf("\n ");
|
// printf("\n ");
|
||||||
// for(uint32_t i = 0; i < self->_capacity; i++)
|
// for(uint32_t i = 0; i < self->_capacity; i++)
|
||||||
// {
|
// {
|
||||||
@ -402,12 +410,103 @@ static void graph_print(struct _graph *self)
|
|||||||
// }
|
// }
|
||||||
// printf("\n");
|
// printf("\n");
|
||||||
// }
|
// }
|
||||||
// printf("\n");
|
|
||||||
|
printf("\n");
|
||||||
printf("print done.\n");
|
printf("print done.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct _graph_node* stack_node_new(void *obj, uint32_t obj_size)
|
||||||
|
{
|
||||||
|
void* new_obj = (void*)calloc(1, obj_size);
|
||||||
|
if (new_obj == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memmove(new_obj, obj, obj_size);
|
||||||
|
|
||||||
|
struct _graph_node* new_node = (struct _graph_node*)malloc(sizeof(struct _graph_node));
|
||||||
|
if (new_node == NULL)
|
||||||
|
{
|
||||||
|
free(new_obj);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
new_node->obj = new_obj;
|
||||||
|
new_node->next = NULL;
|
||||||
|
new_node->edge = NULL;
|
||||||
|
new_node->visited = false;
|
||||||
|
|
||||||
|
return new_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void stack_node_free(struct _graph_node** node)
|
||||||
|
{
|
||||||
|
if(node != NULL && *node != NULL)
|
||||||
|
{
|
||||||
|
if((*node)->obj != NULL)
|
||||||
|
{
|
||||||
|
free((*node)->obj);
|
||||||
|
(*node)->obj = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if((*node)->edge != NULL)
|
||||||
|
{
|
||||||
|
free((*node)->edge);
|
||||||
|
(*node)->edge = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(*node);
|
||||||
|
|
||||||
|
*node = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static bool graph_add_vertex(struct _graph* self, void* obj)
|
static bool graph_add_vertex(struct _graph* self, void* obj)
|
||||||
{
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
if(self->_size >= self->_capacity)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(self->_head->next == NULL)
|
||||||
|
{
|
||||||
|
// no vertex
|
||||||
|
struct _graph_node * new_node = stack_node_new(obj, self->_obj_size);
|
||||||
|
if(new_node == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
self->_head->next = new_node;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
struct _graph_node * cur = self->_head->next;
|
||||||
|
|
||||||
|
// find if exist
|
||||||
|
while(cur != NULL)
|
||||||
|
{
|
||||||
|
if(self->compare((char *)cur->obj, (char *)obj) == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct _graph_node * new_node = stack_node_new(obj, self->_obj_size);
|
||||||
|
if(new_node == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// add to tail
|
||||||
|
cur = self->_head->next;
|
||||||
|
while(cur->next != NULL)
|
||||||
|
{
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
cur->next = new_node;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -450,14 +549,14 @@ static bool graph_init(struct _graph *self, uint32_t obj_size)
|
|||||||
self->_capacity = UINT32_MAX;
|
self->_capacity = UINT32_MAX;
|
||||||
self->_ratio = 1;
|
self->_ratio = 1;
|
||||||
|
|
||||||
self->_head = (struct _graph_vertex *)malloc(sizeof(struct _graph_vertex));
|
self->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node));
|
||||||
if(self->_head == NULL)
|
if(self->_head == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
self->_head->visited = false;
|
self->_head->visited = false;
|
||||||
self->_head->obj = NULL;
|
self->_head->obj = NULL;
|
||||||
self->_head->edges = NULL;
|
self->_head->edge = NULL;
|
||||||
self->_head->next = NULL;
|
self->_head->next = NULL;
|
||||||
|
|
||||||
self->_destory = graph_destory;
|
self->_destory = graph_destory;
|
||||||
@ -482,6 +581,8 @@ static bool graph_init(struct _graph *self, uint32_t obj_size)
|
|||||||
self->bfs = NULL;
|
self->bfs = NULL;
|
||||||
self->dfs = NULL;
|
self->dfs = NULL;
|
||||||
|
|
||||||
|
self->compare = NULL;
|
||||||
|
|
||||||
// -------------------- debug --------------------
|
// -------------------- debug --------------------
|
||||||
self->print_obj = NULL;
|
self->print_obj = NULL;
|
||||||
self->print = graph_print;
|
self->print = graph_print;
|
||||||
|
@ -10,15 +10,38 @@
|
|||||||
*/
|
*/
|
||||||
#include "test.h"
|
#include "test.h"
|
||||||
|
|
||||||
#if 0
|
|
||||||
void test_graph_new(void)
|
void test_graph_new(void)
|
||||||
{
|
{
|
||||||
graph_t graph = graph_new2(sizeof(int), 10);
|
graph_t graph = graph_new(sizeof(int));
|
||||||
TEST_ASSERT_NOT_NULL(graph);
|
TEST_ASSERT_NOT_NULL(graph);
|
||||||
graph_free(&graph);
|
graph_free(&graph);
|
||||||
TEST_ASSERT_NULL(graph);
|
TEST_ASSERT_NULL(graph);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_graph_add_vertex(void)
|
||||||
|
{
|
||||||
|
const int size = 10;
|
||||||
|
int data[10] = {
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
};
|
||||||
|
uint32_t i = 0;
|
||||||
|
|
||||||
|
graph_t graph = graph_new(sizeof(int));
|
||||||
|
TEST_ASSERT_NOT_NULL(graph);
|
||||||
|
graph->compare = compare_num;
|
||||||
|
graph->print_obj = print_num;
|
||||||
|
|
||||||
|
for(i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
graph->add_vertex(graph, &data[i]);
|
||||||
|
}
|
||||||
|
graph->print(graph);
|
||||||
|
|
||||||
|
graph_free(&graph);
|
||||||
|
TEST_ASSERT_NULL(graph);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
void test_graph_print(void)
|
void test_graph_print(void)
|
||||||
{
|
{
|
||||||
graph_t graph = graph_new2(sizeof(int), 10);
|
graph_t graph = graph_new2(sizeof(int), 10);
|
||||||
@ -63,7 +86,9 @@ void test_graph(void)
|
|||||||
{
|
{
|
||||||
UnitySetTestFile(__FILE__);
|
UnitySetTestFile(__FILE__);
|
||||||
|
|
||||||
// RUN_TEST(test_graph_new);
|
RUN_TEST(test_graph_new);
|
||||||
|
RUN_TEST(test_graph_add_vertex);
|
||||||
|
|
||||||
// RUN_TEST(test_graph_print);
|
// RUN_TEST(test_graph_print);
|
||||||
// RUN_TEST(test_graph_from_matrix);
|
// RUN_TEST(test_graph_from_matrix);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user