Thread: Sorting linked list please help with CODE

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

    Sorting linked list please help with CODE

    Heres my program... that doesnt work completely

    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;
    
    while(gets(line) != NULL){
        int i;
        LIST *array;
        array = (LIST *)malloc(16*sizeof(LIST));   //need 16 because I have 15 levels
    //then what do I do??
        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 tried to write something for the sort list function... but I dont understand what to do?.. please.. some code would really be helpful.. then I have to try and implement it..

    I have an array of levels .. each of which have a different linked list with 3 types of data in each element. I need the linked list to sort with respect to the score that is inputed..greatest to least.. but it only can be compared to that level. So in other words.. I have an array of linked lists that need sorting... but as of right now I cant even get my data to go into the array..

    input ::

    name level score

    joe 5 400
    matt 6 300
    eric 5 450


    Can anyone help me with any of my problems listed above?? PLEASE

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you think scanf("&#37;d", &array[i]) is at all feasible?

    Why make an array of lists, plus "slist" which is the list of all the lists, when you could just insert nodes in their proper order into your main list in the first place? (You had hints on how to do that the last time you asked this question.) You've really implemented a stack (things inserted at the front and removed from the front) rather than a list.

    More things: Where do you input the other two pieces of data? And ... wait ... are you supposed to use the level piece of data to determine which of the array[i] lists it goes into? As opposed to what you have now, which is a for loop that goes through each list in turn. In that case, you definitely need to make sure you get all three pieces of data.
    Last edited by tabstop; 09-27-2008 at 10:03 PM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by tabstop View Post
    Why do you think scanf("%d", &array[i]) is at all feasible?

    Why make an array of lists, plus "slist" which is the list of all the lists, when you could just insert nodes in their proper order into your main list in the first place? (You had hints on how to do that the last time you asked this question.) You've really implemented a stack (things inserted at the front and removed from the front) rather than a list.

    More things: Where do you input the other two pieces of data? And ... wait ... are you supposed to use the level piece of data to determine which of the array[i] lists it goes into? As opposed to what you have now, which is a for loop that goes through each list in turn. In that case, you definitely need to make sure you get all three pieces of data.

    um. I dont know. I dont know what I m doing.. Its all trial and error for me.. and i was hoping people here could help me out. My teacher never lectured on this yet he expects us to have this perfect by monday. Not really good at teaching myself.. and its alot to learn in one day..

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's possible that your lecturer hasn't gone over linked lists (I'm skeptical, but it's your class), but I'm sure (s)he's expecting you to be able to think. If you can't realize that three pieces of data need three input variables, or that if you need to make a decision based on some variable you need to use that variable in the code that makes the decision instead of using some completely different variable, there's nothing I can do here that's going to help you understand linked lists, and for that matter I wish your lecturer the best of luck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  4. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM