CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2003
    Posts
    29

    Strings in Linked List

    I have problem in linked list. When I compile and run the below code I have getting the following out put.

    NM9
    10.000000,11.000000,12.000000,13.000000

    NM9
    11.000000,12.000000,13.000000,14.000000

    NM9
    12.000000,13.000000,14.000000,15.000000

    NM9
    13.000000,14.000000,15.000000,16.000000

    NM9
    14.000000,15.000000,16.000000,17.000000

    NM9
    15.000000,16.000000,17.000000,18.000000

    NM9
    16.000000,17.000000,18.000000,19.000000

    NM9
    17.000000,18.000000,19.000000,20.000000

    NM9
    18.000000,19.000000,20.000000,21.000000

    NM9
    19.000000,20.000000,21.000000,22.000000

    Which I expected to be like this.

    NM0
    10.000000,11.000000,12.000000,13.000000

    NM1
    11.000000,12.000000,13.000000,14.000000

    NM2
    12.000000,13.000000,14.000000,15.000000

    NM3
    13.000000,14.000000,15.000000,16.000000

    NM4
    14.000000,15.000000,16.000000,17.000000

    NM5
    15.000000,16.000000,17.000000,18.000000

    NM6
    16.000000,17.000000,18.000000,19.000000

    NM7
    17.000000,18.000000,19.000000,20.000000

    NM8
    18.000000,19.000000,20.000000,21.000000

    NM9
    19.000000,20.000000,21.000000,22.000000

    I have attached the code. Could any one please tell me what is wrong. I am getting the values correctly as I wanted but not the string.

    Thanks and Regards,
    Abbas
    Attached Files Attached Files
    • File Type: c tes.c (1.1 KB, 101 views)

  2. #2
    Join Date
    Mar 2002
    Location
    AhuhA
    Posts
    204

    Smile

    I have taken a look at your code, I amnot really sure, but I found that your code would output something different from what you posted here ...?!
    Your problem might be at the display function and the variable loc though....I think, if possible, you should be a litle more specific so that people here could give your some help.

    Regards,

  3. #3
    Join Date
    Mar 2003
    Posts
    29
    I am sorry I should have attached the programme which I have attached now. The issue is.....

    In the structure there are two variables one is character pointer and a double array.

    What ever I store in the double array gets printed perfectly. But not in the character. What ever is stored in the character variable at last is repeated until the end of the list when I print it.

    NAME10
    1
    NAME10
    2
    NAME10
    3

    So on ......

    Thanks and Regards,
    Abbas
    Attached Files Attached Files
    • File Type: c list.c (985 Bytes, 95 views)

  4. #4
    Join Date
    Mar 2002
    Location
    AhuhA
    Posts
    204
    Oh well, I guess your problem is at your loop. You displayed after you finish your loop, so the variable j will contain the final value.
    That is why your program output the same number after NM.
    This is not really difficult problem, and it is advisable for you to correct this mistake yourself ! I am also sure you can make it !Good luck !!!


    Regards,

  5. #5
    Join Date
    Oct 2001
    Location
    Venezuela
    Posts
    35
    Hi. The problem here is when you copy the string:

    temp->name=nm;

    What you are doing here is just copying the address of the string nm into temp->name. If you change the string in nm, you are also changing the value pointed by temp->name. At the end all temp->name in every node will point to the same string. What you have to do is to reserve some space for the string with malloc and then copy the string char by char:

    Ask for memory for the string:

    temp->name=(char*) malloc(strlen(nm)+1);

    Copy the string:

    strcpy(temp->name, nm);

    And that's it

    Hope this helped

    Kabalprog

  6. #6
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Originally posted by KabalProg
    What you have to do is to reserve some space for the string with malloc and then copy the string char by char:

    Ask for memory for the string:

    temp->name=(char*) malloc(strlen(nm)+1);

    Copy the string:

    strcpy(temp->name, nm);

    And that's it

    Hope this helped

    Kabalprog
    Actually, that's not it. For every malloc() you have in your
    program, there should be a corresponding free(). If you don't
    know this already, then look up malloc() in your compiler's
    documentation.

    Since you gave a .c file, I assume you're writing a C program and
    asking questions about it here [in a C++ forum]. That's fine. I
    just wanted to make the point that if you're using C++, it is
    recommended that you use new and delete instead of malloc()
    and free(). For YOUR particular case it's probably not an issue,
    but it's for those people who read this post as a result of a google
    search four years from now

    --Paul

  7. #7
    Join Date
    Mar 2003
    Posts
    29
    Hi PaulWendt,

    I actually work in a API related to UNIGRAPHICS a CAD/CAM/CAE package. Its API help and examples are rich in C but not in C++. Thats why I am using C. Any how once I master its API I will definitely switch to C++. Thanks for your intrest and time spent.

    Thanks and Best regards,
    Abbas

  8. #8
    Join Date
    Oct 2001
    Location
    Venezuela
    Posts
    35
    Yes, you are right, every time you use malloc you have to do a free. But you can do that when you are destroying the list.

    Instead of free each node like this:

    void freelist(struct mass_list_s **q)
    {
    for(mass_list_s *p=*q; p; )
    {
    *q=p->link;
    free(p);
    p=*q;
    }
    }

    You must do it like this:

    void freelist(struct mass_list_s **q)
    {
    for(mass_list_s *p=*q; p; )
    {
    *q=p->link;
    free(p->name);
    free(p);
    p=*q;
    }
    }

    KabalProg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured