Linked List and Void Pointers
Im trying to create linked list code without having a certain structure that it is based on. eg. normally (in textbooks) they show you to make a linked list structure that is already preplanned especially for your project.
for example:
struct NODE{
NODE *Next;
SOMESTRUCTURE Data;
} *Head;
I am trying to make it so that the "SOMESTRUCTURE" does not have to be defined in the linked list code itself. I am trying to do this using void pointers. This way, the code is not limited to a specific type of linked list. So I could have a linked list of type OBJECTS and of the type MISSILES.
this is how I initiallize the linked lists
void *InitList( unsigned int Size )
{
void *LinkList;
int n = Size / sizeof(char); // how many bytes in Size
LinkList = new char[n];
return LinkList;
}
Can this be done? I came into some problems when accessing elements of the nodes. I couldnt cast a void to a temp stucture.
Anyway, does anyone have any ideas how to make it better? Has anyone tried this before?
Edited by - owie on February 28, 2001 6:51:21 PM
You can either make your own template structure or use the STL list class, also a template class for just this kinda thing.
Harry.
Harry.
Harry.
Oh just to make it clear, the template structure would be something like this:
template <class NODE>
struct NODE{
NODE *Next;
SOMESTRUCTURE Data;
} *Head;
You could use template <class NODE, class SOMESTRUCTURE> if you wanted Data to be a different type too.
Harry.
[edited - replaced the < and > bits with < and >]
Edited by - HarryW on February 28, 2001 7:13:47 PM
template <class NODE>
struct NODE{
NODE *Next;
SOMESTRUCTURE Data;
} *Head;
You could use template <class NODE, class SOMESTRUCTURE> if you wanted Data to be a different type too.
Harry.
[edited - replaced the < and > bits with < and >]
Edited by - HarryW on February 28, 2001 7:13:47 PM
Harry.
|
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
quote:
Original post by HarryW
Oh just to make it clear, the template structure would be something like this:
template
struct NODE{
NODE *Next;
SOMESTRUCTURE Data;
} *Head;
[edited - replaced the < and > bits with < and >]
Edited by - HarryW on February 28, 2001 7:13:47 PM
Just a quick q, how is this going to work? I always thought that the data should be the one that''s the template. As in
|
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement