Click here to Skip to main content
12,455,517 members (63,686 online)
Rate this:
 
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
Updated 13-May-11 2:03am
v2
Comments
Olivier Levrey 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 13-May-11 8:33am
   
Correct, my 5!
SAKryukov 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 13-May-11 8:09am
   
Almost correct. My 4. stoNext pointer is not updated properly.
Manfred R. Bihy 13-May-11 8:31am
   
Thanks for pointing it out!
FTFM ;)
dinesh_redhawk 13-May-11 9:29am
   
Thanx all for ur immediate & quick support
Manfred R. 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 13 May 2011
Copyright © CodeProject, 1999-2016
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