![]() |
| | #1 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| linked list sort with variable struct selection field 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 | |
| | #2 |
| C++ Witch 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 | |
| | #3 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| 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 | |
![]() |
| Tags |
| field, link list, selection, sort, variable |
| Thread Tools | |
| Display Modes | |
|
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 |