Thread: Is there any good way I could create a polymorphic linked list library file?

  1. #1
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288

    Is there any good way I could create a polymorphic linked list library file?

    Linked lists seem to be the most erroneous and most frequent thing I use and post about nowadays. I've been wanting to handle data structures in my own library of functions, but I'm not sure how to imitate polymorphism without too many ambiguities. I could just pass some character or string value to represent a type, but I wanted to see if there was actually something more elegant I could use before I dive in.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Were you planning on using C or C++?

    This seems best suited for C++, but I found a link on stack overflow that might be of use if you demand good ol' C: oop - How can I simulate OO-style polymorphism in C? - Stack Overflow
    Last edited by jwroblewski44; 09-10-2013 at 08:57 PM.
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Lol just use C++. And I was a die hard C fan boy too. Just use C++.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you're using C, I recommend just getting a list library like this one and using it.

    Based on what I read, you really aren't asking for polymorphism, but code reuse. C gurus have already figured this out, so basically exploit them.

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    This might help with getting you started:

    Code:
    struct node {
        void *data;
        struct node *next;
    };
    
    // assign b to a->next and return a.
    struct node *cons(struct node *a, struct node *b);
    
    // reverse each element in lst, then return a pointer to the first element.
    struct node *reverse(struct node *lst);
    
    // count the number of elements in lst.
    size_t length(const struct node *lst);
    
    // search for 'key' in lst. return a lst->data if it can be found, and a null
    // pointer otherwise.
    void *find(const void *key, const struct node *lst,
                int (*compar) (const void *, const void *));
    Basically you just leave allocation and anything to do with the 'data' member up to the code that uses the library. These functions are easy and straightforward to write, so a library isn't usually needed in practice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a simple program using linked list
    By tillu in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2011, 12:53 AM
  2. create a book library without linked lists
    By limilou in forum C Programming
    Replies: 7
    Last Post: 03-22-2011, 10:14 AM
  3. linked list noob - function create new list
    By dukysta in forum C Programming
    Replies: 5
    Last Post: 07-06-2007, 08:16 AM
  4. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  5. linked list (good example)
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 01-30-2002, 12:12 PM