Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,


I have two options: a) array of 100 structures b) link list of the same 100 structures.

Which option is better and why?? should I store 100 structures in the form of array or link list?

additionally, which technique is more efficient and fast?

This question was asked in an interview.

Please help me to understand.

Regards
Joy
Posted
Updated 16-Nov-13 7:09am
v2

Interesting discussion on that topic on stackoverflow[^]
and here[^]
 
Share this answer
 
If you know that you have 100 structures and it's not going to be growing, then the array will be best. The key here is that the items that are being inserted are already set. We're not adding more items at run time, we're not adding different items, the content items are set. Now, I italicized "at run time" for a reason. If you are changing the number of items in the source code, then changing the number of items might not matter.

With that given, the links in Solution 1 give us the reasons why the array will be better.
* It takes up less memory
* It allows for quick-index based random access of any element in the array
* Thanks to the Principle of Locality, it allows caching to happen which can speed up the index based random access

All that being said, it's only a 100 elements. Most of the time, it doesn't matter if you are using an array or a list. To a person looking at the screen, faster than the blink of an eye is faster than the blink of an eye. Yes, there are cases where it will matter if you are using an array or a linked list. Some of these cases are:
* You are performing a long running, brute force algorithm (maybe something with lots of recursive-backtracking).
* You are performing a crucial, real time algorithm.
* You might be working on a video game (or physics simulation), and it's running slow
* You are using the slowest machine in the world (I'm talking about something running pre ms-dos).
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 17-Nov-13 1:32am    
I up-voted with my 4. All the explanations are clear and useful, but I would say that performance could be only compared based on some knows set of operations with their weights.
—SA
List is better in sense that if records exceeded more than 100 that would be no problem in List but you can't do that in simple array. and as from memory aspect it is again better because the size of List increases as we store record. but if we create array than it will reserve memory for 100 elements no matter any value is store in any index. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900