From f43f8c2d1898416f33303e5e133a8efd69cbbeea Mon Sep 17 00:00:00 2001
From: Leonardo Krambeck <lk19@inf.ufpr.br>
Date: Thu, 24 Oct 2019 16:49:19 -0300
Subject: [PATCH] implementa destroi e insere inicio

---
 lib_lista.c | 60 +++++++++++++++++++++++++++++++++++++++++------------
 main.c      |  4 +++-
 2 files changed, 50 insertions(+), 14 deletions(-)

diff --git a/lib_lista.c b/lib_lista.c
index ec5516e..05e3ee2 100644
--- a/lib_lista.c
+++ b/lib_lista.c
@@ -3,10 +3,6 @@
 
 #include "lib_lista.h"
 
-/*
-  Cria uma lista vazia. Ela eh duplamente encadeada e tem sentinelas no
-  inicio e no final. Tambem tem um apontador para um elemento qualquer.
-*/
 int inicializa_lista(t_lista *l)
 {
 	t_nodo *sentinel;
@@ -35,21 +31,59 @@ int inicializa_lista(t_lista *l)
 	return 1;
 }
 
-/*
-  Retorna 1 se a lista está vazia e zero caso contrário.
-*/
-int lista_vazia(t_lista *l){return 1;}
+int lista_vazia(t_lista *l)
+{
+	if (l->tamanho == 0)
+		return 1;
 
-/*
-  Remove todos os elementos da lista e faz com que ela aponte para NULL.
-*/
-void destroi_lista(t_lista *l){}
+	return 0;
+}
+
+void destroi_lista(t_lista *l)
+{
+	if (l->ini == NULL && l->fim == NULL && l->tamanho == 0)
+	{
+		printf ("This list is already destroyed.\n");
+		return;
+	}
+
+	t_nodo *p;
+
+	while (l->ini != NULL)
+	{
+		p = l->ini;
+		l->ini = p->prox;
+		free (p);
+	}
+
+	l->fim = NULL;
+	l->tamanho = 0;
+}
 
 /*
   Insere o elemento item no início da lista.
   Retorna 1 se a operação foi bem sucedida e zero caso contrário.
 */
-int insere_inicio_lista(int item, t_lista *l){return 1;}
+int insere_inicio_lista(int item, t_lista *l)
+{
+	t_nodo *new;
+	
+	new = (t_nodo*) malloc (sizeof (t_nodo));
+	if (new == NULL)
+		return 0;
+
+	new->chave = item;
+
+	new->prox = l->ini->prox;
+	new->prev = l->ini;
+
+	l->ini->prox->prev = new;
+	l->ini->prox = new;
+
+	l->tamanho++;	
+
+	return 1;
+}
 
 /*
   Retorna o tamanho da lista em *tam.
diff --git a/main.c b/main.c
index 04fc9b0..e073f7c 100644
--- a/main.c
+++ b/main.c
@@ -6,7 +6,9 @@ int main ()
 {
 	t_lista l;
 
-	if (inicializa_lista(&l)) printf ("inicializou\n");
+	inicializa_lista(&l);
+	insere_inicio_lista (10, &l);
+	if (!lista_vazia(&l)) printf("nao vazia\n");
 
 	return 0;
 }
-- 
GitLab