Thread: problem sorting a dynamic list

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    16

    Exclamation problem sorting a dynamic list

    I have a list whose structure is the following:

    Code:
    typedef struct dats_s
    {
        char email[MAX_STR];
        struct data_s *next;
    } data;
    and I need to order the elements of the list alpabetically I've produced the following code but it seem not to work... the idea is to use two pointer and two while cycles so that I compare the first element of the list pointed by the first pointer while I scroll from the second to the last element of the list in serch of a smaller element; when I find it I simply exchange the two strings using a temporary string; when the second pointer reaches the end of the list I repeat this process starting from the second element and so on...

    this is the code involved in the sorting process:

    Code:
    p_corr1=p_head;
    p_corr2=(*p_corr1).next;
    
        while(p_corr1!=NULL)
        {
            while(p_corr2!=NULL)
            {
                if(strcmp((*p_corr1).email,(*p_corr2).email)>0)
                {
                    strcpy(temp_email,(*p_corr1).email);
                    strcpy((*p_corr1).email,(*p_corr2).email);
                    strcpy((*p_corr2).email,temp_email);
                }
                p_corr2=(*p_corr2).next;
            }
            p_corr1=(*p_corr1).next;
        }
    any suggestion is welcome!!
    Thank

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you plan to move p_corr2 back to the front of the list so you can get the second element correct (and then the third, etc.)?

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    16
    .... mmm.... so I have to add
    Code:
    p_corr2=(*p_corr1).next;
    at the end of the first while... this way:
    Code:
    p_corr1=p_head;
    p_corr2=(*p_corr1).next;
    
        while(p_corr1!=NULL)
        {
            while(p_corr2!=NULL)
            {
                if(strcmp((*p_corr1).email,(*p_corr2).email)>0)
                {
                    strcpy(temp_email,(*p_corr1).email);
                    strcpy((*p_corr1).email,(*p_corr2).email);
                    strcpy((*p_corr2).email,temp_email);
                }
                p_corr2=(*p_corr2).next;
            }
            p_corr1=(*p_corr1).next;
            p_corr2=(*p_corr1).next;
        }
    is this what you mean??
    Last edited by andryjohn; 09-05-2009 at 11:22 AM. Reason: grammar mistake :)

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That would be what I mean.

    And as fun as it is to simulate for loops with while, you may want to consider actually using for loops.

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    16
    ... the program this way crashes... because when p_corr1 reaches NULL I try to assign to p_corr2 something pointed by NULL!!!!

    i would fix it this way...

    Code:
    while(p_corr1!=NULL)
        {
            while(p_corr2!=NULL)
            {
                if(strcmp((*p_corr1).email,(*p_corr2).email)>0)
                {
                    strcpy(temp_email,(*p_corr1).email);
                    strcpy((*p_corr1).email,(*p_corr2).email);
                    strcpy((*p_corr2).email,temp_email);
                }
                p_corr2=(*p_corr2).next;
            }
            p_corr1=(*p_corr1).next;
            if(p_corr1!=NULL)
                p_corr2=(*p_corr1).next;
        }
    using for cycle...
    come on don't make me suffer... tell me how!!! :-)

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by andryjohn View Post
    ... the program this way crashes... because when p_corr1 reaches NULL I try to assign to p_corr2 something pointed by NULL!!!!
    And that's why you have to be careful when you simulate for-loops with while. Think about what your for loops would like:
    Code:
    for (p_corr1 initialized; p_corr1 not NULL; p_corr1 moves forward) {
        for (p_corr2 initialized; p_corr2 not NULL; p_corr2 moves forward) {
    If p_corr1 is NULL the second loop doesn't even happen.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    16
    oooo yes!! now I see... that's way cleaner!!!

    this could be the code:
    Code:
    for (p_corr1=*p_dati; p_corr1!=NULL; p_corr1=(*p_corr1).next)
        {
            for (p_corr2=(*p_corr1).next; p_corr2!=NULL; p_corr2=(*p_corr2).next)
            {
                if(strcmp((*p_corr1).email,(*p_corr2).email)>0)
                {
                    strcpy(temp_email,(*p_corr1).email);
                    strcpy((*p_corr1).email,(*p_corr2).email);
                    strcpy((*p_corr2).email,temp_email);
                }
            }
        }
    and it works perfectly!!!

    THANK YOU SOOO MUCH!!! you're help was infinitely useful for me!!! thank's again tabstop!!

    Andryjohn

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by andryjohn
    this could be the code:
    I suggest that you rewrite it slightly:
    Code:
    for (p_corr1 = *p_dati; p_corr1 != NULL; p_corr1 = p_corr1->next)
    {
        for (p_corr2 = p_corr1->next; p_corr2 != NULL; p_corr2 = p_corr2->next)
        {
            if (strcmp(p_corr1->email, p_corr2->email) > 0)
            {
                strcpy(temp_email, p_corr1->email);
                strcpy(p_corr1->email, p_corr2->email);
                strcpy(p_corr2->email, temp_email);
            }
        }
    }
    The logic is entirely the same. I merely modified the formatting slightly to be more consistent, and used the notation a->b instead of the notation (*a).b.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jul 2009
    Posts
    16
    thanks laserlight but what do you mean with
    more consistent
    ?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that the indentation style of your outer for loop is different from that of your inner for loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    16
    ok... I see... It may have occurred when pasting the code any way thanks!!

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    the nice thing about list is that you can just play with pointers to change the order of the links - instead of copying the whole nodes...

    you are working with the list as if it is array

    Code:
    Head -> A -> B -> C
    
    
    To swap A and C you are doing
    
    Head -> C -> B -> A
    
    While you can
    
    Head  A <- B <- C
      |             ^
      --------------|
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Jul 2009
    Posts
    16
    I'm not a big fan of pointers.... they require my only neuron to work overtime!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. C programming-Linked list problem
    By male4329 in forum C Programming
    Replies: 18
    Last Post: 06-02-2005, 02:05 AM
  4. doubly linked list problem
    By YevGenius in forum C Programming
    Replies: 4
    Last Post: 05-02-2004, 08:54 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM