Archived

This topic is now archived and is closed to further replies.

Link List with multiple data types

This topic is 5048 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I heard it is possible but I dont know how to do it I am trying to have a link list that can use different data types nodes. Like a link list that had int and char nodes if you understand what I am saying. Really I am trying to have a link list that can store different types of rooms class. Is doing what I ask possible and if so would anyone mind telling me how or where I can look to find the answer. Thank You.

Share this post


Link to post
Share on other sites
Are you writing your own linked list class? Subclass general node types or store unions or polymorphic object references. Are you using a pre-existing list type, e.g. the C++ std::list? Store polymorphic object references or unions.

You could surely do it with void pointers, but void pointers are dangerous; I wouldn''t use them unless absolutely necessary if there''s a typesafe alternative.

Share this post


Link to post
Share on other sites
quote:
Original post by aaron_ds
consider using templates

Templates are nice for compile-time polymorphism. As I read the OP''s question, however, he wants to dynamically store different types in the same list at the same time. If this is not the case, ignore my previous post and listen to aaron ...

Share this post


Link to post
Share on other sites
You would do something like this [in C anyways]:


struct vine{
stuff membertype;
void *data;
void *next;
};


where stuff is most likely some enumerated list of the different possible data types. Your code would walk through this list, and then check the membertype to figure out what the data pointer is.


if (membertype==INT){
intptr=(int *)foo->data;
}else if (membertype==CHAR){
charptr=(char *)foo->data;
}
// and so on.



Pretty messy, and there''s likely a *much* better way in c++.

Share this post


Link to post
Share on other sites
I never made a link list class but I am thinking on making one if it is needed. Any time I ever use link list I always just have a couple of functions that will create a new data type add it to the link list etc... I am not sure if that is a pre existing list type because I am not even sure what it is. What i know for sure the classes would be something like this
class Rooms
{
public:
char Name[20];
};

class Shops
{
public:
char Name[20];
char Items[5][20];

};
That is just a basic example of the classes I want to make. I never used templates before either so sorry if it seems like I dont know what I am talking about or anything. I am really not sure how I can explain it any further. If you all dont mind you can keep on asking me questions until you can fully understand what I am trying to do. I hope you dont mind Thank You.

Share this post


Link to post
Share on other sites
What you want is possibly something like

class Room
{
// ...

};

class Shop : public Room
{
// ...

};

// ...


std::list<Room*> rooms;
rooms.push_back(new Room(/*...*/));
rooms.push_back(new Shop(/*...*/));

// ... And don''t forget to delete them all when you''re done

Share this post


Link to post
Share on other sites
I never made a link list class but I am thinking on making one if it is needed.

You don't have to, there is one in the C++ standard library, called std::list, available through the <list> header. It is a class template, which means it has a parameter which lets you specify, at compile time, what kind of data you will store in the list. So, for example:

std::list<Rooms> list_of_rooms will create a list variable suitable for storing Rooms objects, and
std::list<Shops> list_of_shops will create a list variable suitable for storing Shops objects.

Then you can do things like:

list_of_rooms.push_back( myRoom ); // add an element at the end.

To access the elements, you use iterators , which are pointer-like variables.

std::list<Rooms>::iterator itor;
for( itor = list_of_rooms.begin(); // begin() points to the first room
itor != list_of_rooms.end(); // end() points one-past the last room
// so when we reach it, we're done.
++itor ) // move to the next list item.
std::cout << "Room name: "
<< itor->Name // use like a pointer
<< std::endl;
}

There is much more to know, about how to add/remove/splice ... you should refer to your C++ library reference for details.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)


[edited by - Fruny on February 19, 2004 5:34:37 PM]

Share this post


Link to post
Share on other sites
Thank every one for your help. I am going to try for right now using void pointers. But if I am having problems I guess I will start learning templates cause it seems like everyone thinks it will be safer. Btw is their any class template tutorials.

Share this post


Link to post
Share on other sites