Thread: Need help sorting a linked list. Beginner

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    Need help sorting a linked list. Beginner

    HERES MY CODE:


    Code:
    #include <stdio.h>    /* for printf */
    #include <stdlib.h>   /* for malloc */
    
    typedef struct node
    {
        char player_id[7];
        int level;
        int score;
    
        struct node *next; /* pointer to next element in list */
    } *LIST;
    
    LIST new_list() // Create new list by creating sentinel node
    {
        LIST list = (LIST) malloc(sizeof(struct node));
        list->next = NULL;
        return list;
    }
    
    // Add node to beginning of list
    LIST list_add(LIST list, int i)
    {
        LIST new_node = (LIST) malloc(sizeof(struct node));
    
        if (new_node == NULL)
            return NULL;
    
        new_node->score = i;
        new_node->next = list;
        return new_node;
    }
    
    LIST list_remove(LIST list) /* remove node */
    {
        LIST n;
        if (list->next == NULL)
        {
            return list;
        }
        if (list != NULL)
        {
            n = list->next;
            free(list);
            return n;
        }
        else
            return NULL;
    }
    
    
      LIST *sortlist(LIST list)
      {
        LIST sorted;
        if (list !=NULL)
      }
    
    
    void list_print(LIST list)
    {
        if (list == NULL)
        {
            printf("list is empty\n");
        }
        while (list != NULL)
        {
            printf("score: %d\n", list->score);
            list = list->next;
        }
    }
    
    int main(void)
    {
        LIST slist;
        int len = 5;
        int i, array[len];
    
        slist = new_list();
    
        for (i = 0; i < len; i++)
        {
            printf("Enter a score: ");
            scanf("%d", &array[i]);
            //printf("%d\n", array[i]);
            slist = list_add(slist, array[i]);
        }
    
        list_print(slist);
    
        return 0;
    }

    I dont know what to put in *sortlist..

    How do I do it!? please help

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    What do you want to sort by? player_id? level? score?

    You've got 2 choices:

    * Sort after you've created the linked list
    * Sort while you're creating the linked list

    The general idea for the latter is (assuming smallest to biggest),

    (each box represents a node, and the value inside is the value you're sorting by)
    Code:
    Inserting: [2, 5, 6, 3, 1]
    
    insert 2:
     _
    |2| -> NULL                      the first node goes at the front -- this is a sorted list
     -
    
    insert 5:
     _      _
    |2| -> |5| -> NULL               5 is greater than 2, so it goes after it (traverse and compare)
     -      -
    
    insert 6:
     _      _      _
    |2| -> |5| -> |6| -> NULL        6 is greater than 5 so it goes after it (ie the end)
     -      -      -
    
    insert 3:
     _      _      _      _
    |2| -> |3| -> |5| -> |6| -> NULL  3 is greater than 2 so it goes after it -- but less than 5, so it goes before 5
     -      -      -      -
    
    insert 1:
     _      _      _      _      _
    |1| -> |2| -> |3| -> |5| -> |6| -> NULL  1 is less than 2 so it goes before it
     -      -      -      -      -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 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