C/Visual C - Consulta sobre lista enlazada simple

 
Vista:

Consulta sobre lista enlazada simple

Publicado por dedio (1 intervención) el 09/04/2009 04:49:37
Saludos a todos.

Soy de Buenos Aires, Argentina y estoy aprendiendo a crear estructuras dinámicas en c.
Ya he logrado pilas y colas pero tengo problemas con las listas enlazadas ordenadas.
Al ejecutar el código compilado aparece el mensaje "Fallo de segmentación".
Utilicé un depurador y detectó el error en el código, pero no se de que se trata.
He buscado en manuales que tengo, en internet y en esta misma lista y no encontré nada.

Se trata de una lista de enteros ordenada de menor a mayor.
La línea donde el depurador marca el error está indicada con un comentario y se encuentra en la función insertar.

Desde ya, muchas gracias a todos por su tiempo.

/* --------------------------------------------------------------------

* This program is free software; you can redistribute it and/or modify

* it under the terms of the GNU General Public License as published by

* the Free Software Foundation; either version 2 of the License, or

* (at your option) any later version.

*

* This program is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU General Public License for more details.

*

* You should have received a copy of the GNU General Public License

* along with this program; if not, write to the Free Software

* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,

* MA 02110-1301, USA.

* --------------------------------------------------------------------

* Nombre: lista_completa.c

* Copyright:

* Autor:

* Fecha:

* Descripción: Estructura dinámica tipo lista abierta ordenada.

*/


# include <stdio.h>

# include <stdlib.h>


/* Estructura de cada nodo de la lista.

* valor es el dato a almacenar.

* sig es un puntero para enlazar.

*/

typedef struct Listas

{

int valor;

struct Listas *sig;

}listas;


/* Prototipo de funciones.
*/

listas *carga_nodo(void);

void insertar(int dato);

/*void extraer(void);*/ /*** FALTA IMPLEMENTAR ***/

void listar(void);


/* Declaración global de punteros.
*/

listas *lista = NULL, *nodo = NULL;


int main(void)

{

insertar(20);

insertar(10);

insertar(40);

insertar(30);

insertar(50);

insertar(60);


listar();


free(lista);

free(nodo);

}


/* Función para crear nodos.

* Reserva memoria para un nuevo nodo.

* Verifica que se haya creado el nodo y lo retorna.

*/

listas *carga_nodo(void)

{

listas *nuevo = (listas *)malloc(sizeof(listas));

if(nuevo == NULL)

printf("No se pudo crear el nodo ");

return nuevo;

}

/* Función para insertar nodos.
* Parámetro: el dato a almacenar.
* Crea un nodo con el dato pasado por parámetro.
* El primer if verifica si la lista no està cargada, sino la carga.
* El segundo if verifica la ubicación del nodo en la lista.
*/

void insertar(int dato)

{

listas *aux;



nodo = carga_nodo();

nodo->valor = dato;

nodo->sig = NULL;

if (lista != NULL)

{
if (lista->valor < dato)
{
aux = lista;
while (aux->valor < dato) /*** AQUI ESTA EL ERROR ***/
aux = aux->sig;
if (aux->sig != NULL)
{
nodo->sig = aux->sig;
aux->sig = nodo;
}else aux->sig = nodo;
free(aux);
}else nodo->sig = lista;
lista = nodo;
}else lista = nodo;

}

/* Función para listar los nodos.
* Con un puntero auxiliar recorre la lista sacando por pantalla los valores
* almacenados en cada nodo.
*/

void listar()

{

int dato;

listas *aux;


aux = lista;

while (aux != NULL)

{

dato = aux->valor;

printf("%d ", dato);

aux = aux->sig;

}

printf(" ");

free(aux);

}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder