Building dynamic data structures in JavaScript
We'll need two classes / factories:
LinkedList
class / factory, which will represent the full list.Node
class / factory, containing a value property and a link to the nextNode, set both as null by default.
Build the following functions in our linked list class / factory:
append(value)
adds a new node containing value to the end of the listprepend(value)
adds a new node containing value to the start of the listsize
returns the total number of nodes in the listhead
returns the first node in the listtail
returns the last node in the listat(index)
returns the node at the given indexpop
removes the last element from the listcontains(value)
returns true if the passed in value is in the list and otherwise returns false.find(value)
returns the index of the node containing value, or null if not found.toString
represents your LinkedList objects as strings, so you can print them out and preview them in the console. The format should be:( value ) -> ( value ) -> ( value ) -> null
Extra credit
insertAt(value, index)
that inserts a new node with the provided value at the given index.removeAt(index)
that removes the node at the given index.
Extra Credit Tip: When you insert or remove a node, consider how it will affect the existing nodes. Some of the nodes will need their nextNode link updated.
- How to use classes in JS to construct a linked list
- I really would love to learn C (well, there are many language I will learn, but C is just floating to the top of my mind recently)
Friday, March 8th 2024, @10:23pm This is fun. I really like this programmy computery stuff. I don't really know what that means other than the sense of wanting do more and improve. These things are really fun puzzles. It's somehow puzzle building and puzzle solving all in one! So cool! I'm only stopping tonight to give my brain rest so I can finish up tomorrow.
Dynamic data structures are fun! Who knew :D