sort linked list

This is a discussion on sort linked list within the C Programming forums, part of the General Programming Boards category; Hello, I have to sort the linked list by the last name and by the balance. The function has two ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    20

    sort linked list

    Hello,
    I have to sort the linked list by the last name and by the balance. The function has two modes. If the user want to sort by balance then the mode is equal five other wise the mode sorts the function by last name. I used bubble sort but can't figure it out. If someone could help me out i will greatly appreciated it.


    [code]

    this is the sort function to sort the list. I tried to sort the by the last name the code doesn't work. If someone could help me out that would be great.

    Code:
    /*function sorts the list by last name or balance*/ 
    struct member_account *sort(struct member_account *list, int mode){
     
     struct member_account *before=list; 
     struct member_account *new; 
     if(mode==5)
     {
     }
     else{
     while(before->next && (strcmp(before->next->member_last, new->member_last)==0))
     {
       before=before->next; 
     }
    
    
    new->next = before->next;
     
    before->next=new; 
    }
    return(list); 
    
    
    }
    Last edited by johnhuge; 10-06-2011 at 11:35 AM.

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    20
    can someone please help me out

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The phrase "doesn't work" is meaningless here. The number of people that enjoy chasing down "doesn't work" is pretty small. If you want to tell us what "doesn't work" means, you're likely to get a better response.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    20
    so this is what id to sort by last name but the problem i am having is that it keeps deleting some node while sorting. Can anyone help me with this:
    Last edited by johnhuge; 10-06-2011 at 11:36 AM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    20
    need some help with above code can anyone help me out. THANKS

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    How many members does your struct have? If it's only a few, you might be better off swapping just the contents of each node rather than moving them around.
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    20
    so ur saying not to count the number of node a just do a while loop

  8. #8
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I'm saying, try something like this:

    Code:
    void swap_node( node *A, node *B ) {
       node temp;
       temp.member1 = B->member1;
       temp.member2 = B->member2;
       /* repeat for all node members */
       B->member1 = A->member1;
       B->member2 = A->member2;
       /* repeat for all node members */
       A->member1 = temp.member1;
       A->member2 = temp.member2;
       /* repeat for all node members */
       return;
    }
    All the nodes stay where they are but their contents (except for the pointer to the next one) are swapped.
    Code:
    while(!asleep) {
       sheep++;
    }

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    [before] -> [here] -> [next] -> [after]

    You have to not lose any of those pieces.
    Code:
    struct foo *before, *after, *here, *next;
    ...
    before = (however you arrived here - 1);
    here = (however you arrived here);
    next = here->next;
    after = next->next;
    
    before->next = next;
    next->next = here;
    here->next = after;
    That should do it.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    20
    thanks for the suggestion but i cannot do the swap method u have suggested, the only way i have to do it is the way i setup the function and the pointers.

  11. #11
    Registered User
    Join Date
    Sep 2011
    Posts
    20
    thanks for the replay quzah. where exactly are you reference your code to my because i can't seem to locate it. if you could let me know then i problem can figure it out. THANKS

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by johnhuge View Post
    thanks for the replay quzah. where exactly are you reference your code to my because i can't seem to locate it. if you could let me know then i problem can figure it out. THANKS
    What can't you figure out? I showed how to swap (or at least as well as my sleep deprived mind can produce at the moment), and you have a spot where you are trying to swap and it's not working, so why can't you wrap your head around what I'm showing you? It's not like you have ten spots in your code where you are doing different swaps.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    Don't try and bubble sort a linked-list.
    What makes it really easy to implement for an array is precisely what makes it incredibly hard to implement for a linked list.
    A different type of container calls for a different kind of algorithm.
    The shortest and simplest linked-list sorting algorithm is insertion sort. I strongly suggest you implement that instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  3. bubble sort in a linked list
    By condorx in forum C++ Programming
    Replies: 1
    Last Post: 12-08-2002, 07:41 AM
  4. Sort linked list
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 04:32 AM
  5. sort a linked list ???
    By goran in forum C Programming
    Replies: 2
    Last Post: 11-13-2001, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21