Linked Lists? HUH?!?

Started by
23 comments, last by Ghostface 22 years, 8 months ago
quote:Original post by Forcas
In some situations you really have to learn linked lists. It''s a lot better than making an array that''s way too big and eats up all your RAM. If you''re making a game, you''ll want as much free RAM as possible.

The bottom line is this: If you''re making an insanely large structure that will grow and shrink use linked lists. If you''re making a smaller structure that you''re pretty sure won''t surpass a certain size, use collections or dynamic arrays. If you''re making something that always stays the same size, just use an array.


I thought the same thing about size comparisons between dynamic arrays and linked lists. Then I learned something interesting. This depends on your interface with the array/linked list. In particular, this is true when your list/array stores pointers to objects, rather than the object themselves. In that case, if you use n elements, the amount of memory used by those n objects is the same, regardless of how you store the list of pointers to them. So all that matters is comparing the memory used by the linked list to the memory used by the array. Now, consider the node structure for a typical doubly linked list. It contains a pointer to its contents, a pointer to the previous, and a pointer to the next. Given that most compilers align structures to power of 2 boundries, that will usually end up being 16 bytes, and it would require 12 at minimum on a 32 bit machine. So that''s 12 * n or 16 * n bytes for the nodes. Now consider an array of pointers. Each pointer is 4 bytes. In a growing only dynamic array, the array will never be larger than double the number of elements, or 8 * n bytes. In a dynamic array that grows and shrinks, the array''s size will never be more than 4 times the number of elements, or 16 * n bytes.

Are you still sure linked lists are more memory efficient?

Ok, then, you might be wondering "what if you store the object directly in the linked list nodes and array?" Well, there''s no point storing directly into the array unless the objects are only 4 bytes. It''s still faster to iterate over an array of pointers than to iterate over a linked list. It might potentially reduce the overhead for a linked list node by 4 bytes, but that''s not likely due to the overhead imposed by aligning to a power of 2 boundry as most compilers do. Switching to a singly linked list can cut the overhead further, but the power of 2 boundry is still an issue, and it significantly removes functionality as opposed to a dynamic array or doubly linked list.

The only advantages linked lists really have over dynamic arrays are these:
1. They are better when order matters, and many elements are being inserted/removed from the middle. However, if you have to search for the insert/remove position, (balanced) binary search trees are even better than linked lists. If elements are only being inserted and removed from the ends, dynamic arrays can do better. If order doesn''t matter, dynamic arrays do better.

2. You have a HARD real-time constraint. Dynamic arrays are faster on average for the above operations. However, every O(n) operations, you have one particular insert/delete that takes O(n) time. In most situations, this doesn''t matter. When it does, linked lists are superior.

It might seem like I''m kind of against linked lists... I suppose I am. They are incredibly overused structures, and I''ve taken enough data structures courses to see that there are better structures for the majority of their common uses. That caught me by surprise when I realized it, because I''d been a big fan of linked lists before that. However, dynamic arrays are really a lot more efficient than I had thought, and I found them to be superior to linked lists in most cases.
Advertisement
Here''s a great C++ book. It''s definately work it. I have an older edition but still a great book to own. It''s written by the creator of C++. You can also search Gamedev.net or Flipcode.com for tutorials and explanations on link lists.

The C++ Programming Language Special Edition
First of all, I would like to thank everyone for their posts.

Second of all, I would like to say that I still beleive that my reason for learning how to program is perfectly valid. For those of you who read Game Developer magazine, take a look at the April 2001 issue''s "Soapbox" section (the topic of which is "Whatever Happened to the Designer/Programmer?") for further reasons that support my logic.
------------------------------"I do not feel obliged to believe that the same God who has endowed us with sense, reason, and intellect has intended us to forgo their use. " - Galileo Galilei
yeah linked lists aren''t as good as a lot of other structures but I think they are an important step in learning how to program. They are one of the first types that newbies can learn how to make. They are also a good way to learn about pointers, memory, and recursion.
Ghostface: I agree that it is better if designers at least know the basics of programming before taking up a career in designing. After all, game design requires a good detail of understanding of the whole process, of which programming is an important part (but not the only part, I stress). And designers are being called upon more and more to do basic scripting for the game, which is similar to programming in many respects.

But, be warned: most designers don''t seem to make it into the industry in an overall design role. They tend to make it up to that position either through testing, or programming, or some sort of auxiliary design (eg. map editing). So be sure to specialize in something while you work towards the designer goal.

This topic is closed to new replies.

Advertisement