Implementation of Linked List in Javascript

Linked List implementation in Javascript

In this article, we are trying to implement the concept of Linked List and showed you the number of operations which we can perform over linked list.

Its one of the most popular data structure which we use in our daily programming life as it does not store data sequentially in memory.

Sometimes we struggle to understand the implementation of linked list.

Let simplify it to understand this concept.

Linked list consist of Nodes which is joined through links.

Linked List implementation in Javascript

As you can see in the above image a node consists of two parts:

  1. Data
  2. Links

Here data store the information whatever we want and link has the reference of the next node. So here we are trying we to implement the linked list and will perform some basic operation over it.

Implementation of Linked List in Javascript

To create a Node in Javascript we will need to create a class which will have two attributes called data and link.

class Node{
data;
link;
constructor(data,link){
this.data  = data;
this.link = link;
}
}

Now we are creating a variable which will point the very first node reference or null if our linked list is empty

var linkedList = null;

Here we are creating a function which will add a node in our empty linked list at the end of the linked list.

function addNode(linkedList){
let data = prompt("Enter the data to save in the node");
let node = new Node(data,null);
if(typeof linkedList == 'undefined' || linkedList == null){
linkedList = node;
}else{
let seek = linkedList;
while(seek.link != null){
seek = seek.link
}
seek.link = node;
}
return linkedList
}

This below function will traverse our linked list node one by one and will console data of each linked list node.

 function traverse(linkedList){
let seek = linkedList;
if(typeof linkedList == 'undefined' || linkedList == null){
alert("linked list is empty");
return;
}
while(seek.link != null){
console.log(seek.data);
seek = seek.link
}
console.log(seek.data);
return;
}

To insert node at particular position in our linked list

function insertNode(linkedList){
let position = prompt("enter position where node to be inserted");
position = Number.parseInt(position);
if(position < 0){ 
alert('position should be greater or equal to 0'); return;
} 
let data = prompt("Enter the data to save in the node"); 
let node = new Node(data,null); 
let counter = 0; 
if( (typeof linkedList == 'undefined' || linkedList == null) && position > 0 ){
alert('List is empty. Please add some node first');
return;
}
let seek = linkedList;
while(counter < position-1){
if(typeof seek.link == 'undefined' || seek.link == null){
alert("position is not available.Please add some node first");
return;
}
seek = seek.link
counter++;
}
if(position == 0){
node.link = seek;
window.linkedList = node;
}else{
node.link = seek.link;
seek.link = node;
}
return linkedList;
}

This function will search for data and will return position of the data in the linked list

function searchNode(linkedList){
data = prompt("enter the data to search");
if(typeof data=='undefined' || data == null){
alert("please provide valid data");
return;
}
if(typeof linkedList == 'undefined' || linkedList == null){
alert("your list is empty"); return;
}
let counter = 0;
let seek = linkedList;
do{
if(seek.data == data) {
alert("we have found the data at position: "+counter); return;
}
seek = seek.link;
counter++;
}while(seek.link != null)
alert("Sorry no data found"); return;
}

and these below two function will delete and update the node of the linked list.

function deleteNode(linkedList,type){
if(typeof linkedList == 'undefined' || linkedList == null){
alert('your list is already empty'); return
}
if(type == 'position'){
let position = prompt('Please enter the node position to delete');
position = Number.parseInt(position);
if(position < 0){
alert('position should be greater or equal to 0');
return;
}
let counter = 0;
let seek = linkedList;
let prev = null;
while(counter < position && seek != null){
prev = seek;
seek = seek.link;
counter++;
}
if(counter === position){
if(prev == null){
window.linkedList = seek.link;
}else{
prev.link = seek.link;
}
alert('data has been successfully deleted'); return
}else{
alert('Not able to find the position as list length is '+counter); return
}
}else if(type == 'data'){
let data = prompt('Please enter the node data to delete');
let seek = linkedList;
let prev = null;
do{
if(seek.data == data){
if(prev == null){
window.linkedList = seek.link;
}else{
prev.link = seek.link;
}
alert("data has been successfully deleted"); return;
}else{
prev = seek;
seek = seek.link;
}
}while(seek != null)
}
}
function updateNode(linkedList, type){
if(typeof linkedList == 'undefined' || linkedList == null){
alert('your list is empty'); return
}
if(type == 'position'){
let position = prompt('Please enter the node position to update');
let data = prompt('Please enter the data to store');
position = Number.parseInt(position);
if(position < 0){
alert('position should be greater or equal to 0');
return;
}
let counter = 0;
let seek = linkedList;
while(counter <= position && seek != null){
if(counter == position){
seek.data = data;
alert('data has been successfully updated'); return
}
seek = seek.link;
counter++;
}
alert('Not able to find the position as list length is '+counter); return
}else if(type == 'data'){
let data = prompt('Please enter the node data to update');
let sdata = prompt('Please enter the node data to store');
let seek = linkedList;
do{
if(seek.data == data){
seek.data = sdata;
alert("data has been successfully updated"); return;
}else{
seek = seek.link;
}
}while(seek != null)
alert("Not able to find the required data"); return;
}
}

I hope you found this article useful.