mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-06 00:46:52 +08:00
Compare commits
15 Commits
14d64cc0a2
...
c411ee96eb
Author | SHA1 | Date | |
---|---|---|---|
c411ee96eb | |||
f63aa4db0a | |||
7ad5631aec | |||
c3ed2ab00d | |||
628df4c1a0 | |||
49bc8b0cc4 | |||
6965fa1e25 | |||
0520bb99ae | |||
238c16c3ec | |||
163f3c55da | |||
46f1f365fb | |||
af1a3703ba | |||
b788dd74ee | |||
c661ebaea6 | |||
4fb9ed9d58 |
23
README.md
23
README.md
@ -36,14 +36,23 @@
|
|||||||
|
|
||||||
|
|
||||||
## 性能比较
|
## 性能比较
|
||||||
|项目| 数组| 链表|
|
| 数据结构 | < | 时 |间 | | 复 | 杂 |度 | > | <空间复杂度> |
|
||||||
|---|---|---|
|
|---|---|---|---|---|---|---|---|---|---|
|
||||||
|查找| $O(1)$ | $O(n)$ |
|
|---|(|**平**|**均**|) | (|**最**|**坏**| ) |**最坏**|
|
||||||
|插入| $O(n)$ | $O(1)$ |
|
|---|访问|搜索|插入|删除|访问|搜索|插入|删除|---|
|
||||||
|删除| $O(n)$ | $O(1)$ |
|
| 数组 | $O(1)$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(1)$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$ |
|
||||||
|
| 栈 | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ |
|
||||||
**【A1】** 若链表的操作流程为,先查找元素再删除元素。那么时间复杂度确实是$O(n)$。但是链表的增删优势,在其他应用有体现。比如双向队列,插入和删除效率都为$O(1)$。
|
| 队列 | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ |
|
||||||
|
| 单向链表 | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ |
|
||||||
|
| 双向链表 | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ | $O(n)$ | $O(1)$ | $O(1)$ | $O(n)$ |
|
||||||
|
| 跳表 | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n*log(n))$ |
|
||||||
|
| 哈希表 | $N/A$ | $O(1)$ | $O(1)$ | $O(1)$ | $N/A$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$
|
||||||
|
| 二叉搜索树 | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$ | $O(n)$ |
|
||||||
|
| AVL树 | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(n)$ |
|
||||||
|
| 红黑树 | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(n)$ |
|
||||||
|
| B树 | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(log(n))$ | $O(n)$ |
|
||||||
|
|
||||||
|
**【答疑】** 若链表的操作流程为,先查找元素再删除元素。那么时间复杂度确实是$O(n)$。但是链表的增删优势,在其他应用有体现。比如双向队列,插入和删除效率都为$O(1)$。
|
||||||
|
|
||||||
## 规范
|
## 规范
|
||||||
### 版本说明
|
### 版本说明
|
||||||
|
@ -1,62 +1,90 @@
|
|||||||
/**
|
/**
|
||||||
* @file graph.h
|
* @file graph.h
|
||||||
* @author wenjf (Orig5826@163.com)
|
* @author wenjf (Orig5826@163.com)
|
||||||
* @brief
|
* @brief
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @date 2024-09-03
|
* @date 2024-09-03
|
||||||
*
|
*
|
||||||
* @copyright Copyright (c) 2024
|
* @copyright Copyright (c) 2024
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#ifndef _GRAPH_H
|
#ifndef _GRAPH_H
|
||||||
#define _GRAPH_H
|
#define _GRAPH_H
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
struct _graph_node {
|
struct _graph_edge
|
||||||
void *obj;
|
{
|
||||||
uint32_t **edge;
|
uint32_t weight;
|
||||||
uint8_t *visited;
|
struct _graph_edge* next;
|
||||||
|
void *target;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct _graph {
|
struct _graph_node
|
||||||
struct _graph_node *_head;
|
{
|
||||||
uint32_t **edges;
|
void* obj;
|
||||||
|
struct _graph_node* next;
|
||||||
|
struct _graph_edge* edgehead;
|
||||||
|
bool visited;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct _graph
|
||||||
|
{
|
||||||
|
// -------------------- private --------------------
|
||||||
|
struct _graph_node* _head;
|
||||||
|
|
||||||
uint32_t _size;
|
uint32_t _size;
|
||||||
uint32_t _obj_size;
|
uint32_t _obj_size;
|
||||||
uint32_t _capacity;
|
uint32_t _capacity;
|
||||||
uint32_t _ratio;
|
uint32_t _ratio;
|
||||||
|
|
||||||
// init
|
struct _iterator _iter;
|
||||||
void (*init)(struct _graph *self);
|
|
||||||
bool (*from_matrix)(struct _graph *self, void *obj, uint32_t *edges, uint32_t size);
|
|
||||||
|
|
||||||
// kernel
|
|
||||||
bool (*add)(struct _graph *self, void *obj);
|
|
||||||
bool (*get)(struct _graph *self, uint32_t idx, void *obj);
|
|
||||||
bool (*remove)(struct _graph *self, uint32_t idx);
|
|
||||||
|
|
||||||
|
void (*_destory)(struct _graph* self);
|
||||||
|
|
||||||
|
// -------------------- public --------------------
|
||||||
|
// kernel
|
||||||
|
// -> vertex
|
||||||
|
bool (*add_vertex)(struct _graph* self, void* obj);
|
||||||
|
bool (*del_vertex)(struct _graph* self, void* obj);
|
||||||
|
bool (*find_vertex)(struct _graph* self, void* obj);
|
||||||
|
// -> edge
|
||||||
|
bool (*add_edge)(struct _graph* self, void* from, void* to, uint32_t weight);
|
||||||
|
bool (*del_edge)(struct _graph* self, void* from, void* to);
|
||||||
|
bool (*find_edge)(struct _graph* self, void* from, void* to);
|
||||||
|
|
||||||
|
// traverse
|
||||||
|
bool (*dfs)(struct _graph* self, uint32_t idx);
|
||||||
|
bool (*bfs)(struct _graph* self, uint32_t idx);
|
||||||
|
|
||||||
|
bool (*get)(struct _graph* self, uint32_t idx, void* obj);
|
||||||
|
bool (*remove)(struct _graph* self, uint32_t idx);
|
||||||
|
|
||||||
// base
|
// base
|
||||||
uint32_t (*size)(struct _graph *self);
|
uint32_t(*size)(struct _graph* self);
|
||||||
uint32_t (*capacity)(struct _graph *self);
|
uint32_t(*capacity)(struct _graph* self);
|
||||||
|
bool (*clear)(struct _graph* self);
|
||||||
|
bool (*empty)(struct _graph* self);
|
||||||
|
bool (*full)(struct _graph* self);
|
||||||
|
|
||||||
bool (*empty)(struct _graph *self);
|
// iter
|
||||||
bool (*full)(struct _graph *self);
|
iterator_t (*iter)(struct _graph* self);
|
||||||
|
|
||||||
bool (*dfs)(struct _graph *self, uint32_t idx);
|
// config
|
||||||
bool (*bfs)(struct _graph *self, uint32_t idx);
|
compare_fun_t compare; // !!! you have to implement this function
|
||||||
|
|
||||||
//
|
// others
|
||||||
bool (*clear)(struct _graph *self);
|
bool (*from_matrix)(struct _graph* self, void* obj, uint32_t* edges, uint32_t size);
|
||||||
void (*destory)(struct _graph *self);
|
|
||||||
|
|
||||||
void (*print)(struct _graph *self);
|
// -------------------- debug --------------------
|
||||||
void (*print_obj)(void *obj);
|
void (*print)(struct _graph* self);
|
||||||
|
void (*print_obj)(void* obj);
|
||||||
};
|
};
|
||||||
typedef struct _graph* graph_t;
|
typedef struct _graph* graph_t;
|
||||||
|
|
||||||
|
graph_t graph_new(uint32_t obj_size);
|
||||||
graph_t graph_new2(uint32_t obj_size, uint32_t capacity);
|
graph_t graph_new2(uint32_t obj_size, uint32_t capacity);
|
||||||
void graph_free(graph_t *graph);
|
|
||||||
|
void graph_free(graph_t* graph);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
687
src/graph.c
687
src/graph.c
@ -1,20 +1,21 @@
|
|||||||
/**
|
/**
|
||||||
* @file graph.c
|
* @file graph.c
|
||||||
* @author wenjf (Orig5826@163.com)
|
* @author wenjf (Orig5826@163.com)
|
||||||
* @brief
|
* @brief
|
||||||
* @version 0.1
|
* @version 0.1
|
||||||
* @date 2024-09-03
|
* @date 2024-09-03
|
||||||
*
|
*
|
||||||
* @copyright Copyright (c) 2024
|
* @copyright Copyright (c) 2024
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#include "graph.h"
|
#include "graph.h"
|
||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
|
#if 0
|
||||||
static uint32_t graph_size(struct _graph* self)
|
static uint32_t graph_size(struct _graph* self)
|
||||||
{
|
{
|
||||||
if(self == NULL)
|
if (self == NULL)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -23,22 +24,22 @@ static uint32_t graph_size(struct _graph* self)
|
|||||||
|
|
||||||
static uint32_t graph_capacity(struct _graph* self)
|
static uint32_t graph_capacity(struct _graph* self)
|
||||||
{
|
{
|
||||||
if(self == NULL)
|
if (self == NULL)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return self->_capacity;
|
return self->_capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool graph_clear(struct _graph *self)
|
static bool graph_clear(struct _graph* self)
|
||||||
{
|
{
|
||||||
if(self == NULL && self->_head == NULL)
|
if (self == NULL && self->_head == NULL)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
for(uint32_t i = 0; i < self->_capacity; i++)
|
for (uint32_t i = 0; i < self->_capacity; i++)
|
||||||
{
|
{
|
||||||
for(uint32_t j = 0; j < self->_capacity; j++)
|
for (uint32_t j = 0; j < self->_capacity; j++)
|
||||||
{
|
{
|
||||||
self->_head->edge[i][j] = 0;
|
self->_head->edge[i][j] = 0;
|
||||||
}
|
}
|
||||||
@ -47,30 +48,30 @@ static bool graph_clear(struct _graph *self)
|
|||||||
return self->_size = 0;
|
return self->_size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void graph_destory(struct _graph *self)
|
static void graph_destory(struct _graph* self)
|
||||||
{
|
{
|
||||||
if(self == NULL)
|
if (self == NULL)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self->clear(self);
|
self->clear(self);
|
||||||
if(self->_head != NULL)
|
if (self->_head != NULL)
|
||||||
{
|
{
|
||||||
if(self->_head->visited != NULL)
|
if (self->_head->visited != NULL)
|
||||||
{
|
{
|
||||||
free(self->_head->visited);
|
free(self->_head->visited);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(self->_head->obj != NULL)
|
if (self->_head->obj != NULL)
|
||||||
{
|
{
|
||||||
free(self->_head->obj);
|
free(self->_head->obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(self->_head->edge != NULL)
|
if (self->_head->edge != NULL)
|
||||||
{
|
{
|
||||||
for(uint32_t i = 0; i < self->_capacity; i++)
|
for (uint32_t i = 0; i < self->_capacity; i++)
|
||||||
{
|
{
|
||||||
if(self->_head->edge[i] != NULL)
|
if (self->_head->edge[i] != NULL)
|
||||||
{
|
{
|
||||||
free(self->_head->edge[i]);
|
free(self->_head->edge[i]);
|
||||||
}
|
}
|
||||||
@ -82,23 +83,23 @@ static void graph_destory(struct _graph *self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void graph_print(struct _graph *self)
|
static void graph_print(struct _graph* self)
|
||||||
{
|
{
|
||||||
if(self == NULL || self->_head == NULL || self->print_obj == NULL)
|
if (self == NULL || self->_head == NULL || self->print_obj == NULL)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n ");
|
printf("\n ");
|
||||||
for(uint32_t i = 0; i < self->_capacity; i++)
|
for (uint32_t i = 0; i < self->_capacity; i++)
|
||||||
{
|
{
|
||||||
self->print_obj((char *)self->_head->obj + i * self->_obj_size);
|
self->print_obj((char*)self->_head->obj + i * self->_obj_size);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
for(uint32_t i = 0; i < self->_capacity; i++)
|
for (uint32_t i = 0; i < self->_capacity; i++)
|
||||||
{
|
{
|
||||||
self->print_obj((char *)self->_head->obj + i * self->_obj_size);
|
self->print_obj((char*)self->_head->obj + i * self->_obj_size);
|
||||||
for(uint32_t j = 0; j < self->_capacity; j++)
|
for (uint32_t j = 0; j < self->_capacity; j++)
|
||||||
{
|
{
|
||||||
printf(" %2d ", self->_head->edge[i][j]);
|
printf(" %2d ", self->_head->edge[i][j]);
|
||||||
}
|
}
|
||||||
@ -108,25 +109,25 @@ static void graph_print(struct _graph *self)
|
|||||||
printf("print done.\n");
|
printf("print done.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool graph_from_matrix(struct _graph *self, void *obj, uint32_t *edges, uint32_t size)
|
static bool graph_from_matrix(struct _graph* self, void* obj, uint32_t* edges, uint32_t size)
|
||||||
{
|
{
|
||||||
if(self == NULL || self->_head == NULL)
|
if (self == NULL || self->_head == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(size > self->_capacity || obj == NULL || edges == NULL)
|
if (size > self->_capacity || obj == NULL || edges == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(uint32_t i = 0; i < size; i++)
|
for (uint32_t i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
memmove((char *)self->_head->obj + i * self->_obj_size, (char *)obj + i * self->_obj_size, self->_obj_size);
|
memmove((char*)self->_head->obj + i * self->_obj_size, (char*)obj + i * self->_obj_size, self->_obj_size);
|
||||||
}
|
}
|
||||||
for(uint32_t i = 0; i < size; i++)
|
for (uint32_t i = 0; i < size; i++)
|
||||||
{
|
{
|
||||||
for(uint32_t j = 0; j < size; j++)
|
for (uint32_t j = 0; j < size; j++)
|
||||||
{
|
{
|
||||||
self->_head->edge[i][j] = edges[i * size + j];
|
self->_head->edge[i][j] = edges[i * size + j];
|
||||||
}
|
}
|
||||||
@ -136,19 +137,19 @@ static bool graph_from_matrix(struct _graph *self, void *obj, uint32_t *edges, u
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool graph_bfs(struct _graph *self, uint32_t idx)
|
static bool graph_bfs(struct _graph* self, uint32_t idx)
|
||||||
{
|
{
|
||||||
if(self == NULL || self->_head == NULL)
|
if (self == NULL || self->_head == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(idx >= self->_size || idx >= self->_capacity)
|
if (idx >= self->_size || idx >= self->_capacity)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(uint32_t i = 0; i < self->_size; i++)
|
for (uint32_t i = 0; i < self->_size; i++)
|
||||||
{
|
{
|
||||||
self->_head->visited[i] = 0;
|
self->_head->visited[i] = 0;
|
||||||
}
|
}
|
||||||
@ -161,11 +162,11 @@ static bool graph_bfs(struct _graph *self, uint32_t idx)
|
|||||||
{
|
{
|
||||||
queue->pop(queue, &idx);
|
queue->pop(queue, &idx);
|
||||||
self->_head->visited[idx] = 1;
|
self->_head->visited[idx] = 1;
|
||||||
for(uint32_t i = 0; i < self->_size; i++)
|
for (uint32_t i = 0; i < self->_size; i++)
|
||||||
{
|
{
|
||||||
if(self->_head->edge[idx][i] == 1)
|
if (self->_head->edge[idx][i] == 1)
|
||||||
{
|
{
|
||||||
if(self->_head->visited[i] == 0)
|
if (self->_head->visited[i] == 0)
|
||||||
{
|
{
|
||||||
queue->push(queue, &i);
|
queue->push(queue, &i);
|
||||||
}
|
}
|
||||||
@ -183,124 +184,586 @@ static bool graph_bfs(struct _graph *self, uint32_t idx)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool graph_dfs(struct _graph *self, uint32_t idx)
|
static bool graph_dfs(struct _graph* self, uint32_t idx)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void graph_init2(struct _graph *self)
|
static bool graph_init2(struct _graph* self, uint32_t obj_size, uint32_t capacity)
|
||||||
{
|
{
|
||||||
if(self == NULL || self->_head == NULL)
|
assert(self != NULL);
|
||||||
|
if (self == NULL)
|
||||||
{
|
{
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
uint32_t edges = 0;
|
||||||
|
|
||||||
for(uint32_t i = 0; i < self->_capacity; i++)
|
// -------------------- private --------------------
|
||||||
{
|
self->_size = 0;
|
||||||
*((int *)self->_head->obj + i) = i;
|
self->_obj_size = obj_size;
|
||||||
for(uint32_t j = 0; j < self->_capacity; j++)
|
self->_capacity = capacity;
|
||||||
{
|
self->_ratio = 1;
|
||||||
self->_head->edge[i][j] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
graph_t graph_new2(uint32_t obj_size, uint32_t capacity)
|
self->_head = (struct _graph_node*)malloc(sizeof(struct _graph_node));
|
||||||
{
|
if (self->_head == NULL)
|
||||||
if(obj_size == 0 || capacity == 0)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
graph_t graph = malloc(sizeof(struct _graph));
|
|
||||||
if(graph == NULL)
|
|
||||||
{
|
{
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
graph->_size = 0;
|
self->_head->obj = (void*)malloc(self->_obj_size);
|
||||||
graph->_obj_size = obj_size;
|
if (self->_head->obj == NULL)
|
||||||
graph->_capacity = capacity;
|
|
||||||
graph->_ratio = 1;
|
|
||||||
|
|
||||||
graph->init = graph_init2;
|
|
||||||
graph->destory = graph_destory;
|
|
||||||
|
|
||||||
graph->size = graph_size;
|
|
||||||
graph->capacity = graph_capacity;
|
|
||||||
graph->clear = graph_clear;
|
|
||||||
|
|
||||||
graph->from_matrix = graph_from_matrix;
|
|
||||||
graph->bfs = graph_bfs;
|
|
||||||
graph->dfs = graph_dfs;
|
|
||||||
|
|
||||||
graph->print_obj = NULL;
|
|
||||||
graph->print = graph_print;
|
|
||||||
|
|
||||||
graph->_head = (struct _graph_node *)malloc(sizeof(struct _graph_node));
|
|
||||||
if(graph->_head == NULL)
|
|
||||||
{
|
{
|
||||||
goto done1;
|
goto done1;
|
||||||
}
|
}
|
||||||
|
|
||||||
graph->_head->obj = (void *)malloc(graph->_obj_size);
|
self->_head->edge = (uint32_t**)malloc(self->_capacity * sizeof(uint32_t*));
|
||||||
if(graph->_head->obj == NULL)
|
if (self->_head->edge == NULL)
|
||||||
{
|
{
|
||||||
goto done2;
|
goto done2;
|
||||||
}
|
}
|
||||||
|
|
||||||
graph->_head->edge = (uint32_t **)malloc(graph->_capacity * sizeof(uint32_t *));
|
|
||||||
if(graph->_head->edge == NULL)
|
|
||||||
{
|
|
||||||
goto done3;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t i = 0;
|
uint32_t i = 0;
|
||||||
for(i = 0; i < graph->_capacity; i++)
|
for (i = 0; i < self->_capacity; i++)
|
||||||
{
|
{
|
||||||
graph->_head->edge[i] = (uint32_t *)malloc(graph->_capacity * sizeof(uint32_t));
|
self->_head->edge[i] = (uint32_t*)malloc(self->_capacity * sizeof(uint32_t));
|
||||||
if(graph->_head->edge[i] == NULL)
|
if (self->_head->edge[i] == NULL)
|
||||||
{
|
{
|
||||||
goto done4;
|
edges += 1;
|
||||||
|
goto done3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
graph->_head->visited = (uint8_t *)calloc(1, graph->_capacity * sizeof(uint8_t));
|
self->_head->visited = (uint8_t*)calloc(1, self->_capacity * sizeof(uint8_t));
|
||||||
if(graph->_head->visited == NULL)
|
if (self->_head->visited == NULL)
|
||||||
{
|
{
|
||||||
goto done5;
|
goto done4;
|
||||||
}
|
}
|
||||||
|
|
||||||
// init graph
|
self->_destory = graph_destory;
|
||||||
graph->init(graph);
|
|
||||||
|
|
||||||
return graph;
|
// -------------------- public --------------------
|
||||||
done5:
|
self->size = graph_size;
|
||||||
|
self->capacity = graph_capacity;
|
||||||
|
self->clear = graph_clear;
|
||||||
|
|
||||||
|
self->from_matrix = graph_from_matrix;
|
||||||
|
self->bfs = graph_bfs;
|
||||||
|
self->dfs = graph_dfs;
|
||||||
|
|
||||||
|
// -------------------- debug --------------------
|
||||||
|
self->print_obj = NULL;
|
||||||
|
self->print = graph_print;
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < self->_capacity; i++)
|
||||||
|
{
|
||||||
|
*((int*)self->_head->obj + i) = i;
|
||||||
|
for (uint32_t j = 0; j < self->_capacity; j++)
|
||||||
|
{
|
||||||
|
self->_head->edge[i][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
done4:
|
done4:
|
||||||
for(uint32_t j = 0; j < i; j++)
|
|
||||||
{
|
|
||||||
free(graph->_head->edge[j]);
|
|
||||||
}
|
|
||||||
free(graph->_head->edge);
|
|
||||||
done3:
|
done3:
|
||||||
free(graph->_head->obj);
|
for (uint32_t j = 0; j < edges; j++)
|
||||||
|
{
|
||||||
|
free(self->_head->edge[j]);
|
||||||
|
}
|
||||||
|
free(self->_head->edge);
|
||||||
done2:
|
done2:
|
||||||
free(graph->_head);
|
free(self->_head->obj);
|
||||||
done1:
|
done1:
|
||||||
free(graph);
|
free(self->_head);
|
||||||
done:
|
done:
|
||||||
return NULL;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void graph_free(graph_t *graph)
|
graph_t graph_new2(uint32_t obj_size, uint32_t capacity)
|
||||||
{
|
{
|
||||||
if(graph == NULL || *graph == NULL)
|
graph_t graph = NULL;
|
||||||
|
graph = malloc(sizeof(struct _graph));
|
||||||
|
if (graph == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if(graph_init2(graph, obj_size, capacity) != true)
|
||||||
|
// {
|
||||||
|
// free(graph);
|
||||||
|
// return NULL;
|
||||||
|
// }
|
||||||
|
return graph;
|
||||||
|
}
|
||||||
|
|
||||||
|
void graph_free(graph_t* graph)
|
||||||
|
{
|
||||||
|
if (graph == NULL || *graph == NULL)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
(*graph)->destory(*graph);
|
(*graph)->_destory(*graph);
|
||||||
|
free(*graph);
|
||||||
|
*graph = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static struct _graph_node* graph_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->edgehead = NULL;
|
||||||
|
new_node->visited = false;
|
||||||
|
|
||||||
|
return new_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void greph_node_free(struct _graph_node** node)
|
||||||
|
{
|
||||||
|
if (node != NULL && *node != NULL)
|
||||||
|
{
|
||||||
|
if ((*node)->obj != NULL)
|
||||||
|
{
|
||||||
|
free((*node)->obj);
|
||||||
|
(*node)->obj = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((*node)->edgehead != NULL)
|
||||||
|
{
|
||||||
|
free((*node)->edgehead);
|
||||||
|
(*node)->edgehead = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(*node);
|
||||||
|
|
||||||
|
*node = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct _graph_edge* graph_edge_new(void *target, uint32_t weight)
|
||||||
|
{
|
||||||
|
struct _graph_edge* new_edge = (struct _graph_edge*)malloc(sizeof(struct _graph_edge));
|
||||||
|
if (new_edge == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
new_edge->next = NULL;
|
||||||
|
new_edge->weight = weight;
|
||||||
|
new_edge->target = target;
|
||||||
|
return new_edge;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void greph_edge_free(struct _graph_edge** edge)
|
||||||
|
{
|
||||||
|
if (edge != NULL && *edge != NULL)
|
||||||
|
{
|
||||||
|
free(*edge);
|
||||||
|
*edge = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t graph_size(struct _graph* self)
|
||||||
|
{
|
||||||
|
if (self == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return self->_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool graph_empty(struct _graph* self)
|
||||||
|
{
|
||||||
|
if (self == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return self->size(self) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t graph_capacity(struct _graph* self)
|
||||||
|
{
|
||||||
|
if (self == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return self->_capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool graph_clear(struct _graph* self)
|
||||||
|
{
|
||||||
|
if (self == NULL && self->_head == NULL)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct _graph_node* cur = self->_head->next;
|
||||||
|
struct _graph_node* next = NULL;
|
||||||
|
while (cur != NULL)
|
||||||
|
{
|
||||||
|
next = cur->next;
|
||||||
|
greph_node_free(&cur);
|
||||||
|
cur = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->_head->next = NULL;
|
||||||
|
self->_size = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void graph_destory(struct _graph* self)
|
||||||
|
{
|
||||||
|
if (self == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self->clear(self);
|
||||||
|
if (self->_head != NULL)
|
||||||
|
{
|
||||||
|
free(self->_head);
|
||||||
|
self->_head = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void graph_print(struct _graph* self)
|
||||||
|
{
|
||||||
|
if (self == NULL || self->_head == NULL || self->print_obj == NULL)
|
||||||
|
{
|
||||||
|
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("edge : \n");
|
||||||
|
cur = self->_head->next;
|
||||||
|
while (cur != NULL)
|
||||||
|
{
|
||||||
|
if(cur->edgehead != NULL)
|
||||||
|
{
|
||||||
|
// struct _graph_edge* edge = cur->edgehead->next;
|
||||||
|
struct _graph_edge* edge = cur->edgehead;
|
||||||
|
while (edge != NULL)
|
||||||
|
{
|
||||||
|
struct _graph_node* target = (struct _graph_node*)edge->target;
|
||||||
|
|
||||||
|
printf("from ");
|
||||||
|
self->print_obj(cur->obj);
|
||||||
|
printf(" to ");
|
||||||
|
self->print_obj(target->obj);
|
||||||
|
printf(": %d \n", edge->weight);
|
||||||
|
|
||||||
|
edge = edge->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// self->print_obj(cur->obj);
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
printf("print done.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool graph_add_vertex(struct _graph* self, void* obj)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
if (self->_head->next == NULL)
|
||||||
|
{
|
||||||
|
// no vertex
|
||||||
|
struct _graph_node* new_node = graph_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(cur->obj, obj) == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct _graph_node* new_node = graph_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->_size++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool graph_del_vertex(struct _graph* self, void* obj)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
if (obj == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (self->empty(self))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct _graph_node* cur = self->_head->next;
|
||||||
|
struct _graph_node* pre = self->_head;
|
||||||
|
while (cur != NULL)
|
||||||
|
{
|
||||||
|
if (self->compare(cur->obj, obj) == 0)
|
||||||
|
{
|
||||||
|
pre->next = cur->next;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pre = cur;
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
if (cur == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
greph_node_free(&cur);
|
||||||
|
self->_size--;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool graph_find_vertex(struct _graph* self, void* obj)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
if (obj == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (self->empty(self))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
struct _graph_node* cur = self->_head->next;
|
||||||
|
while (cur != NULL)
|
||||||
|
{
|
||||||
|
if (self->compare(cur->obj, obj) == 0)
|
||||||
|
{
|
||||||
|
// obj is found
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cur == NULL ? false : true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct _graph_node* find_node(struct _graph* self, void* obj)
|
||||||
|
{
|
||||||
|
struct _graph_node* cur = self->_head->next;
|
||||||
|
while (cur != NULL)
|
||||||
|
{
|
||||||
|
if (self->compare(cur->obj, obj) == 0)
|
||||||
|
{
|
||||||
|
// obj is found
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
return cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static bool graph_add_edge(struct _graph* self, void* from, void* to, uint32_t weight)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
if (self->empty(self))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct _graph_node* from_node = self->_head->next;
|
||||||
|
struct _graph_node* to_node = self->_head->next;
|
||||||
|
from_node = find_node(self, from);
|
||||||
|
if (from_node == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
to_node = find_node(self, to);
|
||||||
|
if (to_node == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// printf("print from and to obj start \n");
|
||||||
|
// self->print_obj(from_node->obj);
|
||||||
|
// self->print_obj(to_node->obj);
|
||||||
|
// printf("print from and to obj end \n");
|
||||||
|
|
||||||
|
// from_node add edge
|
||||||
|
struct _graph_edge* new_edge = graph_edge_new(to_node, weight);
|
||||||
|
if (new_edge == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (from_node->edgehead == NULL)
|
||||||
|
{
|
||||||
|
from_node->edgehead = new_edge;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new_edge->next = from_node->edgehead->next;
|
||||||
|
from_node->edgehead->next = new_edge;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if graph is undirected
|
||||||
|
// to_node add edge
|
||||||
|
struct _graph_edge* new_edge2 = graph_edge_new(from_node, weight);
|
||||||
|
if (new_edge2 == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (to_node->edgehead == NULL)
|
||||||
|
{
|
||||||
|
to_node->edgehead = new_edge2;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new_edge2->next = to_node->edgehead->next;
|
||||||
|
to_node->edgehead->next = new_edge2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool graph_del_edge(struct _graph* self, void* from, void* to)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool graph_find_edge(struct _graph* self, void* from, void* to)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool graph_init(struct _graph* self, uint32_t obj_size)
|
||||||
|
{
|
||||||
|
assert(self != NULL);
|
||||||
|
if (self == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------- private --------------------
|
||||||
|
self->_size = 0;
|
||||||
|
self->_obj_size = obj_size;
|
||||||
|
self->_capacity = UINT32_MAX;
|
||||||
|
self->_ratio = 1;
|
||||||
|
|
||||||
|
self->_head = (struct _graph_node*)malloc(sizeof(struct _graph_node));
|
||||||
|
if (self->_head == NULL)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
self->_head->visited = false;
|
||||||
|
self->_head->obj = NULL;
|
||||||
|
self->_head->edgehead = NULL;
|
||||||
|
self->_head->next = NULL;
|
||||||
|
|
||||||
|
self->_destory = graph_destory;
|
||||||
|
|
||||||
|
// -------------------- public --------------------
|
||||||
|
// kernel
|
||||||
|
// -> vertex
|
||||||
|
self->add_vertex = graph_add_vertex;
|
||||||
|
self->del_vertex = graph_del_vertex;
|
||||||
|
self->find_vertex = graph_find_vertex;
|
||||||
|
// -> edge
|
||||||
|
self->add_edge = graph_add_edge;
|
||||||
|
self->del_edge = graph_del_edge;
|
||||||
|
self->find_edge = graph_find_edge;
|
||||||
|
|
||||||
|
// base
|
||||||
|
self->size = graph_size;
|
||||||
|
self->clear = graph_clear;
|
||||||
|
self->empty = graph_empty;
|
||||||
|
self->capacity = graph_capacity;
|
||||||
|
|
||||||
|
self->from_matrix = NULL;
|
||||||
|
self->bfs = NULL;
|
||||||
|
self->dfs = NULL;
|
||||||
|
|
||||||
|
self->compare = NULL;
|
||||||
|
|
||||||
|
// -------------------- debug --------------------
|
||||||
|
self->print_obj = NULL;
|
||||||
|
self->print = graph_print;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
graph_t graph_new(uint32_t obj_size)
|
||||||
|
{
|
||||||
|
graph_t graph = NULL;
|
||||||
|
graph = malloc(sizeof(struct _graph));
|
||||||
|
if (graph == NULL)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (graph_init(graph, obj_size) != true)
|
||||||
|
{
|
||||||
|
free(graph);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return graph;
|
||||||
|
}
|
||||||
|
|
||||||
|
void graph_free(graph_t* graph)
|
||||||
|
{
|
||||||
|
if (graph == NULL || *graph == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
(*graph)->_destory(*graph);
|
||||||
free(*graph);
|
free(*graph);
|
||||||
*graph = NULL;
|
*graph = NULL;
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,98 @@
|
|||||||
|
|
||||||
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,
|
||||||
|
};
|
||||||
|
int temp = 11;
|
||||||
|
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;
|
||||||
|
|
||||||
|
// test add_vertex
|
||||||
|
for(i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(graph->add_vertex(graph, &data[i]));
|
||||||
|
}
|
||||||
|
// graph->print(graph);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(graph->find_vertex(graph, &data[0]));
|
||||||
|
TEST_ASSERT_TRUE(graph->find_vertex(graph, &data[9]));
|
||||||
|
TEST_ASSERT_FALSE(graph->find_vertex(graph, &temp));
|
||||||
|
|
||||||
|
// test del_vertex
|
||||||
|
TEST_ASSERT_TRUE(graph->del_vertex(graph, &data[0]));
|
||||||
|
TEST_ASSERT_FALSE(graph->find_vertex(graph, &data[0]));
|
||||||
|
// graph->print(graph);
|
||||||
|
|
||||||
|
for(i = 1; i < size; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(graph->del_vertex(graph, &data[i]));
|
||||||
|
}
|
||||||
|
TEST_ASSERT_TRUE(graph->empty(graph));
|
||||||
|
|
||||||
|
// test clear
|
||||||
|
for(i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(graph->add_vertex(graph, &data[i]));
|
||||||
|
}
|
||||||
|
// graph->print(graph);
|
||||||
|
|
||||||
|
graph->clear(graph);
|
||||||
|
// graph->print(graph);
|
||||||
|
TEST_ASSERT_TRUE(graph->empty(graph));
|
||||||
|
|
||||||
|
graph_free(&graph);
|
||||||
|
TEST_ASSERT_NULL(graph);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void test_graph_add_edge(void)
|
||||||
|
{
|
||||||
|
const int size = 10;
|
||||||
|
int data[10] = {
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
};
|
||||||
|
int temp = 11;
|
||||||
|
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;
|
||||||
|
|
||||||
|
// test add_vertex
|
||||||
|
for(i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
TEST_ASSERT_TRUE(graph->add_vertex(graph, &data[i]));
|
||||||
|
}
|
||||||
|
graph->print(graph);
|
||||||
|
|
||||||
|
// test add_edge
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[1], 55));
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[0], &data[2], 66));
|
||||||
|
TEST_ASSERT_TRUE(graph->add_edge(graph, &data[1], &data[3], 77));
|
||||||
|
graph->print(graph);
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(graph->add_edge(graph, &temp, &data[1], 0));
|
||||||
|
|
||||||
|
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);
|
||||||
@ -56,12 +142,16 @@ void test_graph_from_matrix(void)
|
|||||||
graph_free(&graph);
|
graph_free(&graph);
|
||||||
TEST_ASSERT_NULL(graph);
|
TEST_ASSERT_NULL(graph);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void test_graph(void)
|
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_add_edge);
|
||||||
|
|
||||||
// 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