Insertion at the end of a chained list C

1

I'd like to know how to go through the list so that I can add a new one at the end of it, in the case at the position in the-> prox of the last one in this list. Here is my code:

typedef struct lista {
   struct no *prim;
} Lista;

typedef struct no {
   char *nome;
   char *apelido;
   char *email;
   struct no *prox;
} No;

void adicionar(Lista* lista, No var){
   No* novo = malloc(sizeof(No));

   novo->nome = var.nome;
   novo->apelido=var.apelido;
   novo->email=var.email;

   if (lista->prim == NULL){
      lista->prim = novo;
   }
   else{

   }
}

My question is what to put in this else for this insertion to be made.

    
asked by anonymous 05.06.2018 / 08:09

1 answer

1

You only need to use while to go through the nodes until you reach the end. The last is what has ->prox a NULL .

So:

...
else{
    No* atual = Lista->prim; //começar no primeiro
    while (atual->prox != NULL){ //navegar até ao fim
        atual = atual->prox;
    }

    atual->prox = novo; //colocar o novo nó no fim
}

Now this will only work if each time you create a new node to by at last, or as the first node, set up your ->prox correctly, specifying NULL , which is something you are not doing.

So in creating the node you should add:

No* novo = malloc(sizeof(No));
novo->prox = NULL; //<--isto
    
05.06.2018 / 12:16
___ ___ erkimt Div does not fit the screen ______ qstntxt ___

On the

link

I use the plugin animate link

So the array of %code% is thus by default

%pre%

While the site is screen beauty

But when you're on mobile, one of the %code% , which I can not identify, is too long ( %code% ).

Iwouldliketomake%code%havetheheightofyourinterior!

Howtodothisforsmallerscreens?

ButtheproblemisthatIcouldnotidentifywheretochange!

    
______azszpr304340___

Your problem is that you are using a %code% in the Mobile version that is too large. in your @media makes a rule to remove this %code% . See below in the image to see how I fixed the bug.

OBS: See in the Chrome DevTools panel that I remove from %code% o %code%

    
___ Use of Select to load data