The Set Class in JavaScript

Samual Sam
Updated on 15-Jun-2020 09:30:31

151 Views

Here is the complete implementation of the MySet class. Exampleclass MySet {    constructor() {       this.container = {};    }    display() {       console.log(this.container);    }    has(val) {       return this.container.hasOwnProperty(val);    }    add(val) {       if (!this.has(val)) {          this.container[val] = val;          return true;       }       return false;    }    delete(val) {       if (this.has(val)) {          delete this.container[val];          return true;       }   ... Read More

Remove Elements from a Linked List Using JavaScript

karthikeya Boyini
Updated on 15-Jun-2020 09:29:51

2K+ Views

Removing an element is very easy in a linked list. We just need to get rid of the node we want to remove, ie, lose its reference. There are 3 cases we need to consider −Removing an element from head: In this case, we can simply assign head = head.next. This way we'll lose the reference of the first element. And out head will start pointing to the second element. Removing an element from the tail: In this case, we can simply assign the node.next of second last node to be null and we'll get rid of the last element from ... Read More

The Linked List Class in JavaScript

Samual Sam
Updated on 15-Jun-2020 09:28:03

176 Views

Here is the complete implementation of the LinkedList class −  Exampleclass LinkedList {    constructor() {       this.head = null;       this.length = 0;    }    insert(data, position = this.length) {       let node = new this.Node(data);       if (this.head === null) {          this.head = node;          this.length++;          return this.head;       }       let iter = 1;       let currNode = this.head;       while (currNode.next != null && iter < position) { ... Read More

Creating a Doubly Linked List using JavaScript

Samual Sam
Updated on 15-Jun-2020 09:26:17

315 Views

Lets start by defining a simple class with a constructor that initializes the head and tail to null. We'll also define another structure on the prototype of the DoublyLinkedList class that'll represent each node in the linked list. Exampleclass LinkedList {    constructor() {       this.head = null;       this.tail = null;       this.length = 0;    } } LinkedList.prototype.Node = class {    constructor(data) {       this.data = data;       this.next = null;       this.prev = null;    } };Let's also create a display function that'll help us ... Read More

Inserting Elements to a Doubly Linked List Using JavaScript

karthikeya Boyini
Updated on 15-Jun-2020 09:24:31

531 Views

We need to create a function insert(data, position) that inserts data at given position in the linked list. We'll perform the following steps −Create a new NodeCheck if the list is empty. If it then adds the node to head and tail and return.If not, then we'll iterate to the position we want to insert it to using currElem. We iterate a linked list by making currElem equal to currElem.next. Now we change the links in the following way −Make new node point to next node in a listMake next node's previous point to the new nodeMake our node point to ... Read More

Remove Elements from a Double Linked List using JavaScript

Samual Sam
Updated on 15-Jun-2020 09:22:15

590 Views

Removing an element is very easy in a linked list. We just need to get rid of the node we want to remove, ie, lose its reference. There are 3 cases we need to consider −Removing an element from head: In this case, we can simply assign head = head.next and remove the previous link from the next element. This way we'll lose the reference of the first element. And out head will start pointing to the second element.Removing an element from the tail: In this case, we can simply assign the node.next of second last node to be null ... Read More

The Doubly Linked List Class in JavaScript

karthikeya Boyini
Updated on 15-Jun-2020 09:19:51

494 Views

Here is the complete implementation of the DoublyLinkedList Class −Exampleclass DoublyLinkedList {    constructor() {       this.head = null;       this.tail = null;       this.length = 0;    }    insert(data, position = this.length) {       let node = new this.Node(data);       // List is currently empty       if (this.head === null) {          this.head = node;          this.tail = node;          this.length++;          return this.head;       }       // Insertion at ... Read More

Circular Linked Lists in JavaScript

Samual Sam
Updated on 15-Jun-2020 09:19:04

541 Views

Circular Linked List is a variation of the Linked list in which the first element points to the last element and the last element points to the first element. Both Singly Linked List and Doubly Linked List can be made into a circular linked list.

Singly Linked List as Circular in JavaScript

karthikeya Boyini
Updated on 15-Jun-2020 09:18:26

288 Views

In the singly linked list, the next pointer of the last node points to the first node.

Doubly Linked List as Circular in JavaScript

Samual Sam
Updated on 15-Jun-2020 09:17:59

247 Views

In the doubly linked list, the next pointer of the last node points to the first node and the previous pointer of the first node points to the last node making the circular in both directions.Insertions and deletions in a circular linked list are the same as other linked lists. You just need to keep track of the last link while performing operations on either end of the linked list.You can look up and try implementing Circular linked list using Circular Linked List Algorithm as a guide.

Advertisements