Falta fazer a desalocação de memória.
#define false 0
#define true 1
#define bool int
#include <stdlib.h>
#include <stdio.h>
typedef struct node {
int val;
struct node *next;
} node_t;
void push(node_t **head, int val) {
node_t *new_node;
new_node = (node_t *) malloc(sizeof(node_t));
new_node->val = val;
new_node->next = *head;
*head = new_node;
}
int V = 0;
node_t **adj = NULL;
void initGraph() {
adj = (node_t **) malloc(V * sizeof(node_t *));
for (int i = 0; i < V; i++) {
adj[i] = NULL;
}
}
void addEdge(int u, int v) {
push(&adj[u], v);
}
void printAllPathsUtil(int u, int d, bool visited[], int path[], int path_index) {
visited[u] = true;
path[path_index] = u;
path_index++;
if (u == d) {
for (int i = 0; i < path_index; i++)
printf("%d ", path[i]);
printf("\n");
} else {
node_t *it;
for (it = adj[u]; it != NULL; it = it->next)
if (!visited[it->val])
printAllPathsUtil(it->val, d, visited, path, path_index);
}
path_index--;
visited[u] = false;
}
void printAllPaths(int s, int d) {
bool *visited;
visited = (bool *) malloc(V * sizeof(bool));
int *path;
path = (int *) malloc(V * sizeof(int));
int path_index = 0;
for (int i = 0; i < V; i++)
visited[i] = false;
printAllPathsUtil(s, d, visited, path, path_index);
}
int main() {
V = 8;
initGraph();
addEdge(1, 2);
addEdge(1, 3);
addEdge(3, 4);
addEdge(3, 5);
addEdge(4, 6);
addEdge(5, 6);
addEdge(2, 3);
addEdge(2, 4);
addEdge(5, 7);
addEdge(4, 7);
int s = 1, d = 6;
printf("Following are all different paths from %d to %d \n", s, d);
printAllPaths(s, d);
return 0;
}