Doubt in a C program !!! List simply chained!

0

I was trying to make a linked list, but I can not get it to insert or print it. If anyone can bring a light to my program, I appreciate it.

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

    typedef struct cel celula;
    struct cel{
         int dado;
         celula *prox;
         };

    void inicializar(celula *p,celula *aux);
    int inserir(celula *p,celula *aux,int count,int valor);
    void imprimir(celula *p,celula *aux,int count);

    main()
    {
    celula *p, *aux;
    int count=0,chave,valor;

    inicializar(&p,&aux);

    printf("Digite o tipo de entrada:\n");
    printf("1 - Inserir numero;\n");
    printf("2 - Imprimir lista de numeros;\n");
    printf("3 - Sair.");
    while(chave!=3)
    {Restart:
    printf("\nInsira a chave: ");
    scanf("%d",&chave);
    switch(chave){
        case 1:
           printf("\nDigite o valor a ser inserido: ");
           scanf("%d",&valor);
           count=inserir(&p,&aux,count,valor);
           break;

        case 2:
           imprimir(&p,&aux,count);
           break;

        case 3:
           printf("\nPrograma encerrado!\n");
           break;

        default:
           printf("\nChave incorreta!\n");
           goto Restart;
         }
      }
   }

   void inicializar(celula *p,celula *aux)
   {
      p=NULL;
      aux=NULL;

   }

   int inserir(celula *p,celula *aux,int count,int valor)
   {
   if(count == 0)
       {p=(celula *)malloc(sizeof(celula));
       aux=p;
       p->dado=valor;
       p->prox=NULL;
       printf("%d/%d",p->dado,count);
       getch();
       count ++;
       return count;
       }
       else
       {aux->prox=(celula *)malloc(sizeof(celula));
       aux=aux->prox;
       aux->dado=valor;
       printf("%d/%d",aux->dado,count);
       getch();
       aux->prox=NULL;
       count ++;
       return count;
       }
   }

   void imprimir(celula *p,celula *aux,int count)
   {
   int i;

   for(i=0;i<count;i++)
      {printf("%d ",p->dado);
      p->prox;
      }
   }
    
asked by anonymous 14.10.2016 / 00:01

1 answer

3

Initialize the list

You can easily check for some implementation errors such as incializar , where you do not allocate memory for pointers. Here's an example of how such a function can be done.

Take a celula_s struct as follows:

typedef struct celula_s{

    int          valor;
    struct nodo  *prox;

} lista_t;

Then the inicializar function can be implemented by allocating memory for the list pointer using the malloc function:

lista_t* lista_inicializar(void){
    lista_t* lista = (lista_t *) malloc( sizeof (lista_t) );
    lista->valor = 0;
    lista->prox = NULL;
    return lista;
}

Insert in the list

Now let's implement a inserir function. In a list that is simply chained you can create a function to insert in whatever position, so here I will show you how to implement a function to insert elements at the beginning of the list.

First we need a cell or node that will be the "head" of the list, in this implementation the next node of the "head" will be the beginning of the list that we have so far, ie it will be in the new first node: / p>

lista_t* lista_inserir_inicio(lista_t* lista, int valor){
    lista_t* novo = lista_inicializar();

    if(novo != NULL){
        novo->valor = valor;
        novo->prox = lista;
        lista = novo;
    }

    return lista;
}

Print the list

To print the list we need a helper pointer of type aux , it will point to the list we want to print, so long as the next element of the aux pointer pointing to the list is not NULL print this cell and move on to the next one.

int lista_imprimir(lista_t* lista){
    lista_t* aux;
    aux = lista;
    printf("[");
    while (aux->prox != NULL){
        printf(" %d ", aux->valor);
        aux = aux->prox;
    }
    printf("]\n");
    return 0;
}

You can check out the code at link .

Att.

    
14.10.2016 / 01:20