How to pass string to an insert function

Asked

Viewed 40 times

0

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct lista{

    char nome[20];

    int idade;

    struct lista* prox;

};

typedef struct lista Lista;


Lista* lst_cria(){

    return NULL;

}

Lista* lst_insere(Lista* lst, int id, char name[], int n){

    
    Lista* Nlst=(Lista*)malloc(sizeof(Lista));

    Nlst->nome; 

    Nlst->idade=id; 

    Nlst->prox=lst;

    return Nlst;

    
}

void lst_imprime(Lista* lst){
    
    Lista* p;
    
    for(p=lst; p!=NULL; p=p->prox){

        printf("%s",p->nome);

        printf("\n");

        printf("%d",p->idade);

        printf("\n");

        
    }
    
    
}






main(){

    Lista* nova_lst;

    nova_lst=lst_cria();

    nova_lst=lst_insere(nova_lst,29,"lucio",20);

    nova_lst=lst_insere(nova_lst,27,"diego",20);

    lst_imprime(nova_lst);

    
    
    
}

Hello, I don’t know how to pass a string to be inserted via function. numbers print, but names don’t.

1 answer

0


You wrote:

    Lista* Nlst=(Lista*)malloc(sizeof(Lista));

    Nlst->nome; 

    Nlst->idade=id; 
    Nlst->prox=lst;
    return Nlst;

And probably intended to write

    Lista* Nlst=(Lista*)malloc(sizeof(Lista));

    Nlst->nome = nome; 

    Nlst->idade=id; 
    Nlst->prox=lst;
    return Nlst;

Understand that a List is a set of nodes. And each node has a payload, a load of data, which in general is a simple pointer.

When you put everything together in the node structure as you did here you will get a lot more work and less result. The List is a container. It’s even called that in C++, or Collection in java. So the List has metadata. Controls that should be INSIDE the List and not released by the program.

Compare your structure with this one below and imagine if your program would be easier to control or not, especially if you have some 3 lists in the same program...

typedef struct
{
    char nome[20];
    int idade;

}   Item;

typedef struct st_node
{
    Item*           D; // dado
    struct st_node* N; // adiante
    struct st_node* P; // atras

}   Node;

typedef struct lista
{
    unsigned    size;
    unsigned    limite;
    Node*       inicio;
    Node*       fim;

}   Lista;

Understand that in this case

  • if you change the data you do not need to move the List or Node. Only Item changes.
  • some questions are solved immediately: each list has its size INSIDE and its pointers.
  • here each node has a data load
  • the list has nodes and nothing talks about data

For example in your program the very routine that inserts a node has the DATA fields as argument. So everything gets tied up and if you change one you have to change everything...

    Lista* lst_insere(Lista* lst, int id, char name[], int n);

When could it be

    Lista* lst_insere(Item* item, Lista* lista);

And so if you just put the phone in Item nothing changes. If you exchange Item for that list of books of the following exercise nothing changes... ;)
So as far as the list is concerned the routine you insert inserts a node in the list. nothing more. never more.

Understand that the first function you write is not the one you insert but the one that lists the elements. The logic? Simple: you will need to list to test, and the list function has to work for an empty or non-existent list. Then type this one and the one that deletes the list. Then the one that inserts nodes...

Note: In general, using only one-sided pointers in the list only complicates things and doesn’t simplify anything. Anything you do with the list will mean going back to the beginning and repositioning yourself. All the time. As you usually use the list to navigate, say, a list, you can imagine what happens: in a queue to return a position you have to tell where it is and go back to the beginning and count again. In a playlist, the classic exercise, to turn the music back does not give, and so on: everything gets more complicated and slow.

Browser other questions tagged

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