Javascript Articles

Page 397 of 534

Creating a linked list using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 356 Views

A linked list is a dynamic data structure where elements (nodes) are stored in sequence, with each node containing data and a reference to the next node. Let's build a complete linked list implementation in JavaScript. Basic Structure We'll start by defining a LinkedList class and a Node structure: class LinkedList { constructor() { this.head = null; this.length = 0; } } LinkedList.prototype.Node = class { constructor(data) ...

Read More

Add elements to a linked list using Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 2K+ Views

In JavaScript, adding elements to a linked list requires careful pointer manipulation to maintain the list structure. We need to create a function insert(data, position) that inserts data at the specified position. Implementation Steps Create a new Node with the provided data Check if the list is empty. If so, add the node as head and return Iterate to the desired position using a current element pointer Update the new node's next pointer to point to the current node's next Update the current node's next pointer to point to the new node Visual Representation ...

Read More

Remove elements from a linked list using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 3K+ Views

Removing an element from a linked list involves breaking the connections between nodes. There are three main scenarios to handle based on the position of the element to be removed. Three Cases for Element Removal Removing from head: Simply assign head = head.next to lose reference to the first element. Removing from tail: Set the second-to-last node's next property to null. Removing from middle: Connect the previous node directly to the node after the one being removed: prevNode.next = nodeToRemove.next. Visual Illustration Original List: ...

Read More

The Linked List Class in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 209 Views

Here is the complete implementation of the LinkedList class in JavaScript. This data structure allows you to store elements in a linear sequence where each element points to the next one. Complete LinkedList Implementation class LinkedList { constructor() { this.head = null; this.length = 0; } insert(data, position = this.length) { let node = new this.Node(data); if (this.head === null) ...

Read More

Removing Elements from a Double Linked List using Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 636 Views

Removing an element from a doubly linked list involves updating the pointers of adjacent nodes to bypass the node being removed. There are three main cases to consider based on the position of the element. Three Cases for Removal Removing from head: Update head to point to the second node and remove the previous link from the new head node. Removing from tail: Update tail to point to the second-to-last node and set its next pointer to null. Removing from middle: Connect the previous and next nodes directly, bypassing the current node by updating their pointers. ...

Read More

The Doubly Linked List class in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 543 Views

A doubly linked list is a data structure where each node contains references to both the next and previous nodes, allowing bidirectional traversal. Here's a complete implementation of a DoublyLinkedList class in JavaScript. Complete Implementation class DoublyLinkedList { constructor() { this.head = null; this.tail = null; this.length = 0; } insert(data, position = this.length) { let node = new this.Node(data); ...

Read More

Circular linked lists in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 580 Views

A Circular Linked List is a variation of the standard linked list where the first element points to the last element and the last element points back to the first element, forming a circle. Both singly and doubly linked lists can be implemented as circular structures. Data: 10 Data: 20 Data: 30 ...

Read More

Set Data Structure in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 573 Views

A Set is a built-in data structure in JavaScript that stores unique values of any type. Unlike arrays, Sets automatically prevent duplicate values and don't maintain insertion order for iteration purposes, making them ideal for storing collections of unique items. Creating a Set You can create a Set using the Set constructor: // Empty Set let emptySet = new Set(); console.log(emptySet); // Set with initial values let numbers = new Set([1, 2, 3, 4, 5]); console.log(numbers); // Set automatically removes duplicates let duplicates = new Set([1, 2, 2, 3, 3, 4]); console.log(duplicates); ...

Read More

When should you use sets in Javascript?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 231 Views

Sets in JavaScript are ideal when you need to store unique elements where order doesn't matter and you primarily need to check for membership of different objects. Sets are also useful when you want to perform operations like union, intersection, and difference similar to mathematical sets. Let's explore both the built-in ES6 Set and understand when to use it effectively. When to Use Sets Use Sets when you need: Unique values only − Automatically prevents duplicates Fast membership testing − O(1) lookup time with has() Set operations − Union, intersection, difference between collections Simple deduplication ...

Read More

Remove elements from a Set using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 266 Views

JavaScript Sets provide methods to remove elements efficiently. The primary method is delete(), which removes a specified value from the Set. Using the delete() Method The delete() method removes a value from the Set and returns true if the value existed, or false if it didn't. Syntax set.delete(value) Example: Removing Individual Elements const mySet = new Set([1, 2, 3, 4, 5]); console.log("Original Set:", mySet); // Remove elements console.log("Delete 3:", mySet.delete(3)); // true - existed console.log("Delete 10:", mySet.delete(10)); // false - didn't exist console.log("Updated Set:", mySet); console.log("Set size:", ...

Read More
Showing 3961–3970 of 5,340 articles
« Prev 1 395 396 397 398 399 534 Next »
Advertisements