diff --git a/lib_lista.c b/lib_lista.c index 6063041e3a71233591954cee7465844c129ee472..7da6c0b8eb0988c2f55413f86e77274dc267bc08 100644 --- a/lib_lista.c +++ b/lib_lista.c @@ -132,10 +132,6 @@ int insere_ordenado_lista(int item, t_lista *l) return 1; } -/* - Remove o primeiro elemento da lista e o retorna em *item. - Retorna 1 se a operação foi bem sucedida e zero caso contrário. -*/ int remove_inicio_lista(int *item, t_lista *l) { if (lista_vazia(l)) @@ -144,10 +140,11 @@ int remove_inicio_lista(int *item, t_lista *l) return 0; } - t_nodo *p = l->ini; + t_nodo *p = l->ini->prox; - l->ini = p->prox; - p->prox->prev = l->ini; + p->prev->prox = p->prox; + p->prox->prev = p->prev; + *item = p->chave; free (p); @@ -156,11 +153,26 @@ int remove_inicio_lista(int *item, t_lista *l) return 1; } -/* - Remove o último elemento da lista e o retorna em *item. - Retorna 1 se a operação foi bem sucedida e zero caso contrário. -*/ -int remove_fim_lista(int *item, t_lista *l){return 1;} +int remove_fim_lista(int *item, t_lista *l) +{ + if (lista_vazia(l)) + { + item = NULL; + return 0; + } + + t_nodo *p = l->fim->prev; + + p->prev->prox = p->prox; + p->prox->prev = p->prev; + *item = p->chave; + + free (p); + + l->tamanho--; + + return 1; +} /* Se o elemento chave existir na lista, o retorna em *item. diff --git a/main.c b/main.c index 823a6dbb500bd9b9e335142b6ff591f4c6311f0a..746655df85c78ea8210c45f2bc7f9f877b7cf948 100644 --- a/main.c +++ b/main.c @@ -4,14 +4,17 @@ void imprime (t_lista *l) { - t_nodo *p = l->ini->prox; - - while (p->prox->prox != NULL) + if (!lista_vazia(l)) { - printf ("%d ",p->chave); - p = p->prox; + t_nodo *p = l->ini->prox; + + while (p->prox->prox != NULL) + { + printf ("%d ",p->chave); + p = p->prox; + } + printf ("%d\n",p->chave); } - printf ("%d\n",p->chave); } int main () @@ -26,7 +29,8 @@ int main () insere_ordenado_lista(3, &l); insere_ordenado_lista(2, &l); imprime (&l); - remove_inicio_lista(&item, &l); + remove_fim_lista(&item, &l); + remove_fim_lista(&item, &l); imprime (&l); return 0;