diff --git a/lib_lista.c b/lib_lista.c index ec5516e82afd51509f7bd6b5f086d34bbb7c0590..05e3ee227c88a5d42baacb3573c7f5c79b4019c6 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 04fc9b0b76b225a75c0803e85bd5ec48f0fedf3b..e073f7c333327548ecb91d89b31fcf77b59fb32b 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; }