Exercise list linked in C (RADIO)

0

I am trying to do an exercise in C of linked lists in which I first create a person structure and then I make another speaker which will have a person structure. I use a list to make the records.

The problem is that it lets me make a record and prints it but when I want to make another record it enters the condition in which memory can not be reserved and I do not know why.

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

typedef struct persona {
    int edad;
    char *nombre;
    int cedula;
    struct persona *sig;
} Persona;

typedef struct locutor {
    Persona persona_locutor;
    struct locutor *sig;
} Locutor;

//INICIALIZAR LA LISTA
Locutor *inicializarListaLocutor(Locutor *cabeza_de_lista) {
    return cabeza_de_lista=NULL;
}

Locutor * crearLocutor() {

    //CREANDO EL Locutor
    Locutor *registroNuevo;
    registroNuevo=(Locutor*)malloc(sizeof(Locutor));

    //SINO SE PUDO RESERVAR MEMORIA PARA EL REGISTRO
    if(!registroNuevo) {
        printf("No se pudo crear memoria para el registro\n");

    return NULL;
    } else {
        //SI SE PUDO RESERVAR LA MEMORIA SE INSERTAN LOS DATOS
        printf("Ingrese el nombre del nuevo locutor: ");
        fflush(stdin);
        gets(registroNuevo->persona_locutor.nombre);
        printf("Ingrese la edad del nuevo locutor: ");
        scanf("%i", &registroNuevo->persona_locutor.edad);
        printf("Ingrese la cedula del nuevo locutor: ");
        scanf("%i", &registroNuevo->persona_locutor.cedula);
        registroNuevo->sig=NULL;
    }

    return registroNuevo;
}


Locutor *insertarEnListaLocutores(Locutor *cabeza_de_lista)
{
    Locutor *cabeza_de_listaAuxiliar ,*registroNuevo;
    registroNuevo = crearLocutor();

    //SI SE PUDO CREAR EL Locutor QUE SE INSERTE EN LA LISTA
    if(registroNuevo)
    {
        //SI LA LISTA ESTA VACIA
        if(!cabeza_de_lista)
            cabeza_de_lista = registroNuevo;
        else {
            //SE USA EL cabeza_de_lista AUXILIAR PARA MOVERSE ENTRE LOS LocutorS
            cabeza_de_listaAuxiliar = cabeza_de_lista;
            while(cabeza_de_listaAuxiliar->sig)
                cabeza_de_listaAuxiliar = cabeza_de_listaAuxiliar->sig;
            cabeza_de_listaAuxiliar->sig=registroNuevo;
        }
    }

    return cabeza_de_lista;
}

void imprimirListaLocutor(Locutor * cabeza_de_lista)
{
    Locutor *cabeza_de_listaAuxiliar;
    cabeza_de_listaAuxiliar = cabeza_de_lista;

    while(cabeza_de_listaAuxiliar)
    {
        printf("El nombre del locutor es:%s \n", cabeza_de_listaAuxiliar->persona_locutor.nombre);
        printf("La edad del locutor es:%d \n", cabeza_de_listaAuxiliar->persona_locutor.edad);
        printf("La cedula del locutor es:%d \n", cabeza_de_listaAuxiliar->persona_locutor.cedula);
        cabeza_de_listaAuxiliar = cabeza_de_listaAuxiliar->sig;
    }
    printf("\n");

}

int main ()
{

     Locutor *cabeza_de_lista_locutor;

     cabeza_de_lista_locutor = inicializarListaLocutor(cabeza_de_lista_locutor);
     cabeza_de_lista_locutor = insertarEnListaLocutores(cabeza_de_lista_locutor);
     cabeza_de_lista_locutor = insertarEnListaLocutores(cabeza_de_lista_locutor);
     cabeza_de_lista_locutor = insertarEnListaLocutores(cabeza_de_lista_locutor);

     imprimirListaLocutor(cabeza_de_lista_locutor);
     printf("\n");
     free(cabeza_de_lista_locutor);

     return 0;
}
    
asked by Mauricio Brito 26.06.2016 в 05:28
source

1 answer

3
 Locutor *cabeza_de_lista_locutor;

 cabeza_de_lista_locutor = inicializarListaLocutor(cabeza_de_lista_locutor);

On that first call to inicializarListaLocutor , where would you say it points cabeza_de_lista_locutor ? The correct answer is: to a random memory position.

What does the function do then?

Locutor *insertarEnListaLocutores(Locutor *cabeza_de_lista)
{
    if(registroNuevo)
    {
        //SI LA LISTA ESTA VACIA
        if(!cabeza_de_lista)
            cabeza_de_lista = registroNuevo;
        else
          // ...
    }

    return cabeza_de_lista;
}

Basically, since cabeza_de_lista is not NULL, you will understand that the list already has elements and will simply add the elements. The problem here is that on the first call the list does not exist .

The solution is as simple as initializing cabeza_de_lista_locutor :

Locutor *cabeza_de_lista_locutor = 0; /* o NULL, como prefieras */

cabeza_de_lista_locutor = inicializarListaLocutor(cabeza_de_lista_locutor);

On the other hand, the structure Persona should not have the pointer sig . First because you do not use it and second because in Locutor can cause problems: If a function receives a pointer of type Persona , should you consider the next pointer?

Greetings.

    
answered by 27.06.2016 / 08:50
source