Introduction to JavaScript list
A data structure used to store multiple values in a linear manner is called Linked list in JavaScript and node is an object in the linked list which consists of data along with the link to the next node in the list. Where link is the pointer to the next node in the list or if there is no next node in the list, it is null and if there is just one pointer in each node pointing to the next node in the list, then the list is called singly linked list or if there are two pointers in each node pointing to the prey node in the list and next node in the list, then the list is called doubly linked list.
Syntax of JavaScript list:
class Nodename
{
constructor(datavalue)
{
this.datavalue = datavalue;
this.next = null;
}
}
const head = new Nodename(datavalue);
Where,
- class Nodename is the structure of the node with datavalue is the data to be stored in the node and next is the pointer pointing to the next node in the list if it is present or it points to null.
- head is the first node in the list.
Working of JavaScript list
- Whenever there is a need to store multiple values in a linear manner in JavaScript, we make use of a data structure called Linked list.
- A node is an object in linked list which consists of data along with the link to the next node in the list where link is the pointer to the next node in the list or if there is no next node in the list, it is null.
- If there is just one pointer in each node pointing to the next node in the list, then the list is called singly linked list.
- If there are two pointers in each node pointing to the prev node in the list and next node in the list, then the list is called doubly linked list.
The methods that can be used while working with Linked list in JavaScript are:
1. insert(data)
insert(data) method is used to add the given element at the end of the list.
The code to implement the insert(data) method works as follows:
insert(data) is defined to add new elements to the list within which a node is created and the head node is checked if it is empty and if it is true, the created node is assigned to the head node and an element is inserted into it and the pointer from this node is made to point to the next node in the list.
2. remove(data)
remove(data) method is used to remove the given element from the end of the list. The removed element is returned by this method or -1 is returned by this method if there is no element present in the list.
The code to implement the remove(data) method works as follows:
remove(data) method is defined to remove the given element from the list. The list is iterated over the length of the list to find the given element and once the element is found, it is removed from the list.
3. display()
display() method is used to display the elements in the list.
The code to implement the display() method works as follows:
The function display is used to display the elements in the list which works by displaying each element and pointing the pointer to the next node while traversing to the end of the list.
Example of JavaScript list
JavaScript program to demonstrate Linked List in JavaScript to add an element at the end of the list, delete an element from the list and to display the elements of the list.
Code:
//creating a data structure for linked list which initializes the head and length of the list to null
class LinkedList
{
constructor()
{
this.head = null;
this.length = 0;
}
//a function called insert is defined to insert an element into the list
insert(element, pos = this.length)
{
//a new node is created and checking if the node is null and if its true, it is made the head of the node
let node = new this.Node(element);
if (this.head === null)
{
this.head = node;
this.length++;
return this.head;
}
let iteration = 1;
let currentNode = this.head;
//checking if the current node is not null and the variable iteration is less than the length of the list
while (currentNode.next != null && iteration < pos)
{
currentNode = currentNode.next;
iteration++;
}
//assigning the next node of the current node to the new node consisting of element
node.next = currentNode.next;
currentNode.next = node;
this.length++;
return node;
}
//a function called remove is defined to remove the element from the list
remove(element, pos = 0)
{
//checking if the length of the list is zero or not
if (this.length === 0)
{
console.log("The list do not contain items");
return;
}
this.length--;
let currentNode = this.head;
if (pos <= 0)
{
this.head = this.head.next;
}
else if (pos >= this.length - 1)
{
while (currentNode.next.next != null)
{
currentNode = currentNode.next;
}
currentNode.next = null;
}
else
{
let iteration = 0;
//a variable called iteration is chosen and checked if the position of less than the iteration
while (iteration < pos)
{
currentNode = currentNode.next;
iteration++;
}
currentNode.next = currentNode.next.next;
}
return currentNode;
}
//a function called display is defined to display the elements in the list
display()
{
let currentNode = this.head;
while (currentNode != null)
{
console.log(currentNode.element);
currentNode = currentNode.next;
}
}
}
LinkedList.prototype.Node = class
{
constructor(element)
{
this.element = element;
this.next = null;
}
}
//creating an instance of the linked list and inserting the elements into the list, deleting the elements from the list and displaying the elements
var rivers = new LinkedList();
rivers.insert("Ganga");
rivers.insert("Yamuna");
rivers.insert("Kaveri");
rivers.display();
rivers.remove("Ganga");
rivers.display();
Output:
In the above program, a data structure to create a Linked List is defined within which the head node and the length of the list is initialized. Then a function called insert is defined to add new elements to the list within which a node is created and the head node is checked if it is empty and if it is true, the created node is assigned to the head node and element is inserted to it and the pointer from this node is made to point to the next node in the list. Then a function called remove is defined to remove the given element from the list. The list is iterated over the length of the list to find the given element and once the element is found, it is removed from the list. The function display is used to display the elements in the list.
Recommended Articles
This is a guide to JavaScript list. Here we discuss the introduction, working of JavaScript list along with examples respectively. You may also have a look at the following articles to learn more –