Linked List From the Start
The second part of a series going through the basics of data collections that all programmers need to know.
Join the DZone community and get the full member experience.Join For Free
this post is one in a series about stuff formally trained programmers know – the rest of the series can be found here .
in the previous post on array , we saw that all read operations are θ(1), which is awesome. an important reality of programming is everything is a trade off, so when you get fast reads with array adding items when you don't know the collection size is expensive.
array growth issue example
let's say you create an array of ints, named x, and set the length to 5 (currently that is using 20 bytes). now we want to add a 6th item, so the solution is to create a second array, named y, with a larger length. if we just want to handle one more, it means y is now taking up 24 bytes of memory. then we need to do a bunch of copy operations as we copy items from x to y, which is really slow. by the end of the process, just adding one item was really expensive.
linked list to the rescue
the solution is to change the way we store the data structure in memory. with a linked list which each value is wrapped with metadata and stored separately in memory (compared to an array which stores all values in a single continuous block of memory). the reason each item is wrapped is that it then gets a pointer to the next item in the collection, so that you can still navigate through the collection.
pros and cons
the big advantage to linked list is that since the values can go anywhere in memory the collection can be expanded indefinitely until you run out of memory for very little cost, either θ(n) or θ(1). the difference is if the collection implementation keeps a pointer to the final item in or not; if it does not then it needs to navigate through each item, θ(n), and if it knows the location of the last item then it just needs just go directly to it and set its pointer to the next item.
removing and reordering items is also much faster than an array since you just need to find the items before/after and change where their pointers point to.
what is the downside then? navigation through the collection is slower than an array. for example, if we create an integer array and i want to access the fifth item can be done with simple math:
(start of array in memory) + (int size in memory * offset)
- that will give us the location of the integer value we want to read, basically an θ(1) operation.
with linked list though, i need to ask the first item where the second is; then ask the second where the third is; then ask the third where the fourth is; then ask the forth where the fifth is. so θ(n) operation for reading.
linked lists also use more memory since you aren't just storing values, we are storing the values and one or two pointers with each value. this is marginal when storing types without a constant size, like a class since an array then needs to store the pointers to the values, but it is worth remembering.
the interesting thing about linked list compared to array is that it is very flexible in its implementation. the simplest version is to just have a pointer to the first item and each item in the collection needs to point the next item. this is known as a singly linked list, as each item is linked to one other.
the linked list may also store a pointer to the last item to make adding faster.
most common implementations though use a doubly linked list where each item in the collection not only points to the next item in the collection, but also points to the previous item in the collection. at the trade off of memory (for the extra pointer) and potentially more expensive operations (like an insert now impacts two items and not just one) you gain the ability to navigate forwards and in reverse.
Published at DZone with permission of Fred Macle. See the original article here.
Opinions expressed by DZone contributors are their own.