Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
I want to traverse this linklist (stoListItem) in C
 
this is the code i have written:
 
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 13-May-11 1:43am
Edited 13-May-11 2:03am
v2
Comments
Olivier Levrey at 13-May-11 8:04am
   
Edited for readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Manfred's answer is almost correct. You just need to keep your commented statement:
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;
}
  Permalink  
Comments
Manfred R. Bihy at 13-May-11 8:33am
   
Correct, my 5!
SAKryukov at 13-May-11 14:01pm
   
Sure, a 5.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It should read more like this:
 
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
  Permalink  
v2
Comments
Olivier Levrey at 13-May-11 8:09am
   
Almost correct. My 4. stoNext pointer is not updated properly.
Manfred R. Bihy at 13-May-11 8:31am
   
Thanks for pointing it out!
FTFM ;)
dinesh_redhawk at 13-May-11 9:29am
   
Thanx all for ur immediate & quick support
Manfred R. Bihy at 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)

  Print Answers RSS
0 CPallini 420
1 Sergey Alexandrovich Kryukov 310
2 George Jonsson 269
3 Gihan Liyanage 255
4 OriginalGriff 245
0 OriginalGriff 4,903
1 CPallini 3,960
2 Sergey Alexandrovich Kryukov 3,474
3 George Jonsson 2,786
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140905.1 | Last Updated 13 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100