C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-17-2009, 06:49 PM   #1
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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);
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 01-17-2009 at 06:58 PM.
MK27 is offline   Reply With Quote
Old 01-17-2009, 10:17 PM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,364
Consider a predicate function like what qsort() requires.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 01-17-2009, 10:48 PM   #3
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 01-17-2009 at 10:53 PM.
MK27 is offline   Reply With Quote
Reply

Tags
field, link list, selection, sort, variable

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenating in linked list drater C Programming 12 05-02-2008 11:10 PM
singly linked circular list DarkDot C++ Programming 0 04-24-2007 08:55 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C Programming 3 03-04-2005 02:46 PM
Binary Search Trees Part III Prelude A Brief History of Cprogramming.com 16 10-02-2004 03:00 PM
How can I traverse a huffman tree carrja99 C++ Programming 3 04-28-2003 05:46 PM


All times are GMT -6. The time now is 06:32 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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