Archived

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

HellRiZZer

Linked List inside linked list . PLEASE HELP..!

Recommended Posts

I''s thinking of creating a particle engine, but I''m stuck on creating and using classes as linked lists. That''s what I want to do: Create a Particle class like: struct Particle { Particle *nParticle; int PUID; AddParticle(int a); int GetInfo(int ID); }; With that I can create a linked list; But now, when I want to use a collection or a linked list inside a ParticleSystem(like snow or rain), I couldn''t implement it. Also , I wanted the ParticleSystem to be a linked list also, so I can add other PArticle Systems with use of Manager class. Please help ! This is a question of life and death. I''ve been trying to solve it for two weeks already, looked on Gamasutra and other places, but couldn''t find anything like this. Please!!

Share this post


Link to post
Share on other sites
Generally you should separate the implementation of the nodes from the implementation of the list. A generic single-linked list might look like:
    
template<class DataType>
class List
{
// the node struct, that contains data and a ptr to the next node

struct _Node
{
DataType Data; // the actual data

_Node *pNext; // ptr to the next node

//

_Node(const DataType &InData, _Node *pInNext) // ctor

: Data(InData), pInNext(pNext)
{}
};

_Node *pFirst;
public:
List() : pFirst(NULL) {}
void Add(const DataType &InData)
{
// here we add the new data at the beginning

// of the list

pFirst = new _Node(InData, pFirst);
}
// gets the n:th element

bool Get(int num, DataType &OutData)
{
_Node *pTmp = pFirst;
for(int i = 0; i < num && pTmp; i++, pTmp = pTmp->pNext);
if(pTmp)
{
OutData = pTmp->Data;
return true;
}
return false;
}
};
// use like this

int main()
{
List<int> intlist;
inlist.Add(3);
inlist.Add(2);
inlist.Add(1);
int res = 0;
if(inlist.Get(2, res))
cout << "second value is:" << res << endl;
else
cout << "no second value was found" << endl;
return 0;
}

An easier way would be to go with STL, there are loads of container (template-)classes there, and I would recommend vector in your case (or in most cases).


Edited by - amag on March 15, 2001 10:23:03 AM

Share this post


Link to post
Share on other sites
Generally what you''re looking for is implemented via a second pointer called Down. So you have mNext, which is the next item in the list, possibly mPrev for the previous item, and mDown, for a nested sublist.

Share this post


Link to post
Share on other sites
Generally I forget to get to the real conclusion, namely that in the code I describe above you could use it like this to solve your problem (will also work with STL) :
    
typedef List<int> IntList;
int main()
{
List<IntList> list;
list.Add(IntList());
list.Get(0).Add(10);
}

Well, you get the picture...anyways, it's probably easier to use STL, since then you have everything done for you .

Edited by - amag on March 16, 2001 12:56:40 PM

Share this post


Link to post
Share on other sites
Thanks to all guys who has tried toanswer my question. I very appreciate that! If the code will work, I''ll include you in post titles Don''t think it''s gonna be a great game , but still..
THANKS!!

Share this post


Link to post
Share on other sites