In this activity, we're going to be creating a very basic linked list. It will allow you to add to either the head or the tail, but not include any additional methods to add or delete from the list. We will be adding those in a later code drill.
-
Copy your
Node.jsfile from the previous activity into this folder. OurLinkedList.jsfill will be importing it and using the constructor you created.- If you had trouble with the
Nodeconstructor, there is a completed one in thehintfolder you can use. - For this activity, you should not access the
valueandnextof aNodeand operate on them directly. Instead, use the methods provided by theNodeconstructor in order to manipulate the values of the node.
- If you had trouble with the
-
Within the
LinkedList.jsfile, fill out theLinkedListconstructor function by following the instructions below. -
Create a constructor function for a new
LinkedListobject that optionally takes in a value to instantiate theLinkedListwith- The
LinkedListobject needs to keep track of theheadnode and thetailnode.- The
headnode is the start of the linked list. - The
tailnode is the end of the linked list. Reaching this point means we've reached the end of the iteration. - If there are no nodes in the linked list, both the
headand thetailshould be null. - If there is only one node, the
headand thetailshould both point to it since it is both the start and end of the list.
- The
- The
LinkedListobject also needs to keep track of the length of the list. - If the user doesn't pass in a value when instantiating the list, then the
headandtailshould point to null and thelengthis 0 - If the user does pass in a value when instantiating the list, then the
headandtailshould both point to a newNodeobject made with that value.lengthwill be 1 in this case since there is one node in list.
- The
-
Create a
getListHeadmethod that returns theheadof the list -
Create a
getListTailmethod that returns thetailof the list -
Create a
getListLengthmethod that returns thelengthof the list, which is also the number of nodes in the list -
Create a
insertAtHeadmethod that takes in a value, creates a new node with that value, and inserts that node before theheadnode.- This should update the
headto point at the new node and increment the length of the list. - The new node should be updated to point at the previous head.
- If there was no
head, then set the new node to be both theheadand thetail - Increment
lengthafterwards - This method should return
thisso we can chain methods together such ascurrLinkedList.insertAtHead(5).insertAtHead(9)
- This should update the
-
Create a
insertAtTailmethod that takes in a value, creates a new node with that value, and inserts that node after thetailnode.- The
tailshould be updatede to point at the new element. - This should update the
tailto point at the new node and increment the length of the list. - Increment
lengthafterwards - This method should return
thisso we can chain methods together such ascurrLinkedList.insertAtTail(3).insertAtTail(7)
- The