NekoSan

I've just started studying linked lists and every example I've seen is different from the last. My problem is say for example that I want to make a linked lists that contains Sprite objects for my game.

Would I make one node called "Node" and store all of my types of Sprites in there Ex:

Code Block

struct Node{

Node *pNext;

DWORD placeValue;

Cat catObject;

Dog dogObject;

};

Or would I make a Node that only contains the *pNext, but define a different sub-node that tells what type of Node it is and only holds that one object. Ex:

Code Block

struct Node{

Node *pNext;

DWORD placeValue;

};

struct Node_Cat : Node{

Cat catObject;

};

Or would I have the Object act as a node itself Or something else




Re: Visual C++ General Linked Lists -- Help Declaring Nodes

Chuck the Code Monkey

I wouldn't go with either approach. I would have a parent class that all your sprite objects inherit from and then your linked list can be made up of those.

Code Block

class Sprite
{
//Define some stuff common to all sprites
};

class Cat : public Sprite
{
//Define specifig cat stuff
};

class Dog : public Sprite
{
//Define specific dog stuff
};

class Person : pubic Sprite
{
//Define specific person stuff
};


struct Node

{

Sprite obj;

Node* next;

};


I would caution you against re-inventing the wheel though. If it's something you have to implement for a programming class or project that's one thing, but if you are writing this on your own then I would strongly suggest using the linked list built in to the Standatd Template Library (STL). The STL contains a whole slew of commonly used data structors such as linked lists, stacks, queues, vetcors, etc. and is available in just about every implementation of C++ (GCC, Borland C++, Visual C++, etc.).

You can use the STL list instead like this:

Code Block

//Header file for the STL linked list

#include <list>


//Place the definitions for your sprites in a header file

#include "Sprites.h"


using namespace std;

void doStuff(void)
{

//Declare the liinked list of sprites

linkedlist<Sprite> sprites;


//Add some sprites to the linked list

sprite.push_back(Cat());

sprite.push_back(Dog());

sprite.push_back(Person());

sprite.push_back(Dog());

sprite.push_back(Person());

sprite.push_back(Person());

sprite.push_back(Person());


//Traverse the list and do some stuff with each sprite

Sprite tmp;

list<Sprite>::iterator current = sprites.begin();

while(surrent != sprites.end())

{

//The data contained in the node can be accessed by treating the

//list iterator as a pointer


//Dereference to use . notation
tmp = *current;
tmp.SomeSpriteMethod();

//or using -> notation

current->SomeSpriteMethod();


//Move to the next node in the list

++current;

}

}



I strongly recommend looking through the STL documentation from SGI. It's an excellent resourse:

"Standard Template Library Programmer's Guide"
http://www.sgi.com/tech/stl/





Re: Visual C++ General Linked Lists -- Help Declaring Nodes

NekoSan

Thanks for the help and the link. That's much needed information.

I did do the idea where you have a parent class that the sprites inherit from and the linked list was made of those, but I did something wrong and I got an error saying that I can't convert from one type to another, but thanks to you I got it working.