Generic List Functions

This is a discussion on Generic List Functions within the C Programming forums, part of the General Programming Boards category; I have a linked list of 'ball' objects and a function to add new balls to the list. I also ...

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Generic List Functions

    I have a linked list of 'ball' objects and a function to add new balls to the list. I also have a 'letter' object, which now has its own function to add letters to that objects list. Is there anyway that I could recode this as a generic function to handle any object? It would save a lot of repetitive code.

    Code:
    /***************************************************************/
    struct ball
    {
        float x, y;
        float dx, dy;
        int rad;
        struct ball *next;
    }; typedef struct ball ball;
    
    ball *NewBall(ball **head)
    {
        ball *newnode;
        if(!(newnode = malloc(sizeof(*newnode))))
            return NULL;
        
        newnode->next=*head; 
        *head = newnode;     
        return newnode;
    }
    /***************************************************************/
    struct letter
    {
        float x, y;
    
        char ch;
        struct letter *next;
    }; typedef struct letter letter;
    
    letter *NewLetter(letter **head)
    {
        letter *newnode;
    
        if(!(newnode = malloc(sizeof(*newnode))))
            return NULL;
        
        newnode->next=*head; 
        *head = newnode;    
        return newnode;
    }
    /***************************************************************/

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    struct ball
    {
        float x, y;
        float dx, dy;
        int rad;
     }; 
    struct node
    {
       void* data;
       struct node* next;
    }
    When you add new node - you pass pointer to the data and its size...
    You will allocate corresponding memory block for data and memcpy the passed data to the allocated block.
    When you destruct the node - you will free the allocated block of data.

    In this case the list will be owner of the memory where the data is stored.
    Other way - to store only pointers and leave the calling function to bother with allocating deallocating memory for the data... It can cause less memomy overhead (you do not need to copy every data stored in the list, only pointers) but can bring a big headake while monitoring the memory...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah, maybe repeating the code for each object type would be best after all. Having them all on one giant list seems messy. Although I guess the list could be split up. I hadent really considered that originally, thanks for the explanation.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    There's always the 'pretend template' idea of multiple inclusion of the same header file, with a bunch of differing includes.
    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"

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    You could even make the add new function as generic by

    Code:
    void * AddNewNode(void *, int);
    But in this function definition you need to typecast it to appropriate struct every time. Which would he messy.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 02:19 PM
  2. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 05:35 AM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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