-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.c
59 lines (47 loc) · 1.06 KB
/
serial.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-License-Identifier: BSD-3-Clause
#include <stdio.h>
#include <stdlib.h>
#include "os_graph.h"
static int sum;
static os_graph_t *graph;
static void processNode(unsigned int nodeIdx)
{
os_node_t *node;
node = graph->nodes[nodeIdx];
sum += node->nodeInfo;
for (int i = 0; i < node->cNeighbours; i++)
if (graph->visited[node->neighbours[i]] == 0) {
graph->visited[node->neighbours[i]] = 1;
processNode(node->neighbours[i]);
}
}
static void traverse_graph(void)
{
for (int i = 0; i < graph->nCount; i++) {
if (graph->visited[i] == 0) {
graph->visited[i] = 1;
processNode(i);
}
}
}
int main(int argc, char *argv[])
{
FILE *input_file;
if (argc != 2) {
fprintf(stderr, "Usage: %s input_file\n", argv[0]);
exit(EXIT_FAILURE);
}
input_file = fopen(argv[1], "r");
if (input_file == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
graph = create_graph_from_file(input_file);
if (graph == NULL) {
fprintf(stderr, "[Error] Can't read the graph from file\n");
exit(EXIT_FAILURE);
}
traverse_graph();
printf("%d", sum);
return 0;
}