Linked List inside linked list . PLEASE HELP..!
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!!
Generally you should separate the implementation of the nodes from the implementation of the list. A generic single-linked list might look like:
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
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 thisint 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
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.
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) :
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement