Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

Help making a simple link list program in C

This topic is 4717 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

Can some one show me a simple dos console link list program so I can understand how they work? and even better if they can use the std::list using #include <list> that should be included in any c/c++ compiler. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Well if you have decided a linked list is the best data structure to use for your game, then using it is just a matter of adding objects to the list. Here's a trivial example for you to consider (untested code):


// list class library
#include <list>

// Cut back on tpying for now
using namespace std;

class Object
{
public:
virtual void Draw()
{
cout << "Object::Draw method" << endl;
}
virtual void Update()
{
cout << "Object::Update method" << endl;
}
virtual void Destroy()
{
cout << "Object::Destroy method" << endl;
}
};

class Enemy : public Object
{
virtual void Draw()
{
cout << "Enemy::Draw method" << endl;
}
virtual void Update()
{
cout << "Enemy::Update method" << endl;
}
virtual void Destroy()
{
cout << "Enemy::Destroy method" << endl;
}
};

class Hero: public Object
{
virtual void Draw()
{
cout << "Hero::Draw method" << endl;
}
virtual void Update()
{
cout << "Hero::Update method" << endl;
}
virtual void Destroy()
{
cout << "Hero::Destroy method" << endl;
}
};

list<Object*> EntityList;

void Init()
{
EntityList.push_back( new Hero );
EntityList.push_back( new Enemy );
EntityList.push_back( new Enemy );
}

void Run()
{
while ( EntityList.size() > 0 )
{
// Get the value of the "front" list item.
Object* tempObj = EntityList.front();

// Call the update function
tempObj->Update();

// Call the draw function
tempObj->Draw();

// Remove from the list
EntityList.pop_front();

// Push the object back on if it's not destroyed yet
EntityList.push_back( tempObj );
}
}





Ok so there's a basic example of using a list and how it would work in your game. Yes, it is not a good idea™ to pop, then push again, but this is just a quick example. Ideally, you would want to use the example at the bottom of that reference page in my first post.

// Create constant iterator for list.
list<int>::const_iterator iter;
// Iterate through list and output each element.
for (iter=lst.begin(); iter != lst.end(); iter++)
{
out_stream << (*iter) << delim;
}




That way you simply iterate though all your objects and you don't have to push/pop redundadntly. Just note of how this is a game design issue. If you do not have your game setup with polymorphism like this, then you will have to find a different design procedure to implement with linked lists.

Now, on the other hand, you *can* just do something like:

list<Hero*> HeroList;
list<Enemy*> EnemyList;

And so on, but by the time you do all that, you might as well just make a class manager for each of those to handle all of the functionality. It's up to you what you want to do in the end, good luck!

Share this post


Link to post
Share on other sites
Does any one have another way to do this? Or have an example of how to use link list in handeling game objects?

Share this post


Link to post
Share on other sites
So say that I have

team 1 that has 4 entites

team 2 that has 4 entites

has 3 artifacts

so I would make

list<Hero*> team1;
list<Enemy*> team2;
list<Object*> artifacts;

but after that what needs to be acomplished is were I think I am lost.

I think I would add top each list how ever many iteams there are for that set

then I am not sure I would use them.

Can any one clear this up for me Thanks



Share this post


Link to post
Share on other sites
i'll give you a short example of how I use a list in my program to store the locations of a bunch of trees.


list<D3DXVECTOR3> treeList;

//generate it's locations
for(int i = 0; i < 1000; ++i)
{
D3DXVECTOR3 treeSpot = D3DXVECTOR3( ((float)rand() / RAND_MAX)*300, 0, ((float)rand() / RAND_MAX)*300);
treeList.push_front(treeSpot);
}


//render the trees
list<D3DXVECTOR3>::iterator iter;
for(iter = treeList.begin(); iter != treeList.end(); iter++)
{
DWGGFXRender(pTreeMesh, (*iter).x, (*iter).y, (*iter).z );
}




you can see I create my list, then add numerous vectors to it by use of the push_front function. push_front, adds the vector to the front of the list. Then I use an iterator to iterate through the list, rendering the mesh at each x, y, and z value.

Hope this helps.

Share this post


Link to post
Share on other sites
Well, with the information given its very hard to tell you what to do, as that is specific to your needs. If you explain what you are trying to accomplish then we will be better able to aid you.

That said I will post a wild guess:
You initialized three std::lists

ist<Hero*> team1;
list<Enemy*> team2;
list<Object*> artifacts;

Now the next logical step would be to add objects to those lists (long time sense I have done anything with std::list, so bare with me)

Hero* new_hero = new Hero;
// initialize your hero
...
// add to list
team1.push_back( new_hero );

// and again (a loop would probably do this justice)
new_hero = new Hero;
...

Note that because I used new to create the Hero objects I will have to use delete on them at some point (les std::list does this for you? When in doubt delete, a runtime error will correct you if your wrong [grin])

Hero* hero = team1.pop_back();
delete hero;
// once again a loop would work wonders
hero = team1.pop_back();
delete hero;

Hope that helps.

** edit **
Dang! Beaten to posting... oh well, at least only some of the above information is duplicate…

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
Can some one show me a simple dos console link list program so I can understand how they work?



// Untested code //

class Node {
//data, often a pointer, but can be anything at all (including a full fledged class)
int data; // we’ll use an integer

Node* next;
};

Node* head = NULL;

void AddToList( int data ) {
Node* node = new Node;
node->data = data;
node->next = head;
head = node;
}

void RemoveHead() {
if( head == NULL ) return;
Node* node = head;
head = head->next;
delete node;
}

int GetHead() {
if( head != NULL ) return head->data;
else return1;
}









That is the simplest linked lists you can create (singly linked list), doubly linked lists and double ended linked lists allow you to traverse the list with greater ease, however many times they are not required. When coding linked lists by hand the KISS (keep It Simple Stupid) rule is very important, as they are notorious for creating ambiguous null pointer errors. Flowcharting your algorithms (remember data structures and algorithms class?) works wonders for defeating these errors also.

** edit **
Had a typo in the code...

[Edited by - CyberFox on June 22, 2005 4:03:01 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
then I am not sure I would use them.
The point of data structures is abstraction: being able to generically apply an operation to collections of objects rather than doing so one by one. If you have a sequence data structure, you can iterate over it and apply an operation to each element; you can find a specific element, perhaps according to certain criteria, and then apply an operation; you can add or remove elements from the sequence - at specific points if the performance characteristics of the container make it cost effective; and so forth.

The real question is why you think you need a list. What do you need to do to your objects, and is there any specific benefit to doing it to them in aggregate (as a collection) over individually.

Don't use a feature just because it's there or because everyone else does. Use it because you need it and it makes your life easier. I suggest just learning how the various Standard C++ Library containers work for intrinsic types. It'll give you a lot of insight on how you might employ them for your own objects later on.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!