Passing type as a parameter; generalised linked list in C?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2007
Posts: 84
Reputation: phalaris_trip is an unknown quantity at this point 
Solved Threads: 5
phalaris_trip's Avatar
phalaris_trip phalaris_trip is offline Offline
Junior Poster in Training

Passing type as a parameter; generalised linked list in C?

 
0
  #1
May 14th, 2008
I was trying to revise my linked list and implement it in C to check if there is a difference in performance... just as a sort of weird hobby..

In the absence of templates I'm trying to figure out how to pass a type as a parameter for this purpose. For example va_arg(arg, int) retrieves an int. I know it's a macro, not a function, but I still can't figure out how this is done.. if I could add this kind of functionality and then store the type somewhere in some variable then I could probably implement the linked list using void pointers.

Alternatively, would it be possible to just use void pointers and sizeof? How would one ensure that the user doesn't start mixing types within the linked list then?

Thanks for any advice, I'm really bored..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Passing type as a parameter; generalised linked list in C?

 
0
  #2
May 14th, 2008
va_args will be of no help to you because all it does is point to the beginning byte of the stack where a variable of unknown size starts. The program still has to know the data type.

This is how I impleneted a general-purpose linked list
  1. typesed struct node
  2. {
  3. void * data; // data for this node
  4. struct node* next;
  5. struct node* prev;
  6. } LINK;

To insert a node at the head of the linked list.
  1. struct node* insertHead(LINK** head, void* data)
  2. {
  3. LINK* newNode = malloc(sizeof(LINK));
  4. newNode->data = data;
  5. newNode->next = NULL;
  6. newNode->prev = NULL;
  7. if( *head == NULL)
  8. *head = newNode;
  9. else
  10. {
  11. newNode->next = *head;
  12. *head = newNode;
  13. }
  14. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: Passing type as a parameter; generalised linked list in C?

 
0
  #3
May 15th, 2008
I think the node structure should be like this:

  1. struct node {
  2. void *data;
  3. int type;
  4. struct node *next;
  5. struct node *prev;
  6.  
  7. }LINK;

where: type is used for type. For example type=1 indicates int, type=2 indicates float, type=3 indicates userdefine and so on...


and Insert function will look like this:

  1. int inser(LINK **head, void data, int type) {
  2. if(type==1) { // int
  3. LINK *newnode = (LINK*)malloc(sizeof(LINK));
  4. newnode->data = malloc(sizeof(int));
  5.  
  6. }
  7.  
  8. and so on......
  9.  
  10.  
  11.  
  12. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC