From 1fc18936db94ae75c8449e18f48a3bf17c19c2e0 Mon Sep 17 00:00:00 2001
From: Vytor Calixto <vytorcalixto@gmail.com>
Date: Thu, 11 May 2017 23:31:59 -0300
Subject: [PATCH] =?UTF-8?q?Add=20fun=C3=A7=C3=B5es=20para=20destruir=20gra?=
 =?UTF-8?q?fo=20e=20v=C3=A9rtices?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 libs/grafo.c   |  8 +++++---
 libs/grafo.h   |  2 +-
 libs/vertice.c | 10 ++++++++++
 libs/vertice.h |  2 ++
 4 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/libs/grafo.c b/libs/grafo.c
index d3f94b4..07774be 100644
--- a/libs/grafo.c
+++ b/libs/grafo.c
@@ -111,7 +111,9 @@ void tabuleiroParaGrafo(Tblr t, Grafo g) {
     return;
 }
 
-int *buscaCaminho(Grafo g) {
-    int *caminho = malloc(sizeof(int));
-    return caminho;
+void destroiGrafo(Grafo g) {
+    destroiLista(g->vertices, destroiVertice);
+    free(g);
+    g = NULL;
+    return;
 }
diff --git a/libs/grafo.h b/libs/grafo.h
index aef7000..cd6a6ab 100644
--- a/libs/grafo.h
+++ b/libs/grafo.h
@@ -16,6 +16,6 @@ void criaArco(Vertice v, Vertice w);
 
 void tabuleiroParaGrafo(Tblr t, Grafo g);
 
-int *buscaCaminho(Grafo g);
+void destroiGrafo(Grafo g);
 
 #endif
diff --git a/libs/vertice.c b/libs/vertice.c
index c70a196..105720b 100644
--- a/libs/vertice.c
+++ b/libs/vertice.c
@@ -11,3 +11,13 @@ Vertice criaVertice() {
     v->filhos = constroiLista();
     return v;
 }
+
+int destroiVertice(void *v) {
+    Vertice w = (Vertice) v;
+    // Como os outros vértices também estão no grafo, deixamos isso a cargo dele
+    destroiLista(w->pais, NULL);
+    destroiLista(w->filhos, NULL);
+    free(w);
+    w = NULL;
+    return 1;
+}
diff --git a/libs/vertice.h b/libs/vertice.h
index fc061ef..c707a56 100644
--- a/libs/vertice.h
+++ b/libs/vertice.h
@@ -13,4 +13,6 @@ typedef struct Vertice *Vertice;
 
 Vertice criaVertice();
 
+int destroiVertice(void *v);
+
 #endif
-- 
GitLab