linked list sort with variable struct selection field

This is a discussion on linked list sort with variable struct selection field within the C Programming forums, part of the General Programming Boards category; If I want to write a sort function for a linked list that can use external functions for selecting between ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    linked list sort with variable struct selection field

    If I want to write a sort function for a linked list that can use external functions for selecting between criteria (depending on whether the criteria is alpahabetic or numeric), what's the best way to pass this "selection field" option in? In other words, if I pass a node* in as the head, how can I also pass in an option to indicate which struct member should be used in the sort?

    My idea was to use a pointer to the offset of the data within the struct. I'm hoping there won't be padding that causes a problem.
    Code:
    typedef struct _node {
           char field1[64];
           char field2[64];
           int field3;
           struct _node *next;
    } node;
    
    node *sortfunc (node *head, int offset, int datatype) {
           node *one=head, *two=head->next;
           char *one_a, *two_a;
           int one_n, two_n;
           if (datatype==0) {
                    one_i=&int(head+offset);      //  ???
                    result=compnumbers(one_i,two_i);
           if (datatype==1) {
                   one_a=(char*)head+offset;
                   result=comparewords(one_a, two_a);
    Last edited by MK27; 01-17-2009 at 05:58 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,780
    Consider a predicate function like what qsort() requires.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Consider a predicate function like what qsort() requires.
    I thought about using a function pointer, but then I would either still need the datatype parameter or else do a lot of casting with void pointers (since the compare function will be accepting either two ints or two strings) which would be ridiculuous. If I keep the datatype parameter, etc, I might as well select one or the other within the sort function (since I only have to deal with two possibilities). In fact I suppose even using a separate function to compare two ints is unnecessary.

    Using an offset for the data field is easy enough for both me and the compiler. I was just wondering if there was another way.
    Last edited by MK27; 01-17-2009 at 09:53 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM

Tags for this Thread


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