I'm sure you all remember dynamic arrays and linked lists from your DS/A class(es) at school. If not (and just make sure we're all on the same page), I'll quickly summarize here.
Dynamic arrays store their elements contiguously in memory. They allocate one giant chuck of memory on the heap,
and store everything one after another in that chunk of memory. This gives them good cache locality. They
reallocate if they need to add more elements than they have room for. (This piece is important, remember it for
later.) std::vector
is a C++ example of a dynamic array.
Linked Lists store their elements in nodes (each of which is a single element and a pointer to the next element).
Because each node is allocated its own chunk of memory, a linked list never has to resize like a dynamic array
does. std::forward_list
is a C++ example of a linked list. (Similar stuff applies to a doubly linked
list as well though.)
Pros:
Cons:
And that's it. Insertion being constant time for a linked list doesn't even matter since linked lists are so slow overall. (Look up benchmarks, much smarter people than me have beaten this horse to death already.) It turns out that having a cache miss every single time you look at a new element really slows you down. (And it just gets worse as CPUs get faster.)
Please stop asking this question in interviews. It's a bullshit question. The only valid answers are:
malloc
." (What year is it again?) Not: "My data-structures and algorithms prof said so but I never bothered to benchmark it..."