From fb8b51cb676f5e2edda44a36e333b1cddbb77fab Mon Sep 17 00:00:00 2001 From: Jakob Olsson <jakobols@kth.se> Date: Tue, 29 May 2018 16:42:00 +0200 Subject: [PATCH] preparation for dfs algorithm --- dfs.c | 49 +++++++++++++++++++++++++++++++++++++++++ dfs.h | 15 +++++++++++++ dongle_infrastructure.c | 3 +++ 3 files changed, 67 insertions(+) create mode 100644 dfs.c create mode 100644 dfs.h diff --git a/dfs.c b/dfs.c new file mode 100644 index 0000000..d1e7741 --- /dev/null +++ b/dfs.c @@ -0,0 +1,49 @@ +/* +DFS(G,v) ( v is the vertex where the search starts ) + Stack S := {}; ( start with an empty stack ) + for each vertex u, set visited[u] := false; + push S, v; + while (S is not empty) do + u := pop S; + if (not visited[u]) then + visited[u] := true; + for each unvisited neighbour w of u + push S, w; + end if + end while + END DFS() +*/ +#include "dfs.h" + +void stack_enqueue(struct node *n, struct list_head *stack) +{ + if (list_empty(stack)) + INIT_LIST_HEAD(stack); + list_add(&node->list, stack); +} + +struct node *stack_dequeue(struct list_head *stack) +{ + struct node *n; + + return list_first_entry(stack, struct node, list); +} + +void add_visited(struct node *n, struct list_head *visited) +{ + if (list_empty(visited)) + INIT_LIST_HEAD(visited); + list_add(&node->list, visited); +} + +bool is_visited(struct node *n, struct list_head *visited) +{ + struct node *tmp; + + list_for_each_entry(tmp, visited, list) { + if (strncmp(tmp->path, n->path, 1024) == 0) + return true; + } + + return false; +} \ No newline at end of file diff --git a/dfs.h b/dfs.h new file mode 100644 index 0000000..4666f5e --- /dev/null +++ b/dfs.h @@ -0,0 +1,15 @@ +#ifndef DFS_H +#define DFS_H +#include "common.h" + +struct node { + struct list_head list; + char *path; + char *name; +}; + +void enqueue_stack(struct node *n, struct list_head *stack); +struct node *dequeue_stack(struct list_head *stack); +void add_visited(struct node *n, struct list_head *visited); +bool is_visited(struct node *n, struct list_head *visited); +#endif \ No newline at end of file diff --git a/dongle_infrastructure.c b/dongle_infrastructure.c index 4f8a4f0..db55b20 100644 --- a/dongle_infrastructure.c +++ b/dongle_infrastructure.c @@ -17,6 +17,7 @@ #include "dongle_infrastructure.h" #include "dongle.h" +#include "dfs.h" enum { DEV, @@ -28,6 +29,8 @@ const struct blobmsg_policy dev_policy[__DEV_MAX] = { }; LIST_HEAD(devices); +LIST_HEAD(visited); +LIST_HEAD(unvisited); int devices_status(struct uloop_timeout *t) { -- GitLab