Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to traverse this linklist (stoListItem) in C

this is the code i have written:

C
typedef struct ListItem
{
    char* pc_Serial_Number;
    int n_Availability;
    char* pc_Color;	
    char* pc_IPAddress;
    struct ListItem *pstNextListItem;
} ListItem_t;

ListItem_t stoListItem;
ListItem_t *stoNext;

stoNext = &stoMVCIListItem;
if (stoNext !=0)
{
    while (stoNext->pstNextListItem!= 0)
    {
        printf("\n Device (%d)", iMVCI_No);
        printf("\n SerialNumber = %s \n Availability = %d"
            "\n Colour = %s \n IP = %s",
            stoListItem.pc_Serial_Number, stoListItem.n_Availability,
            stoNext->pc_Color, stoNext->pc_IPAddress);
        i_No++;
        /*stoNext = stoNext->pstNextListItem;*/
        stoNext = stoListItem.pstNextListItem;
    }
    i_No = 0;
}



Please tell me what is wrong in this..Why iam not able to get the proper values from the linked list
Posted
Updated 13-May-11 2:03am
v2
Comments
Olivier Levrey 13-May-11 8:04am    
Edited for readability.

Manfred's answer is almost correct. You just need to keep your commented statement:
C
stoNext = &stoMVCIListItem;
 
while (stoNext != 0)
{
    printf("\n Device (%d)",iMVCI_No);
    printf("\n SerialNumber = %s \n Availability = %d"                     "\n Colour = %s \n IP = %s",
        stoNext->pc_Serial_Number,
        stoNext->n_Availability,
        stoNext->pc_Color,
        stoNext->pc_IPAddress
    );
    i_No++;
    //your commented statement was the correct one actually
    //it updates current pointer to the next item in the list
    stoNext = stoNext->pstNextListItem;
}
 
Share this answer
 
Comments
Manfred Rudolf Bihy 13-May-11 8:33am    
Correct, my 5!
Sergey Alexandrovich Kryukov 13-May-11 14:01pm    
Sure, a 5.
--SA
It should read more like this:

C#
typedef struct ListItem
{
    char* pc_Serial_Number;
    int n_Availability;
    char* pc_Color;
    char* pc_IPAddress;
    struct ListItem *pstNextListItem;
}ListItem_t;

ListItem_t stoListItem;
ListItem_t *stoNext;

stoNext = &stoMVCIListItem;

while (stoNext != 0)
{
    printf("\n Device (%d)",iMVCI_No);
    printf("\n SerialNumber = %s \n Availability = %d"                     "\n Colour = %s \n IP = %s",
        stoNext->,pc_Serial_Number,
        stoNext->n_Availability,
        stoNext->pc_Color,
        stoNext->pc_IPAddress
    );
    i_No++;
    stoNext=stoNext->pstNextListItem;
}

i_No=0;


That's all! The if block can be eliminated completely and all the nescessary work is now done in the while loop.

Best Regards,

-MRB
 
Share this answer
 
v2
Comments
Olivier Levrey 13-May-11 8:09am    
Almost correct. My 4. stoNext pointer is not updated properly.
Manfred Rudolf Bihy 13-May-11 8:31am    
Thanks for pointing it out!
FTFM ;)
[no name] 13-May-11 9:29am    
Thanx all for ur immediate & quick support
Manfred Rudolf Bihy 13-May-11 11:43am    
Your welcome!
As Olivier already pointed out you were already onto the right track.

Keep at it and have fun!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900