24
Arrays and linked lists
Sometimes you need to store a list of elements in memory. Suppose
you’re writing an app to manage your todos. You’ll want to store the
todos as a list in memory.
Should you use an array, or a linked list? Let’s store the todos in an
array first, because it’s easier to grasp. Using an array means all your
tasks are stored contiguously (right next to each other) in memory.
Now suppose you want to add a fourth task. But the next drawer is
taken up by someone else’s stuff!
It’s like going to a movie with your friends and finding a place to sit—
but another friend joins you, and there’s no place for them. You have to
move to a new spot where you all fit. In this case, you need to ask your
computer for a different chunk of memory that can fit four tasks. Then
you need to move all your tasks there.
Dostları ilə paylaş: