Like Tree1Likes
  • 1 Post By iMalc

Linked List Sorting

This is a discussion on Linked List Sorting within the C Programming forums, part of the General Programming Boards category; Hey there, I need help as i'm new in C. I have a linked list that i'm posting here but ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    108

    Linked List Sorting

    Hey there,

    I need help as i'm new in C.
    I have a linked list that i'm posting here but i need to know if it's good first.

    Secondly i want to know how to scan a group of words in one line and after pressing enter i want the words to be printed.
    I guess this has to be in my main.. no idea how to do that

    Hope i'm clear

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_WORDS 
    
    
    typedef struct info
    {
        void *data;      
        struct info *next; 
    }INFO;
    
    
    
    
    /*---------------CREATING THE NODE-------------*/
    
    
    INFO *list_create(void *data)              
    {
        INFO *node;                             
        if (!(node=malloc(sizeof(INFO))))       
            return NULL;
        node->data=data;
        node->next=NULL;                        
        return node;
    }
    
    
    /*--------------INSERTING THE NODES-------------*/
    
    
    INFO *list_insert(INFO *node, void *data)  
    {
        INFO *new_node;                         
        new_node = list_create(data);         
        new_node->next = node->next;          
        node->next = new_node;
        return new_node;
    }
    
    
    /*-------------INSERT AT BEGINNING-------------*/
    
    
    INFO *list_insert_beg(INFO *list, void *data)
    {
        INFO *new_node;
        new_node = list_create(data);       
        new_node->next = list;
        return new_node;
    }
    
    
    /*-------------REMOVING THE NODE--------------*/
    
    
    int list_remove(INFO *list, INFO *node)            
    {
        while (list->next && list->next != node)
            list = list->next;
        
        if (list->next)
        {
            list->next = node->next;
            free(node);
            return 0;
        }
        else
        {
            return -1;
        }
    }
    
    
    /*------------------------OPERATION ON LIST------------------*/
    
    
    /*----------TRAVERSING THE LIST----------------*/
    
    
    int list_trav_each(INFO *node, int(*func)(void*))
    {
        while (node)
        {
            if (func(node->data)!=0)
                return -1;
            node = node->next;
        }
        return 0;
    }
    
    
    /*-----------SEARCHING THE LIST------------*/
    
    INFO *list_find(INFO *node, int(*func)(void*,void*), void *data)
    {
        while (node)
        {
            if(func(node->data, data)>0)
                return node;
            node=node->next;
        }
        return NULL;
    }
    Thanks for the help

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You should test it to check whether it is bug free or not.
    You used void pointer, instead of char pointer, thus your list is a generic one. In other words you can store there any data you wish, but this needs you to be more careful.

    At a glance I can see you will have a memory leak, since in the remove function you free only the node and not the data.
    You should do it like this
    Code:
    if (list->next)
        {
            list->next = node->next;
            free(node->data);
            free(node);
            return 0;
        }
    Do it with this order, otherwise you will lose reference to the data and you won't be able to deallocate it. This has as result, to leave garbage in the memory. Very common mistake!

    You can do that, but it would be much easier if you typed a word, then pressed enter, type a word and again press enter.
    But if you really want to do it in one line, then you should have enough memory to read a BIG string that will be consisted of the words separated by whitespaces.

    I would suggest you to focus on the list for now. Test it with fixed data.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    It looks OK to me, but take that with a grain of salt, because I don't know a whole lot about your code.

    You should write comments explaining details about the func pointers, at the very least. What do they return, what does the return value mean, and how do you call the function pointer? All of these questions are important because they dictate to the person using the code how to write the functions you are going to use in callbacks.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    At a glance I can see you will have a memory leak, since in the remove function you free only the node and not the data.
    Actually that should be implemented in callbacks. Since it is the user's data, freeing the data is not necessarily as simple as calling free.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    list_insert inserts a node into the second position in the list which is very odd.
    It'll crash if the list is empty, as will list_remove.
    AndiPersti likes this.
    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"

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    In addition to iMalc's comment, you can't remove the first node of your list.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with sorting linked list
    By hockey8 in forum C Programming
    Replies: 1
    Last Post: 11-08-2010, 02:55 PM
  2. sorting in linked list
    By esi in forum C Programming
    Replies: 8
    Last Post: 04-24-2007, 03:03 PM
  3. linked list sorting..
    By dlcp in forum C Programming
    Replies: 7
    Last Post: 04-10-2007, 07:03 AM
  4. Sorting the linked list
    By zbap in forum C Programming
    Replies: 2
    Last Post: 12-14-2003, 05:08 PM
  5. Sorting a linked list
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2001, 05:49 PM

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