From 2ceb5fddbb6f35645cdbb9b45ce7c57ba69aaf92 Mon Sep 17 00:00:00 2001 From: Leonardo Krambeck <lk19@inf.ufpr.br> Date: Thu, 24 Oct 2019 21:35:15 -0300 Subject: [PATCH] arruma remove inicio e implementa remove fim --- lib_lista.c | 36 ++++++++++++++++++++++++------------ main.c | 18 +++++++++++------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/lib_lista.c b/lib_lista.c index 6063041..7da6c0b 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 823a6db..746655d 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; -- GitLab