Thread: How to access data from a Linked List?

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    Question How to access data from a Linked List?

    Hi!

    I believe it must be quite simple to do, and I'm not finding it on the forum. I'd like to access a specific variable in my code which is in a Linked List.

    I've tried to use something like struct_name[position].variable_name, but It's not working. On the code below, I tried to print its value...

    What am I supposed to do?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct list {
        int info;
        struct list* next;
    };
       
    typedef struct list List;
    
    List* initialize (void)
    {
         return NULL;
    }
        
    List* insert (List* l, int i)
    {
           List* new = (List*) malloc(sizeof(List));
           new->info = i;
           new->next = l;
           return new;
     }
        
    void print (List* l)
     {
           List* p;   
           for (p = l; p != NULL; p = p->next)
    
                printf("info = %d\n", p->info);
     }
        
    int main(int argc, char *argv[])
    {
        
        l = initialize();  
        l = insert(l, 23); 
        l = insert(l, 45);
      
         print(l);
         printf("%d\n",l[0].info); //<------------------------ Here
      
    
         system("PAUSE");    
         return 0;
    Thanks =)

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    19
    i've tried to use something like struct_name[position].variable_name, but It's not working. On the code below, I tried to print its value...
    structures are NOT array, so struct-name[position], is wrong.

    besides it is a LINKED list where 1 structure points to another one SINGLE structure, so they are not even "similar" to arrays.

    Now, if you look carefully you are dealing here with POINTERS TO structure.

    now if p is a pointer to a structure, you access its members by (*p).member_name.
    or more simply by p->member_name, which is just a shorthand for the previous version.

    so here it should be l->info.
    now, l is a pointer to the FIRST structure, so you have to enter the loop by assigning to l the pointer to the next structure.
    .
    In order to terminate the loop, you make sure that it stops when the structure pointer l is equal to null, i.e. 0, yes address zero, which is a sign that we reached one structure beyond the "meaningful" structures, and in fact this is exactly what you defined above for function print().
    Last edited by alecodd; 01-25-2011 at 09:23 PM.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    uhmm

    So the only way is to use a loop to access one by one...
    Thanks for your explanation, alecodd!

    This code is not mine, actually. That's why one print routine works and the another one doesn't. I'm using it to understand how all this stuff work, and than work on my project =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM

Tags for this Thread