Skip to content

Commit

Permalink
add graph_insert_edge
Browse files Browse the repository at this point in the history
  • Loading branch information
weijieblog committed May 29, 2016
1 parent 9370721 commit 9073571
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
26 changes: 26 additions & 0 deletions graph/graph_insert_edge.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "graph.h"

int graph_insert_edge(Graph *graph, const void *data1, const void *data2) {
ListElm *element;
int result;

for (element = list_head(&graph->adjlists); element != NULL; element = list_next(element)) {
if (graph->match(data2, ((AdjList *)list_data(element))->vertex))
break;
}
if (element == NULL)
return -1;

for (element = list_head(&graph->adjlists); element != NULL; element = list_next(element)) {
if (graph->match(data1, ((AdjList *)list_data(element))->vertex))
break;
}
if (element == NULL)
return -1;

if ((result = set_insert(&((AdjList *)list_data(element))->adjacent, data2)) != 0)
return result;

graph->ecount++;
return 0;
}
5 changes: 2 additions & 3 deletions graph/graph_insert_vertex.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
int graph_insert_vertex(Graph *graph, const void *data) {
ListElm *element;
AdjList *adjlist;

int result;

for (element = list_head(&graph->adjlists); element != NULL; element = list_next(element)) {
Expand All @@ -17,9 +16,9 @@ int graph_insert_vertex(Graph *graph, const void *data) {
adjlist->vertex = (void *)data;
set_init(&adjlist->adjacent, graph->match, NULL);

if ((result = list_push(&graph->adjlists, adjlist)) != 0) {
if ((result = list_push(&graph->adjlists, adjlist)) != 0)
return result;
}

graph->vcount++;
return 0;
}
17 changes: 13 additions & 4 deletions graph/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ static int _match (const void *key1, const void *key2) {
int main (int argc, char **argv) {
Graph *testGraph1 = (Graph *)malloc(sizeof(Graph));
int testData1 = 0;
void *testPrt = &testData1;
int testData2 = 1;
void *testPtr1;
void *testPtr2;

graph_init(testGraph1, _match, NULL);
printf("'graph_init' is pass ? %d \n", testGraph1->vcount == 0 &&
testGraph1->ecount == 0 &&
testGraph1->destory == NULL);


graph_insert_vertex(testGraph1, testPrt);
printf("'graph_insert_vertex' is pass ? %d \n", testGraph1->vcount == 1 &&
testPtr1 = &testData1;
graph_insert_vertex(testGraph1, testPtr1);
testPtr2 = &testData2;
graph_insert_vertex(testGraph1, testPtr2);
printf("'graph_insert_vertex' is pass ? %d \n", testGraph1->vcount == 2 &&
testGraph1->ecount == 0 &&
testGraph1->destory == NULL);

graph_insert_edge(testGraph1, testPtr1, testPtr2);
printf("'graph_insert_edge' is pass ? %d \n", testGraph1->vcount == 2 &&
testGraph1->ecount == 1);




graph_destory(testGraph1);
Expand Down
2 changes: 1 addition & 1 deletion graph/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ setObjects = ../set/set_init.o \
../set/set_remove.o \
../set/set_union.o

graphObjects = main.o graph_init.o graph_destory.o graph_insert_vertex.o
graphObjects = main.o graph_init.o graph_destory.o graph_insert_vertex.o graph_insert_edge.o

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

Expand Down
2 changes: 1 addition & 1 deletion set/set.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

void set_init(Set *set, int (*match)(const void *key1, const void *key2), void (*destory)(void *data));
void set_destory(Set *set);
int set_insert(Set *set, void *data);
int set_insert(Set *set, const void *data);
int set_remove(Set *set, void **data);
int set_union(Set *setu, const Set *set1, const Set *set2);
int set_intersection(Set *seti, const Set *set1, const Set *set2);
Expand Down
4 changes: 2 additions & 2 deletions set/set_insert.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "set.h"

int set_insert (Set *set, void *data) {
int set_insert (Set *set, const void *data) {
SetElm *new;

if (set_is_member(set, data))
Expand All @@ -9,7 +9,7 @@ int set_insert (Set *set, void *data) {
new = (SetElm *)malloc(sizeof(SetElm));
if (new == NULL)
return -1;
new->data = data;
new->data = (void *)data;
new->next = NULL;
new->prev = NULL;

Expand Down

0 comments on commit 9073571

Please sign in to comment.