- What is a linked list?
- Why use a linked list?
- The design of a linked list
- The LinkedList class
- Retrieving data from the list
- Removing data from a linked list
- Making the list iterable
- Using the class
What is a linked list?
A linked list is a data structure that stores multiple values in a linear fashion. Each value in a linked list is contained in its own node, an object that contains the data along with a link to the next node in the list. The link is a pointer to another node object or null
if there is no next node. If each node has just one pointer to another node (most frequently called next
) then the list is considered a singly linked list (or just linked list) whereas if each node has two links (usually previous
and next
) then it is considered a doubly linked list
. In this post, I am focusing on singly linked lists.
Why use a linked list?
The primary benefit of linked lists is that they can contain an arbitrary number of values while using only the amount of memory necessary for those values.
Though originally intended for better memory management, linked lists also became popular when developers didn’t know how many items an array would ultimately contain. It was much easier to use a linked list and add values as necessary than it was to accurately guess the maximum number of values an array might contain. As such, linked lists are often used as the foundation for built-in data structures in various programming languages.
The built-in JavaScript Array
type is not implemented as a linked list, though its size is dynamic and is always the best option to start with. You might go your entire career without needing to use a linked list in JavaScript but linked lists are still a good way to learn about creating your own data structures.
The design of a linked list
The most important part of a linked list is its node structure. Each node must contain some data and a pointer to the next node in the list. Here is a simple representation in JavaScript:
1 | class LinkedListNode { |
The following image shows the resulting data structure.
The structure of a linked list allows you to traverse all of the data by following the next
pointer on each node. Here is a simple example of how to traverse a linked list and print each value out to the console:
1 | let current = node12; |
The LinkedList
class
It’s more customary to create a class to encapsulate this functionality.
1 | const head = Symbol("head"); |
Adding new data to the list
1 | const head = Symbol('head'); |
Retrieving data from the list
1 | class LinkedList { |
Removing data from a linked list
Removing data from a linked list is a little bit tricky because you need to ensure that all next
pointers remain valid after a node is removed.
1 | class LinkedList { |
Making the list iterable
In order to be used with the JavaScript for-of
loop and array destructuring, collections of data must be iterables. The built-in JavaScript collections such as Array
and Set
are iterable by default, and you can make your own classes iterable by specifying a Symbol.iterator
generator method on the class. I prefer to first implement a values()
generator method (to match the method found on built-in collection classes) and then have Symbol.iterator
call values()
directly.
The values()
method need only do a basic traversal of the list and yield
the data that each node contains:
1 | class LinkedList { |
The values()
method is marked with an asterisk (*
) to indicate that it’s a generator method. The method traverses the list, using yield
to return each piece of data it encounters. (Note that the Symbol.iterator
method isn’t marked as a generator because it is returning an iterator from the values()
generator method.)
Using the class
Once complete, you can use the linked list implementation like this:
1 | const list = new LinkedList(); |
This basic implementation of a linked list can be rounded out with a size
property to count the number of nodes in the list, and other familiar methods such as indexOf()
. The full source code is available on GitHub at my Computer Science in JavaScript project.
Source:
https://humanwhocodes.com/blog/2019/01/computer-science-in-javascript-linked-list/