🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Linked Lists

Started by
6 comments, last by frizb 24 years, 6 months ago
I''ve searched all over and can''t seem to find a source to explain Linked lists in great detail. I understand the following: Each node contains a pointer representing the "next" node. If I''m simply trying to use a linked list for my ammo when a press the fire button I''m not sure what I should be linking to. Should I just be storing new x,y positions for the next bullet or should I be storing an entire PLAYER structure which contains the x,y info for the ammo? Here is what I am gathering I should be doing: //////////////////////////////////////////////////// struct PLAYER { // all player info & ammoinfo here- ie x, y , // dx, dy, etc NODE* m_node; } ////////////////////////////////// struct NODE { NODE* next; int* x_ammo; // xpos & ypos of ammo on screen int* y_ammo; } /////////////////////////////////// Or should I be scrap the x_ammo & y_ammo and change it to something else? I can''t find a decent explanation in any of my books either. If you can point me towards the right direction I''ll be happy. Thanks! Still Learning...
Still Learning...
Advertisement
Well by doing that your what shooting around player structs?
What i would do is make the bullets have there own struct. Something like this.
typedef struct bullet{
float x,y;//or ints
int type;//type of bullet it is laser,ect
struct _bullet* next;
}bullet;

typedef struct {
bullet *head;
} LIST ;

This way you can have more than one list of bullets if you need them ect.

Hope this might help alittle.

You pretty much have exactly the right idea with your sample code there. NODE is a link in the ammo list - you might want to call it something more descriptive though, so you know what you''re dealing with (like ammo_node, that way if you make another list for something else you can give it a descriptive name too).

I''m not quite sure what Zenroth is getting at in his post. Anyway, you can find an article on linked lists at C-Scene <a href=http://www.cscene.org/CS1/CS1-04.html>here</a> which should help a bit. What you need to do next is make a couple of functions, one to add items to your list, one to remove them, and perhaps one to remove ALL the items in the list (if you have a use for that). For instance, your function to add to the list could take 3 parameters - the X and Y positions of the bullet, and a pointer to the list head. Remember, if you''re going to modify the pointer to the list head, you need to pass a pointer to a pointer. This function would allocate a new node, assign the X and Y coordinates, and link it to the list. No need to use pointers to X and Y in your node structure, unless you have a reason for that. Your remove function could take a pointer to the node you wish to remove and a pointer to the list. To remove bullets going out of range you''d make a function which goes through the list, checking each X and Y coordinate. If the bullet needs to be removed it calls the remove function, passing the node that needs to be removed and the list.

I think that about explains it... post a reply if anything there needs clarification.

Good luck

Starfall
Well, this board is buggered. Not only does it no longer like HTML tags, and no longer have a link to the UBB codes, but it also will not let me edit my own post to fix it, calling it a hack attempt. If it ain''t broke, don''t fix it...

Starfall
Starfall, thanks for your explanation on the usage of linked lists. It was very enlightening =) However, your link does not work. Thanks again =)

Best regards,
Sherman
_________________ Best regards, Sherman Chin Director Sherman3D (Malaysia) Sdn Bhd www.Sherman3D.com www.AlphaKimori.com
I got the link to work.

http://www.cscene.org/CS1/CS1-04.html

Thanks. I''ll check this out. Is there anything special I need to do to get the blitting of the ammo to work right? (Using BltFast).

Still Learning...
Still Learning...
Sorry about the link Sherman, it''s the board, not the link I''m afraid. Use the one frizb posted above. It''s a pretty brief explanation. As for the blitting - just go through the linked list for each player and look at the X and Y coordinates. You could do this in the same function which checks whether they''re out of range or not. Then draw the bullet at that point if it''s valid, and remove it from the list if it''s not. I assume you''re refreshing the whole screen every frame, so you don''t need to save what''s behind the bullet.

Good luck

Starfall
Instead of writing new linked-list code every time you need it, it might be helpful to write a template class if you''re in C++ or a void * linked list structure and create wrappers for the various data types you use.

e.g.

typedef struct _tagNode {
void * data;
struct _tagNode * next;
} Node;
typedef Node * pNode;

typedef struct _tagListHeader {
pNode * head;
pNode * last;
} ListHeader;
typedef ListHeader * pListHeader;

Then if we have the function:
BOOL AddNewData(pListHeader linkedlist, void * data);

And you want to create a new linked-list type for your ammo structures simply:

BOOL AddNewAmmoData(pListHeader linkedlist, struct ammo * data) {
return AddNewData(linkedlist, data);
}

This topic is closed to new replies.

Advertisement