Doubt in a program in C!!! Simply chained list!

Asked

Viewed 321 times

0

I was trying to make a chained list, but I can’t get her to insert it or print it. If anyone can bring a light to my program I thank you.

    #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;
      }
   }

1 answer

3

Initialize the list

It is possible to easily check some implementation errors as in the function incializar, where you do not allocate memory for pointers. See an example of how such a function can be done.

Take a struct celula_s as follows:

typedef struct celula_s{

    int          valor;
    struct nodo  *prox;

} lista_t;

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

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

Insert into the list

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

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

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 list type auxiliary pointer aux, it will point to the list we want to print, so while the next element of the pointer aux that is pointing to the list is not NULL print this cell and pass to the next.

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 the code in full on this link.

Att.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.