In this post , I will be showing how we can build a singly linked list data structure using javascript language.We will also learn all operations on it.
🧐Overview
In a singly linked list each item points to its successor.Each item in a linked list is called as a node.There is a head node in each linked list which points to its first node.The last node of linked list points to null.
Node Class
First we will create a blue print for our node of linked list.We will create a Node Class in javascript which will have two properties-
- data - which will store data of node
- next - which will points to next node
class Node {
constructor(data, next = null) {
this.data = data;
this.next = next;
}
}
Linked List Class
Now we will create a actual linked list class which will have two properties -
- head -which will points to the first node of linked list
- size -which represents the number of nodes present in the list
class LinkedList {
constructor() {
this.head = null;
this.size = 0;
}
}
Operations
Now , we will create some member functions in LinkedList class to perform various operations on linked list.
(1) insertFirst()
To insert a node at first position of list.
Here we will simply point the head to new node and next of new node to previous head node . At the end , we will increment the size of list by 1.
// Insert Node at first position
insertFirst(data) {
this.head = new Node(data, this.head);
this.size++;
}
(2) insertLast()
To insert a node at last position of list.
Here we will have 2 case
1.If list is empty - then we will simply point the head of node to new node.
2.if list is not empty - then we will traverse the whole list and then point the next of last node to new node.
// Insert Node at last position
insertLast(data) {
const newLast = new Node(data);
// Check if list is empty then last node is first node which will be head
if (!this.head) {
this.head = newLast;
}
else {
// if list is not empty traverse to last node
let last = this.head;
while (last.next) {
last = last.next;
}
last.next = newLast;
}
this.size++;
}
(3) insertAt()
To insert a node at given index.
Here also we will have 2 case
1.If index is 0 - then we will simply point the head of node to new node.
2.if index is not 0 - then we will create two variables to track previous and current node and will traverse the list upto given index and and then point the next of previous node to new node and next of new node to current node.
// insert a node at a particular index
insertAt(data, index) {
// check if index is valid
if (index >= 0 && index < this.size) {
// if first index
if (index === 0) {
this.head = new Node(data, this.head);
return;
}
const node = new Node(data);
let current, previous;
current = this.head;
let count = 0;
while (count < index) {
previous = current;
count++;
current = current.next;
}
node.next = current;
previous.next = node;
this.size++;
}
else {
console.log('You have entered an invalid index!!');
}
}
And that's it for this one. To see how we can write remaining four operations which are
4.removeAt() - To remove a node at given index.
5.getAt() - To get data of a node at given index.
6.clearList() - To clear (empty) a whole list.
7.printListData() - To print data of all nodes in list
visit satishnaikawadi.me
I hope 😇 all of you understand how to build linked list with javascript. Thank you for reading.For any queries , feel free to ask.
Top comments (3)
No , its not like java. It can be both . You can keep them in seperate files or in same file depending upon how you like it.
Correct.