Linked Lists are a data structure consisting of multiple nodes that contain the data we're keeping track of. Each node is "linked" to another node, meaning that it either points to or has another node pointing to it. This way, when someone has access to the first element of a linked list, they can traverse the entire list and see every element within that list.
-
In this series of activities, we're going to be building a linked list and all its associated methods up from scratch.
- Within the
Node.jsfile, fill out theNodeconstructor function by following the instructions below.
- Within the
-
The first step is to construct the
Nodesof a linked list. We will be using constructor functions in order to do this. -
Create the constructor function for a new
Nodeobject that optionally takes in a value to instantiate theNodewith.- The
Nodeobject needs to keep track of it'svaluealong with a pointer to thenextelement of the linked list. - The
valueshould be null for now unless the constructor function is called with an argument, in which case we use the argument as thevalueinstead of the default null nextwill be null until we link thisNodeto another one.
- The
-
Next, we're going to add some methods to our
Nodeobject so we're not operating directly on the data stored inside it.- Add a
getValuemethod that returns the value of our node - Add a
getNextmethod that returns the nodenextpoints to - Add a
setValuemethod that stores the argument insidevalue- If this isn't passed an argument, it should set
valueto be null
- If this isn't passed an argument, it should set
- Add a
setNextmethod that stores the node passed to it as argument- If this isn't passed an argument, it should set
setNextto be null
- If this isn't passed an argument, it should set
- Add a