Skip to content

Commit

Permalink
add graph_destory
Browse files Browse the repository at this point in the history
  • Loading branch information
weijieblog committed May 24, 2016
1 parent 872addd commit 7d63604
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
int ecount;
int (*match)(const void *key1, const void *key2);
void (*destory)(void *data);
List Adjlists;
List adjlists;
} Graph;

typedef enum _VertexColor_ {white, gray, black} VertexColor;
Expand Down
18 changes: 18 additions & 0 deletions graph/graph_destory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "graph.h"

void graph_destory(Graph *graph) {
AdjList *adjlist;

while (list_size(&graph->adjlists) > 0) {
if (list_remove(&graph->adjlists, list_head(&graph->adjlists), (void **)&adjlist) == 0) {
set_destory(&adjlist->adjacent);
if (graph != NULL)
graph->destory(adjlist->vertex);
free(adjlist);
}
}

list_destory(&graph->adjlists);
memset(graph, 0, sizeof(Graph));
return;
}
2 changes: 1 addition & 1 deletion graph/graph_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ void graph_init(Graph *graph, int (*match)(const void *key1, const void *key2),
graph->ecount = 0;
graph->match = match;
graph->destory = destory;
list_init(&graph->Adjlists, NULL, match);
list_init(&graph->adjlists, NULL, match);
}
5 changes: 5 additions & 0 deletions graph/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ int main (int argc, char **argv) {
printf("'graph_init' is pass ? %d \n", testGraph1->vcount == 0 &&
testGraph1->ecount == 0 &&
testGraph1->destory == NULL);

graph_destory(testGraph1);
printf("'graph_destory' is pass ? %d \n", testGraph1->vcount == 0 &&
testGraph1->ecount == 0 &&
testGraph1->destory == 0);
return 0;
}
22 changes: 11 additions & 11 deletions graph/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ listsObjects = ../list/list_init.o \
../list/list_pop.o \
../list/list_shift.o

setObjects = ../set/set_init.o \
../set/set_destory.o \
../set/set_difference.o \
../set/set_insert.o \
../set/set_intersection.o \
../set/set_is_equal.o \
../set/set_is_member.o \
../set/set_is_subset.o \
../set/set_remove.o \
../set/set_union.o
setObjects = ../set/set_init.o \
../set/set_destory.o \
../set/set_difference.o \
../set/set_insert.o \
../set/set_intersection.o \
../set/set_is_equal.o \
../set/set_is_member.o \
../set/set_is_subset.o \
../set/set_remove.o \
../set/set_union.o

graphObjects = main.o graph_init.o
graphObjects = main.o graph_init.o graph_destory.o

objects = $(listsObjects) $(setObjects) $(graphObjects)

Expand Down

0 comments on commit 7d63604

Please sign in to comment.