-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.c
37 lines (33 loc) · 961 Bytes
/
graph.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "graph.h"
node_p createNode(int ID) {
node_p newNode = (node_p)malloc(sizeof(node_t));
if (!newNode) { err_exit("Unable to allocate memory for new node"); }
newNode->ID = ID;
newNode->Degree = 0;
newNode->Processed = 0;
vector V;
vector_init(&V);
newNode->Vertices = V;
return newNode;
}
void printNode(node_p node) {
printf("ID: %d, Degree: %d, Processed: %d, Vertices: ", node->ID, node->Degree, node->Processed);
for (int i = 0; i < vector_count(&node->Vertices); i++) {
printf("%d ", ((node_p)vector_get(&node->Vertices, i))->ID);
}
printf("\n");
}
void addEdge(node_t *src, node_t *dst) {
dst->Degree++;
vector_add(&src->Vertices, dst);
}
// Remove from src the vertex with ID = dst->ID;
void removeEdge(node_p src, node_t *dst) {
for (int i = 0; i < vector_count(&src->Vertices); i++) {
if (((node_p)vector_get(&src->Vertices, i))->ID == dst->ID) {
dst->Degree--;
vector_delete(&src->Vertices, i);
break;
}
}
}