— javaScript — 2 min read
A linked list is a data structure with a list of objects (also called nodes) that point to other objects or null values. It's a bunch of objects within objects. To be considered a linked list, it also has to have a head and a tail. To start, we need to create a template for the node object. After that, we have to make a template for the actual linked list, and that's it.
Without going too deep into the javaScript language, below, we're just declaring a class called Node
. One thing to note is that
classes in javaScript are just special functions. This means you can do all of the same stuff by just declaring a function. To make it feel & look like object-oriented programming, you can use
the class syntax below. Inside of the class body is the constructor method. This method can only be declared once and is responsible for initializing the object instance.
1class Node{2 constructor(val){3 this.value = val4 this.next = null5 }6}
After creating the class for the node, we have to create the actual linked list structure. This is the template for the object that will hold our node objects. Or at least that's the best way I can explain it right now. You can declare the class by using the following syntax. I'll be naming this class SinglyLinkedList
since that's what we'll be constructing. After that, you have to add a constructor method to initialize an object instance. Inside of the constructor method, you have to add a head and a tail property. I'm also tracking the length of the list, so I'll be adding a length property as well. I'll also be adding one method called a push, so we can actually add some nodes.
1class SinglyLinkedList {2 constructor (){3 this.head = null;4 this.tail = null;5 this.length = 0;6 }7}
The push method allows us to add a node to the end of the list. To sum it all up, we would declare a method called push
and pass in a value as
an argument to be passed to the node object. The first thing we would do in this method is instantiate the node. After that, we ask
if the head is null (checking if the list is empty or not). If the head is null, then we need to point the head and the tail to the new node. If the head is not null, we should point the current tail next property to the new node and point the tail to the new node (making the new node the tail). After that, we will increase the list's length by one and return the list.
1//add a new node the end of the Linked List2push(val){ 3 let newNode = new Node(val);4 if(this.head === null){5 this.head = newNode;6 this.tail = this.head;7 } else {8 this.tail.next = newNode;9 this.tail = newNode;10 }11 this.length++;12 return this;13 }
1class Node{2 constructor(val) {3 this.val = val;4 this.next = null;5 }6}78class SinglyLinkedList {9 constructor (){10 this.head = null;11 this.tail = null;12 this.length = 0;13 }14 15 push(val){16 17 var newNode = new Node(val);18 if(this.head === null){19 this.head = newNode;20 this.tail = this.head;21 } else {22 this.tail.next = newNode;23 this.tail = newNode;24 }25 this.length++;26 return this;27 28 }29}30//test it31let newList = new SinglyLinkedList()32console.log(newList.push('DeAndre'))33console.log(newList.push('is'))34console.log(newList.push('a'))35console.log(newList.push('cool'))36console.log(newList.push('dude'))
Get at me on twitter.