Implementation of LinkedList in Javascript
Last Updated :
29 Dec, 2023
In this article, we will be implementing the LinkedList data structure in Javascript. LinkedList is the dynamic data structure, as we can add or remove elements at ease, and it can even grow as needed. Just like arrays, linked lists store elements sequentially, but don’t store the elements contiguously like an array.
Example 1: In the below code example, we define a class Node having two properties: element and next. Element holds the data of a node while next holds the pointer to the next node, which is initialized to the null value.
Javascript
insertAt(element, index)
{
if (index < 0 || index > this .size)
return console.log( "Please enter a valid index." );
else {
let node = new Node(element);
let curr, prev;
curr = this .head;
if (index == 0) {
node.next = this .head;
this .head = node;
} else {
curr = this .head;
let it = 0;
while (it < index) {
it++;
prev = curr;
curr = curr.next;
}
node.next = curr;
prev.next = node;
}
this .size++;
}
}
|
Example 2: This example, shows a Linked List class with a constructor and a list of methods to be implemented. Linked List class has two properties: i.e. head and size, where the head stores the first node of a List, and size indicates the number of nodes in a list.
Javascript
removeFrom(index)
{
if (index < 0 || index >= this .size)
return console.log( "Please Enter a valid index" );
else {
let curr, prev, it = 0;
curr = this .head;
prev = curr;
if (index === 0) {
this .head = curr.next;
} else {
while (it < index) {
it++;
prev = curr;
curr = curr.next;
}
prev.next = curr.next;
}
this .size--;
return curr.element;
}
}
|
Let’s implement each of these functions:
1. add(element) – It adds an element at the end of the list.
Example: Below is the code example.
Javascript
removeElement(element)
{
let current = this .head;
let prev = null ;
while (current != null ) {
if (current.element === element) {
if (prev == null ) {
this .head = current.next;
} else {
prev.next = current.next;
}
this .size--;
return current.element;
}
prev = current;
current = current.next;
}
return -1;
}
|
In the order to add an element at the end of the list we consider the following :
- If the list is empty then add an element and it will be head
- If the list is not empty then iterate to the end of the list and add an element at the end of the list
2. insertAt(element, index) – It inserts an element at the given index in a list.
Example: Below is the code example.
Javascript
indexOf(element)
{
let count = 0;
let current = this .head;
while (current != null ) {
if (current.element === element)
return count;
count++;
current = current.next;
}
return -1;
}
|
In order to add an element at the given index of the list we consider three conditions as follows:
- if the index is zero we add an element at the front of the list and make it head
- If the index is the last position of the list we append the element at the end of the list
- if the index is between 0 or size – 1 we iterate over to the index and add an element at that index
3. removeFrom(index) – It removes and returns an element from the list from the specified index
Example: Below is the code example.
Javascript
isEmpty()
{
return this .size == 0;
}
|
In order to remove an element from the list we consider three conditions:
- If the index is 0 then we remove the head and make the next node head of the list
- if the index is size – 1 then we remove the last element from the list and make prev the last element
- if it’s in between 0 to size – 1 we remove the element by using prev and the current node
4. removeElement(element) – This method removes element from the list. It returns the removed element, or if it’s not found it returns -1.
Example: Below is the code example.
Javascript
size_of_list()
{
console.log( this .size);
}
|
The above method is just a modification of removeFrom(index), as it searches for an element and removes it, rather than removing it from a specified location
Helper Methods
Let’s declare some helper methods which are useful while working with LinkedList.
1. indexOf(element) – it returns the index of a given element if the element is in the list.
Example: Below is the code example.
Javascript
printList()
{
let curr = this .head;
let str = "" ;
while (curr) {
str += curr.element + " " ;
curr = curr.next;
}
console.log(str);
}
|
In this method, we iterate over the list to find the index of an element. If it is not present in the list it returns -1 instead.
2. isEmpty() – it returns true if the list is empty.
Example: Below is the code example.
Javascript
let ll = new LinkedList();
console.log(ll.isEmpty());
ll.add(10);
ll.printList();
console.log(ll.size_of_list());
ll.add(20);
ll.add(30);
ll.add(40);
ll.add(50);
ll.printList();
console.log( "is element removed ?" + ll.removeElement(50));
ll.printList();
console.log( "Index of 40 " + ll.indexOf(40));
ll.insertAt(60, 2);
ll.printList();
console.log( "is List Empty ? " + ll.isEmpty());
console.log(ll.removeFrom(3));
ll.printList();
|
In this method we check for the size property of the LinkedList class, and if it’s zero then the list is empty.
3. size_of_list() – It returns the size of list
Example: Below is the code example.
Javascript
class Node {
constructor(element) {
this .element = element;
this .next = null
}
}
class LinkedList {
constructor() {
this .head = null ;
this .size = 0;
}
add(element) {
let node = new Node(element);
let current;
if ( this .head == null )
this .head = node;
else {
current = this .head;
while (current.next) {
current = current.next;
}
current.next = node;
}
this .size++;
}
insertAt(element, index) {
if (index < 0 || index > this .size)
return console.log( "Please enter a valid index." );
else {
let node = new Node(element);
let curr, prev;
curr = this .head;
if (index == 0) {
node.next = this .head;
this .head = node;
} else {
curr = this .head;
let it = 0;
while (it < index) {
it++;
prev = curr;
curr = curr.next;
}
node.next = curr;
prev.next = node;
}
this .size++;
}
}
removeFrom(index) {
if (index < 0 || index >= this .size)
return console.log( "Please Enter a valid index" );
else {
let curr, prev, it = 0;
curr = this .head;
prev = curr;
if (index === 0) {
this .head = curr.next;
} else {
while (it < index) {
it++;
prev = curr;
curr = curr.next;
}
prev.next = curr.next;
}
this .size--;
return curr.element;
}
}
removeElement(element) {
let current = this .head;
let prev = null ;
while (current != null ) {
if (current.element === element) {
if (prev == null ) {
this .head = current.next;
} else {
prev.next = current.next;
}
this .size--;
return current.element;
}
prev = current;
current = current.next;
}
return -1;
}
indexOf(element) {
let count = 0;
let current = this .head;
while (current != null ) {
if (current.element === element)
return count;
count++;
current = current.next;
}
return -1;
}
isEmpty() {
return this .size == 0;
}
size_of_list() {
console.log( this .size);
}
printList() {
let curr = this .head;
let str = "" ;
while (curr) {
str += curr.element + " " ;
curr = curr.next;
}
console.log(str);
}
}
let ll = new LinkedList();
console.log(ll.isEmpty());
ll.add(10);
ll.printList();
console.log(ll.size_of_list());
ll.add(20);
ll.add(30);
ll.add(40);
ll.add(50);
ll.printList();
console.log( "is element removed ?" + ll.removeElement(50));
ll.printList();
console.log( "Index of 40 " + ll.indexOf(40));
ll.insertAt(60, 2);
ll.printList();
console.log( "is List Empty ? " + ll.isEmpty());
console.log(ll.removeFrom(3));
ll.printList();
|
3. printList() – It prints the contents of the list.
Example: Below is the code example.
In this method, we iterate over the entire list and concatenate the elements of each node and print it.
Note: Different helper methods can be declared in the LinkedList class as required.
Implementation
Now, let’s use the LinkedList class and its different methods described above.
Example: Below is the code example.
Javascript
let ll = new LinkedList();
console.log(ll.isEmpty());
ll.add(10);
ll.printList();
console.log(ll.size_of_list());
ll.add(20);
ll.add(30);
ll.add(40);
ll.add(50);
ll.printList();
console.log( "is element removed ?" + ll.removeElement(50));
ll.printList();
console.log( "Index of 40 " + ll.indexOf(40));
ll.insertAt(60, 2);
ll.printList();
console.log( "is List Empty ? " + ll.isEmpty());
console.log(ll.removeFrom(3));
ll.printList();
|
Example: Below is the Complete Code.
Javascript
class Node {
constructor(element) {
this .element = element;
this .next = null
}
}
class LinkedList {
constructor() {
this .head = null ;
this .size = 0;
}
add(element) {
let node = new Node(element);
let current;
if ( this .head == null )
this .head = node;
else {
current = this .head;
while (current.next) {
current = current.next;
}
current.next = node;
}
this .size++;
}
insertAt(element, index) {
if (index < 0 || index > this .size)
return console.log( "Please enter a valid index." );
else {
let node = new Node(element);
let curr, prev;
curr = this .head;
if (index == 0) {
node.next = this .head;
this .head = node;
} else {
curr = this .head;
let it = 0;
while (it < index) {
it++;
prev = curr;
curr = curr.next;
}
node.next = curr;
prev.next = node;
}
this .size++;
}
}
removeFrom(index) {
if (index < 0 || index >= this .size)
return console.log( "Please Enter a valid index" );
else {
let curr, prev, it = 0;
curr = this .head;
prev = curr;
if (index === 0) {
this .head = curr.next;
} else {
while (it < index) {
it++;
prev = curr;
curr = curr.next;
}
prev.next = curr.next;
}
this .size--;
return curr.element;
}
}
removeElement(element) {
let current = this .head;
let prev = null ;
while (current != null ) {
if (current.element === element) {
if (prev == null ) {
this .head = current.next;
} else {
prev.next = current.next;
}
this .size--;
return current.element;
}
prev = current;
current = current.next;
}
return -1;
}
indexOf(element) {
let count = 0;
let current = this .head;
while (current != null ) {
if (current.element === element)
return count;
count++;
current = current.next;
}
return -1;
}
isEmpty() {
return this .size == 0;
}
size_of_list() {
console.log( this .size);
}
printList() {
let curr = this .head;
let str = "" ;
while (curr) {
str += curr.element + " " ;
curr = curr.next;
}
console.log(str);
}
}
let ll = new LinkedList();
console.log(ll.isEmpty());
ll.add(10);
ll.printList();
console.log(ll.size_of_list());
ll.add(20);
ll.add(30);
ll.add(40);
ll.add(50);
ll.printList();
console.log( "is element removed ?" + ll.removeElement(50));
ll.printList();
console.log( "Index of 40 " + ll.indexOf(40));
ll.insertAt(60, 2);
ll.printList();
console.log( "is List Empty ? " + ll.isEmpty());
console.log(ll.removeFrom(3));
ll.printList();
|
Output
true
10
1
undefined
10 20 30 40 50
is element removed ?50
10 20 30 40
Index of 40 3
10 20 60 30 40
is List Empty ? false
30
10 20 60 40
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
Share your thoughts in the comments
Please Login to comment...