Insert end list function on

Asked

Viewed 76 times

-2

Guys I’m not getting to develop this function.

bool inserir_fim (tipo_lista * p, tipo_lista * novo_no)
{



}
  • You have examples here: http://www.cprogressivo.net/2013/10/Lista-simplesmentente_chainedto a hair_com-C-Inserindo-nos-no-inicio-e-fim.html

1 answer

1


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

struct tipo_lista{
    int info;
    struct tipo_lista * prox;
};

typedef struct tipo_lista tipo_lista;

tipo_lista * cria_no (int valor)
{
    tipo_lista * novo;
    novo = (tipo_lista *)malloc(sizeof(tipo_lista));
    novo -> info = valor;
    novo -> prox = NULL;
    return novo;
}

bool inserir_fim (tipo_lista * p, tipo_lista * novo_no)
{
    if (!p) {
        return false;
    }

    while (p->prox) {
        p = p->prox;
    }

    p->prox = novo_no;

    return true;
}

int main(void) {
    tipo_lista * p = cria_no(1);
    inserir_fim(p, cria_no(2));
    printf("%d %d\n", p->info, p->prox->info);
    return 0;
}
  • 1

    It would not be true or false return?

  • 1

    I think so, maybe 0 and 1 works, but it’s better true and false.

  • And how I test it on main?

  • 1

    You will need the function to create a new node to test this function.

  • type: https://pastebin.com/vyGn1mJm

  • 1

    I edited the answer with the main.

  • error on this main printf

  • 1

    What error did you make? I am testing here: https://ideone.com/6h1WsX

Show 4 more comments

Browser other questions tagged

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