Thread: Help on Sorting a Linked List

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Help on Sorting a Linked List

    Hi everyone!
    I'm trying to develop a function to sort a linked list. I get no compiling errors, but when I run the program, my program stalls.

    Can anyone help?

    insert
    Code:
    struct node* Sort(struct node *head)
       {
          struct node *original, *current, *pointer, *newList;
          original = head;
          current = head;
          pointer = head;
          int smallest = current->data;
          int count = 0;
                    
          pointer = head;
          for(current = head; current != head; current = current->next){
             for(pointer = head; pointer != head; pointer = pointer->next){
            
                if(pointer->data < smallest){
                   smallest = pointer->data;
                }
             }     
             
             while (original->data != smallest){ // count where smallest is in the orignal link list
                original = original->next;
                count++;
             }        
               
             newList = AddToList(newList, smallest); // add the smallest value to newList
             RemoveFromList(head, count);	// remove the smallest value from the original list
    			count = 0;   // reset count    
             smallest = current->data;	// smallest is now the next value in current
          } 
            return newList;	// return the newList
          }

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    I don't think you want to do this:

    Code:
          for(current = head; current != head; current = current->next){
             for(pointer = head; pointer != head; pointer = pointer->next){

    Those loops would never be entered.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    How come?
    I'm really puzzled on how to sort a linked list... :/

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >>How come?
    The for loop sets pointer = head; then does the logical test pointer != head; which is false. So the loop will exit immediately and the nested loop is skipped over because it is considered to be the body of the outer loop.

    >>I'm really puzzled on how to sort a linked list... :/
    It can be daunting the first time you write a sorting routine for it. There are web pages to help you understand it in a good way though. If you don't feel like writing a routine then you could always qsort an array of nodes and put the list back together. *shrug*

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Take a look at here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List help!!
    By onepuzzledstud in forum C Programming
    Replies: 2
    Last Post: 10-11-2010, 08:36 PM
  2. Circularly-Doubly Linked List implementation
    By BlackOps in forum C Programming
    Replies: 4
    Last Post: 07-19-2009, 04:45 AM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 06:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

Tags for this Thread