1

"Crie uma TAD lista não-ordenada implementando a função menor (remove o menor elemento de uma lista) dinâmica/encadeada".

int menor_elem(Lista *lst){
Lista aux = *lst;
if (lista_vazia(*lst) == 1) 
    return 0;

Não sei como eu faço a repetição para achar e salvar o ponteiro anterior do menor elemento!! Pff explique o código pois não estou conseguindo achar uma solução!

EDITADO: Já fiz o .H com o typedef e o usuario.c não interessa para o exercício! eu tentei fazer o menor_elem mas não sei como continua (só preciso fazer a função que retira o menor elemento da lista ).

já criei a lista, já verifiquei se ela esta vazia, já fiz a função que retira e adiciona um elemento e já fiz uma função que mostra o tamanha da lista, só falta a função que retira o menor elemento.

#include <stdio.h>
#include <stdlib.h>
#include "lista.h"

struct no {
int info;
struct no *prox;
};

Lista cria_lista(){
return NULL;
}

int lista_vazia(Lista lst){
if (lst == NULL)
    return 1;
else
    return 0;
}

int insere_elem(Lista *lst, int elem){
Lista N = (Lista) malloc(sizeof(struct no));
if (N == NULL)
    return 0;
N->info = elem;
N->prox = *lst;
*lst = N;
return 1;
}

int remove_elem (Lista *lst, int elem) {
if (lista_vazia(*lst) == 1) {return 0;}
Lista aux = *lst;
if (elem == (*lst)->info) {
    *lst = aux->prox;
    free(aux);
    return 1;
}
while (aux->prox != NULL && aux->prox->info != elem)
    aux = aux->prox;
if (aux->prox == NULL)
    return 0;
Lista aux2 = aux->prox;
aux->prox = aux2->prox;
free(aux2);
return 1;
}

int imprime_lista(Lista lst){
Lista aux = lst;
if (lista_vazia(lst) == 1){
    printf(" -[ ]-\n");
    return 0;
}
printf(" -[");
while (aux!= NULL){
    printf(" %d",aux->info);
    aux = aux->prox;
    }
    printf(" ]- \n");
}

int tamanho_lista(Lista lst){
Lista aux = lst;
if (lista_vazia(lst)==1){
    printf("A lista tem 0 elementos\n");
    return 0;
    }
int x=0;
while(aux!= NULL){
    x++;
    aux = aux->prox;
}
printf("A lista tem %d elementos\n",x);
 }
2
  • Você é obrigado a usar esse pedaço da função? Já escreveu alguma coisa da TAD? se sim gostaria de saber de que maneira você está usando Lista. 20/04/2019 às 2:18
  • O começo da função fui eu que tentei fazer, mas não precisa usar ele. Já editei com o código completo!
    – Arthur
    20/04/2019 às 12:14

1 Resposta 1

1

Consegui !!

int menor_elem(Lista *lst){
if (lista_vazia(*lst) == 1)
    return 0;
Lista aux = *lst;
int x;
x = aux->info;
while (aux->prox != NULL){
    if (aux->info < x)
        x = aux->info;
    aux = aux->prox;
}
remove_elem(lst, x);
return 1;

}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .