Chained List in C - How to implement insertion function

Asked

Viewed 281 times

-1

I am trying to implement data into a chained list. How can I do a function to enter login data, name and value into my list?

typedef struct registro_st{         // sequência de objetos do mesmo tipo
    char login[50];
    char nome[50];
    float valor;
    struct registro *prox;
} registro;

typedef struct nodo_st{
    registro dado;
    struct nodo *prox;
} nodo;

typedef struct Lista_st{
    nodo *cabeca;
    nodo *cauda;
    int tamanho;
} lista;


nodo* CriarNodo(registro p){

    nodo* n;
    n = (nodo*)malloc(sizeof(nodo));
    n->dados = p;
    n->prox= NULL;
    return n;
}

lista* criarLista(){
    lista* l = (lista*)malloc(sizeof(lista));
    l->cabeca = NULL;
    l->cauda = NULL;
    l->tamanho = 0;
    return l;
}

1 answer

1


Do your input readings on main, create a new record on main (you didn’t put the creation function Log, so I assumed you haven’t implemented it yet). After reading the data, put this way in the record, node and, consequently, in the list:

int main() {
    .
    .
    .
    registro_st *novoRegistro = criaRegistro(); //é importante fazer essa função!
    printf("Login: ");
    scanf("%s", novorRegistro->login);
    printf("Nome: ");
    scanf("%s", novorRegistro->nome);
    printf("Valor: ");
    scanf("%f", novorRegistro->valor);
    nodo_st *novoNodo = criaNodo(novoRegistro);
    inserirLista(novoNodo);
    .
    .
    .
    }

Browser other questions tagged

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