This is a 2-part post on linked lists. In this first part, we shall focus on explanation, visualization, and use cases, while in the next part we shall implement a linked list in javascript. So let's get into it.
Linked List
A linked list is a linear data structure whose nodes/elements are stored randomly in memory, unlike an array whose elements are stored in a contiguous (side-by-side) manner in memory. Each node in a linked list has a pointer to the next node in the list; This makes it possible to find the nodes even though they are not contiguous in memory.
This manner of randomly storing data in memory makes inserting and deleting data much faster than in other linear data structures like arrays.
Below is a representation of memory on a computer. Let's assume each block can store one node/element.
Say we have an array with 7 elements, they would look like this in memory.
Remember that we mentioned above that arrays store their data contiguously in memory; this allows direct accessing of elements using their index in the array. A linked list, on the other hand, can only access its data sequentially, meaning that to get to the 4th, 10th, or 100th node we must first start from the 1st node and work our way forward. Because each node is stored randomly in memory, only through the nodes preceding the node we're looking for can we find it.
A Linked List with 7 nodes could look like this in memory.
This manner of storing data randomly in memory has its benefits, as we mentioned earlier, but in order to better understand why, let's look at how arrays add new elements to memory.
Remember the array from before, say we wanted to increase the number of elements from 7 to 9. The new elements can't just be added to the end of the array because we don't know if those spaces in memory are occupied or not.
So what happens instead is that the elements of the array are copied to a place in memory that can hold 9 nodes, before the new nodes are added in and the old array is deleted.
This cycle is the same regardless of the number of elements in the array, could be 10, 100, or 1000 doesn't matter; This cycle will always repeat when new elements are added to the array. As you can already imagine this situation can get pretty expensive timewise.
Adding new nodes to a linked list is much faster because they can be placed in any free space in memory and don't need to be side by side. so adding 2 new nodes to our previous linked list would go like this.
As we can see, the process is a lot faster compared to arrays.
Now that we understand why linked lists are useful, let's take a proper look at the structure of a linked list.
Structure Of Linked List
A linked list is a collection of nodes that contain data and a pointer to the next node on the list. From this definition, we can infer that each node in a linked list is divided into 2 parts the data and a pointer to the next node.
A linked list keeps track of the first node in the list, which is known as the head. The node at the end of a linked list points to null
.
Types Of Linked List
The above image is a kind of linked list known as a Singly-Linked List. This is a kind of linked list whose pointers all go in one direction. This linked list can only be traversed forwards, not backwards.
There are 2 other kinds of linked lists the Doubly-Linked List and Circular-Linked List.
Doubly-Linked List
A doubly-linked list is a list that can be traversed both forwards and backwards.
Each node in a doubly-linked list has an extra pointer to the node before it, except the head and last node which point to null
.
Circular Linked List
A circular linked list is a doubly-linked list with no nodes pointing to null
. Instead, the prev pointer of the head node points to the last node on the list, while the next pointer of the last node points to the head of the list.
Use Cases
A linked list can be used in a lot of ways; some of which include:
Building Stacks and Queue data structures.
Efficiently make use of memory in your application.
Build audio/video player and applications that use next and previous buttons.
Undo/Redo functions in word editors and other development applications.
Web browser history tracker.
In my next post, we'll look at how to create a linked list in Javascript and some of the common methods of a linked list. Thanks for reading.