Ir ao conteúdo
  • Cadastre-se

Listas


Posts recomendados

#include <stdio.h>
#include <ctype.h>
typedef struct nodo {
int inf;
struct nodo * prox;
}NODO;

typedef NODO * LISTA_ENC;
void cria_lista (LISTA_ENC *lista);
int eh_vazia (LISTA_ENC lista);
int tamanho (LISTA_ENC lista);
void inserir (LISTA_ENC *lista, int val, int k);
int recuperar (LISTA_ENC lista, int k);
void retirar (LISTA_ENC *lista, int k);
void imprime_lista(LISTA_ENC lista);
void imprime_inverso_lista(LISTA_ENC lista);
void inserir_ordenado(LISTA_ENC * lista, int valor);
void cria_lista (LISTA_ENC *lista){
*lista=NULL;
}

int eh_vazia (LISTA_ENC lista){

return (lista == NULL);
}


int tamanho (LISTA_ENC lista){
int cont;
for (cont=0; lista!= NULL; cont++)
lista = lista->prox;
return (cont);
}
void inserir (LISTA_ENC *lista, int val, int k){
NODO *novo;

if (k < 1 || k > tamanho(*lista)+1){

printf ("\nERRO! Posição inválida para inserção.\n");
exit (1);
}

novo = (NODO *) malloc (sizeof(NODO));
if (!novo) {
printf ("\nERRO! Memória insuficiente!\n");
exit (2);
}
novo->inf = val;
if (k==1){
novo->prox = *lista;
*lista = novo;
}


else {

LISTA_ENC aux;
for (aux=*lista; k>2; aux=aux->prox, k--);
novo->prox = aux->prox;
aux->prox = novo;
}

}
int recuperar (LISTA_ENC lista, int k){

if (k < 1 || k > tamanho(lista)){
printf ("\nERRO! Consulta invalida.\n");
exit (3);
}

for (;k>1;k--) lista=lista->prox;
return (lista->inf);

}
void retirar (LISTA_ENC *lista, int k) {
NODO *aux;
if (k < 1 || k > tamanho(*lista)) {
printf ("\nERRO! Posição invalida para retirada.\n");
exit (4);
}
if (k==1) {
aux = *lista;
*lista = aux->prox;
free (aux);
} else
{
NODO *aux2;
for (aux=*lista; k>2; k--, aux=aux->prox);
aux2 = aux->prox;
aux->prox = aux2->prox;
free (aux2);
}

}
main (){
	
}

galera professor passou esse codigo com essa lista, so que ele nao mostrou aonde está o erro, e queria saber de alguns de vocês o codigo certou ou aonde ta o erro, se poderem ajudar agradeço bastante, pois lista é chato demais de entender.

Link para o comentário
Compartilhar em outros sites

Eu encontrei cerca de 3 erros que estavam atrapalhando o funcionamento, talvez alguns sejam mais para otimizar, enfim, segue abaixo:

#include <stdio.h>
#include <ctype.h>
#include <winsock.h>
typedef struct nodo {
int inf;
struct nodo * prox;
}NODO;

typedef NODO * LISTA_ENC;

void cria_lista (LISTA_ENC *lista);
int eh_vazia (LISTA_ENC lista);
int tamanho (LISTA_ENC lista);
void inserir (LISTA_ENC *lista, int val, int k);
int recuperar (LISTA_ENC lista, int k);
void retirar (LISTA_ENC *lista, int k);
void imprime_lista(LISTA_ENC lista);
void imprime_inverso_lista(LISTA_ENC lista);
void inserir_ordenado(LISTA_ENC * lista, int valor);

void cria_lista (LISTA_ENC *lista){
    //Aqui tava travando o programa
    lista=(LISTA_ENC *)NULL;
}

int eh_vazia (LISTA_ENC lista){
    return (lista == NULL);
}

int tamanho (LISTA_ENC lista){
    int cont = 0;
    for (cont=0;; cont++){
        //Precisa verificar se a lista
        //inicial é nula - e as restantes
        if(!lista || !lista->prox) break;
        lista = lista->prox;
    }
    return (cont);
}
//Usei o endereco de uma lista em vez de ponteiro
//E troquei os *lista por lista - no caso eram referentes a um ponteiro
void inserir (LISTA_ENC lista, int val, int k){
    NODO *novo;

    if (k < 1 || k > tamanho(lista)+1){
        printf ("\nERRO! Posição inválida para inserção.\n");
        exit (1);
    }

    novo = (NODO *) malloc (sizeof(NODO));
    if (!novo) {
        printf ("\nERRO! Memória insuficiente!\n");
        exit (2);
    }

    novo->inf = val;
    if (k==1){
        novo->prox = lista;
        lista = novo;
    } else {
        LISTA_ENC aux;
        for (aux=lista; k>2; aux=aux->prox, k--);
            novo->prox = aux->prox;
            aux->prox = novo;
        }
}

int recuperar (LISTA_ENC lista, int k){

    if (k < 1 || k > tamanho(lista)){
        printf ("\nERRO! Consulta invalida.\n");
        exit (3);
    }
    for (;k>1;k--)
        lista=lista->prox;
    return
        (lista->inf);
}

void retirar (LISTA_ENC *lista, int k) {
    NODO *aux;
    if (k < 1 || k > tamanho(*lista)) {
        printf ("\nERRO! Posição invalida para retirada.\n");
        exit (4);
    }
    if (k==1) {
        aux = *lista;
        *lista = aux->prox;
        free (aux);
    } else {
        NODO *aux2;
        for (aux=*lista; k>2; k--, aux=aux->prox);
            aux2 = aux->prox;
        aux->prox = aux2->prox;
        free (aux2);
        }
}
main (){
    //Pequeno teste
    LISTA_ENC lista = NULL;
    cria_lista(&lista);
    inserir(lista,1,1);
}

 

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

Ebook grátis: Aprenda a ler resistores e capacitores!

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!