Test Foro de elhacker.net SMF 2.1

Programación => Programación C/C++ => Mensaje iniciado por: NOB2014 en 20 Julio 2016, 16:28 PM

Título: segmentation fault al mostrar lista simplemente ligada en lenguaje C.
Publicado por: NOB2014 en 20 Julio 2016, 16:28 PM
Hola, que tengan un muy buen día.
Nuevamente Danielito con algún inconveniente con lista enlazada, lo que ocurre es que si ingreso un dato o más todo bien pero si la lista está vacía me da el error que dejo en la imagen a continuación. -
   
                          (http://i66.tinypic.com/hvtiqr.png)

Encuentro muchos ejs., pero casi todos son sin ingreso por teclado sino con datos ingresados antes de compilar, me parece que tiene que ver conque la lista esta igualada a NULL o sea que no apunta a ninguna dirección en concreto pero no logro dar con la tecla

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

struct nodo{
int dato;
struct nodo *siguiente;
};

struct lista{
struct nodo *primero;
struct nodo *ultimo;
int elementos;
};

void menu( void );
void limpiar( void );
struct lista *agregar( struct lista *L );
struct nodo *crear( int dato );
void mostrar( struct lista *L );

int main( void ){
menu();

return 0;
}


void menu( void ){
struct lista *Lista = NULL;
int opc, ok, ch;


do{
do{
limpiar();
printf( "\n ========== Menu ==========\n" );
printf(" \n 1 - Agregar\n 2 - Buscar\n 3 - Borrar\n 4 - Ordenar " );
printf(" \n 5 - Total\n 6 - Finalizar\n\n Ingrese Opcion....: "  );
ok = scanf( "%d", &opc ) == 1 && opc > 0 && opc <= 6;
while ((ch = getchar()) != EOF && ch != '\n');
}while( !ok );

switch ( opc ){
case 1: Lista = agregar( Lista );
break;
case 2: printf("\n Buscar");
break;
case 3: printf("\n Borrar");
break;
case 4: printf("\n Ordenar");
break;
case 5: mostrar( Lista );
break;
case 6:;
free(Lista);
break;
}
}while( opc != 6 );
}

void limpiar( void ){
system("cls||clear");
}

struct lista *agregar( struct lista *L ){
int ok, ch, dto;

do{
limpiar();
printf( "\n Ingrese dato (mayor a 0 y menor a %d)....: ", INT_MAX );
ok = scanf( "%d", &dto ) == 1 && dto >0 && dto <= INT_MAX;
while ((ch = getchar()) != EOF && ch != '\n');
}while( !ok );

if( L != NULL ){
struct nodo *e = crear( dto );
L->ultimo->siguiente = e;
L->ultimo = e;
L->elementos++;
return L;
}else{
struct nodo *e = crear( dto );
struct lista *l = calloc( sizeof( struct lista ), 1 );
l->primero = e;
l->ultimo = e;
l->elementos = 1;
return l;
}
}

struct nodo *crear( int dato ){
struct nodo *e = calloc( sizeof( struct nodo), 1 );
e->dato = dato;
e->siguiente = NULL;

return e;
}

void mostrar( struct lista *L ){
struct nodo *auxiliar;
int i=0;
auxiliar = L->primero;

while( auxiliar != NULL ){
printf( "\n %d", auxiliar->dato );
auxiliar = auxiliar->siguiente;
i++;
}
if( i == 0 ) printf( "\n La lista esta vacia..." );

printf( "\n Pulse una tecla para continuar..." );getchar();
}


Saludos.
Título: Re: segmentation fault al mostrar lista simplemente ligada en lenguaje C.
Publicado por: AlbertoBSD en 20 Julio 2016, 16:41 PM
Valida que L no sea Null y procede pero si es NULL manda error de lista vacia
Título: Re: segmentation fault al mostrar lista simplemente ligada en lenguaje C.
Publicado por: NOB2014 en 20 Julio 2016, 17:03 PM
Hola.
Gracias, ya lo logre, juraría que en un momento lo intente de la manera que tú dices, pero algún otro error seguramente estaba cometiendo.

void mostrar( struct lista *L ){
struct nodo *auxiliar;
int i=0;

if( L != NULL ){
auxiliar = L->primero;
while( auxiliar != NULL ){
printf( "\n %d", auxiliar->dato );
auxiliar = auxiliar->siguiente;
i++;
}
}else{
printf( "\n La lista esta vacia..." );
}

printf( "\n Pulse una tecla para continuar..." );getchar();
}


Gracias Alberto y un abrazo.