🎉 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 vs Arrays

Started by
0 comments, last by Qoy 24 years, 6 months ago
I am making an overhead shooter like Raptor: Call of Shadows, and Drone, and I was thinking that there are two ways I can store the enemies, and powerups, etc in the levels. When I load them from the file into memory, I can either create an array with the same number of elements as enemies in the file, and just end up having "dead" enemies that I don''t process when I process the AI, or I can create a linked list and throw away the enemies as they go off the screen (since I don''t need them once they are passed) and when they are destroyed. The linked list would save memory, but would probably slow it down a bit. What do you people think the best option would be? http://www.crosswinds.net/~uselessknowledge

Advertisement
Here''s a very unconventional idea to think about. In the situation you mentioned you want a stucture that varies in a ceratin range (say 0-10 enemies at once), but the contents vary often. You are trying to avoid the link list for which reason: because of memory allocation/deletion, or because of the extra traversal time/storage required? For the second case, you MUST realize that the array having to identify dead players and wasting ALL the space for the entire board is deffinately less efficient.
For the first case, here''s my idea. USE a linked list container, but don''t allocate and deallocate memory during the board. There are two ways to do this, assuming that you know the MAXIMUM size of the list at any one time. The first, and easiest is a ARRAY based implementation of a linked list :). You simple allocate an array of nodes, and instead of a POINTER for the next element, the node contains an INDEX. For efficiency, the INDEX of the HEAD of the list should also be stored (it will NOT be static, so should not be assumed to be 0, or you would have to MOVE items when changing the head).
The second system is to use a linked list, but to also have the list manage it''s own node allocations. You could do this by telling the list how big it should be when you construct it. IT then allocates the memory for this many nodes, and when you add a node, it uses one it''s already allocated. When you remove an item, it simply puts the node back into it''s list of availible (unused) nodes. This system allows your client that''s USING the list to benifit from a linked list (no need to traverse dead enemies, and the list stays small), but you don''t need to thrash the memory manager.
IN FACT ... you could allocate an ARRAY containing the enemy data for the entire board (so you don''t have to access the file during play), and then just store a linked list of the index''s of the ACTIVE enemies. This system works REALLY well if the enemies always become active IN ORDER, because then you can also just store the INDEX of the next enemy to activate, and activation becomes very efficient and easy.
Good Luck, If any of this is poorly explianed, or if you see a hole I missed, post it to this thread and i''ll try to explain it better, or fix my errors.

This topic is closed to new replies.

Advertisement