List linked in C

-1

First I create the list, then I check if it is empty or not, then I pass the values that I want the list to have, but when I print the lists I do not get the values that I passed on to them. How do I print what I'm going through?

struct alumnos
{
    char nombre[30];
    int cedula;
    int telefono;
    struct alumnos *siguiente;
};

typedef struct alumnos _nodo;

_nodo *crearLista(_nodo *apuntador);

bool listaVacia(_nodo *apuntador);

_nodo *insetarEnLista(char nombre[], int cedula, int telefono, _nodo *apuntador);

void imprimirLista (_nodo *apuntador);

_nodo *crearLista(_nodo *apuntador)
{
    return (apuntador = NULL);
}

bool listaVacia(_nodo *apuntador)
{
    if (apuntador == NULL)
    {
        return (true);
    }
    else
    {
        return (false);
    }
}

_nodo *insetarEnLista(char nombre[], int cedula, int telefono, _nodo *apuntador)
{

    _nodo *registroNuevo, *apuntadorAuxiliar;

    registroNuevo = (_nodo *) malloc(sizeof(_nodo));

    if (registroNuevo != NULL)
    {
        strcpy(registroNuevo->nombre, nombre);
        registroNuevo->cedula, cedula;
        registroNuevo->telefono, telefono;
        registroNuevo->siguiente = NULL;

        if (listaVacia(apuntador))
        {
            apuntador = registroNuevo;
        }
        else
        {
            apuntadorAuxiliar = apuntador;

            while (apuntadorAuxiliar->siguiente != NULL)
            {
                apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
            }

            apuntadorAuxiliar->siguiente = registroNuevo;
        }
    }

    return apuntador;
}

void imprimirLista (_nodo *apuntador)
{
    _nodo *apuntadorAuxiliar;

    apuntadorAuxiliar = apuntador;

    if (apuntador == NULL)
    {
        printf("NO HAY ELEMENTOS EN LA LISTA \n");
    }
    else
    {
        while(apuntador != NULL)
        {
            printf(" ------------NODO-------------- \n");
            printf("NOMBRE: %S \n", apuntadorAuxiliar->nombre);
            printf("CEDULA: %d \n", apuntadorAuxiliar->cedula);
            printf("TELEFONO: %d \n", apuntadorAuxiliar->telefono);

            apuntadorAuxiliar = apuntadorAuxiliar->siguiente;
        }
    }

    return;
}

int main()
{
    /*printf("INTRODUZCA LOS NUMEROS DE CEDULA QUE DESEA IMPRIMIR \n");*/

    _nodo *iniciolista;
    int cedula, telefono;

    scanf("%d", &cedula);
    scanf("%d", &telefono);

    iniciolista = crearLista(iniciolista);

    iniciolista = insetarEnLista("Luis", cedula, telefono, iniciolista);
    iniciolista = insetarEnLista("Luis", 456, 547991, iniciolista);

    imprimirLista(iniciolista);

    return 0;
}

This is what it prints me:

-----NODO-----
NOMBRE:
CEDULA: 0
TELEFONO: 0
    
asked by ilfredo 20.11.2016 в 03:27
source

1 answer

1
_nodo *crearLista(_nodo *apuntador)
{
    return (apuntador = NULL);
}

We already started badly if the function that is supposed to create the list does not absolutely nothing .

It is assumed that this function should create the list in case it does not exist, then its implementation should look more like the following example:

_nodo *crearLista(_nodo *apuntador)
{
  if( apuntador == NULL )
  {
    apuntador = (_nodo*)malloc(sizeof(_nodo));
    apuntador._siguiente = NULL;
  }

  return apuntador;
}

But of course, the problem that you face then is that you can not use this first element as a valid node in the list, since its fields have no value. On the other hand, this function will not be useful if the pointer of the list is not correctly initialized:

int main()
{
  _nodo *iniciolista; // ¿Qué valor tiene aquí iniciolista? Seguro que no es NULL
  iniciolista = crearLista(iniciolista); //Esta llamada no va a funcionar

One possible solution would be:

int main()
{
  _nodo *iniciolista = crearLista(NULL);

If your idea is that the pointer to iniciolista points to a valid node then the function crearLista should not exist, you already have ìnsetarEnLista 'that does the same operation and also adds a valid node:

int main()
{
  scanf("%d", &cedula);
  scanf("%d", &telefono);

  _nodo *iniciolista = insetarEnLista("Luis", cedula, telefono, NULL); // NULL para que cree la lista correctamente
}

The program has other vices, such as the use of global variables that is something that is left over, or the total absence of a function to erase the entire list and release the reserved memory, but your biggest problem is commented here.

    
answered by 20.11.2016 в 23:05