DataStructures Flashcards

1
Q

When are Linked Lists preferable to arrays? (4 reasons)

A

a) You need constant-time insertions/deletions from the list (such as in real-time computing where time predictability is absolutely critical)
b) You don’t know how many items will be in the list. With arrays, you may need to re-declare and copy memory if the array grows too big
c) You don’t need random access to any elements
d) You want to be able to insert items in the middle of the list (such as a priority queue)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When are arrays preferable to linked-lists? (4 reasons)

A

a) You need indexed/random access to elements
b) You know the number of elements in the array ahead of time so that you can allocate the correct amount of memory for the array
c) You need speed when iterating through all the elements in sequence. You can use pointer math on the array to access each element, whereas you need to lookup the node based on the pointer for each element in linked list, which may result in page faults which may result in performance hits.
d) Memory is a concern. Filled arrays take up less memory than linked lists. Each element in the array is just the data. Each linked list node requires the data as well as one (or more) pointers to the other elements in the linked list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly