#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Doubt with linked list


    Hello, I'm new here. I have problems in this code. I insert 4 numbers, but the output is 3 more numbers and trash still happens Segmentation FAUTL. I'm using Ubuntu.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Tipo_Lista {
            int conteudo;
            struct Tipo_Lista *seg; // Pointed to the next element of the list
    };
    
    typedef struct Tipo_Lista Lista;
    
    / / Initialize the list
    void initLista (void) {
            Lista *lst;
            lst = malloc (sizeof(Lista));
            lst = NULL;
    }
    / / Inserts new elements in the list
    Lista* insere (Lista *lst, int x) {
            Lista *nova;
    
            nova = malloc (sizeof (Lista));
            nova->conteudo = x;
            nova->seg = lst;
            
            return nova;
    }
    
    / / Display the list elements
    void imprimi (Lista *lst) {
            Lista *p;
            for (p = lst; p != NULL; p = p->seg)
                    printf ("Item: %d\n", p->conteudo);
    }
    
    int main (void) {
            Lista *lst;
    
            initLista();
            lst = insere(lst, 40);
            lst = insere(lst, 60);
            lst = insere(lst, 80);
            lst = insere(lst, 100);
    
            imprimi(lst);
    
            return 0;
    
    }
    I'm learning now linked lists. If you can help me I will be grateful.

    Please write in formal English because I speak in another language and translate with Google translator. I'm from Brazil.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Your problem is that you are not terminating your linked list chain correctly.

    One way to fix:
    1. Get rid of calling initLista() as you are simply allocating memory and not returning it, which causes a memory leak.

    2. In main() call insere() like this instead:
    Code:
            /* initLista(); <--- Removed from code */
    
            lst = insere(NULL, 40);
            lst = insere(lst, 60);
            lst = insere(lst, 80);
            lst = insere(lst, 100);
    When you call lst =insere(NULL, 40), you are telling it that the previous item is NULL, which properly terminates the list. Then your imprimi() rouitine should correctly work.

    3. Your English is actually pretty good :).
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Thumbs up


    Thank you, your explanation was great. Thanks also for the compliment. :)

IMN logo majestic logo threadwatch logo seochat tools logo