From 93550020b3d0aa19b7a863b54c7a1d3451727ad4 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Mon, 9 Oct 2023 23:50:40 +0530 Subject: [PATCH] Update CircularDoublyLinkedList.js --- .../Linked-List/CircularDoublyLinkedList.js | 142 +++++++++--------- 1 file changed, 67 insertions(+), 75 deletions(-) diff --git a/Data-Structures/Linked-List/CircularDoublyLinkedList.js b/Data-Structures/Linked-List/CircularDoublyLinkedList.js index 23709df946..72e266decf 100644 --- a/Data-Structures/Linked-List/CircularDoublyLinkedList.js +++ b/Data-Structures/Linked-List/CircularDoublyLinkedList.js @@ -5,10 +5,10 @@ class Node { * Creates a new Node with the given element. * @param {*} element - The element to be stored in the node. */ - constructor (element) { - this.element = element - this.next = null - this.prev = null + constructor(element) { + this.element = element; + this.next = null; + this.prev = null; } } @@ -16,33 +16,10 @@ class CircularDoublyLinkedList { /** * Creates an empty Circular Doubly Linked List. */ - constructor () { - this.length = 0 - this.head = null - this.tail = null - } - - /** - * Appends an element to the end of the list. - * @param {*} element - The element to be appended. - */ - append (element) { - const node = new Node(element) - - if (!this.head) { - this.head = node - this.tail = node - node.next = node // Circular reference - node.prev = node // Circular reference - } else { - node.prev = this.tail - node.next = this.head - this.tail.next = node - this.head.prev = node - this.tail = node - } - - this.length++ + constructor() { + this.length = 0; + this.head = null; + this.tail = null; } /** @@ -51,76 +28,91 @@ class CircularDoublyLinkedList { * @param {*} element - The element to be inserted. * @returns {boolean} - True if the insertion was successful, false otherwise. */ - insert (position, element) { + insertAt(position, element) { if (position >= 0 && position <= this.length) { - const node = new Node(element) - let current = this.head - let previous = null - let index = 0 + const node = new Node(element); + let current = this.head; + let previous = null; + let index = 0; if (position === 0) { - node.next = current - node.prev = this.tail - this.head = node - current.prev = node - this.tail.next = node + if (!this.head) { + this.head = node; + this.tail = node; + node.next = node; // Circular reference + node.prev = node; // Circular reference + } else { + node.next = current; + node.prev = this.tail; + this.head = node; + current.prev = node; + this.tail.next = node; + } } else { while (index++ < position) { - previous = current - current = current.next + previous = current; + current = current.next; } - node.next = current - node.prev = previous - previous.next = node - current.prev = node + node.next = current; + node.prev = previous; + previous.next = node; + current.prev = node; if (position === this.length) { - this.tail = node + this.tail = node; } } - this.length++ - return true + this.length++; + return true; } else { - return false + return false; } } + /** + * Appends an element to the end of the list. + * @param {*} element - The element to be appended. + */ + append(element) { + return this.insertAt(this.length, element); + } + /** * Removes and returns the element at the specified position. * @param {number} position - The position of the element to be removed. * @returns {*} - The removed element, or null if the position is invalid. */ - removeAt (position) { + removeAt(position) { if (position >= 0 && position < this.length) { - let current = this.head - let previous = null - let index = 0 + let current = this.head; + let previous = null; + let index = 0; if (position === 0) { - this.head = current.next - this.head.prev = this.tail - this.tail.next = this.head + this.head = current.next; + this.head.prev = this.tail; + this.tail.next = this.head; if (this.length === 1) { - this.tail = null + this.tail = null; } } else { while (index++ < position) { - previous = current - current = current.next + previous = current; + current = current.next; } - previous.next = current.next - current.next.prev = previous + previous.next = current.next; + current.next.prev = previous; if (position === this.length - 1) { - this.tail = previous + this.tail = previous; } } - this.length-- - return current.element + this.length--; + return current.element; } else { - return null + return null; } } @@ -128,21 +120,21 @@ class CircularDoublyLinkedList { * Iterator over the elements in the list. */ *elements() { - let currentNode = this.head - if (!currentNode) return + let currentNode = this.head; + if (!currentNode) return; do { - yield currentNode.element - currentNode = currentNode.next - } while (currentNode !== this.head) + yield currentNode.element; + currentNode = currentNode.next; + } while (currentNode !== this.head); } /** * Checks if the list is empty. * @returns {boolean} - True if the list is empty, false otherwise. */ - isEmpty () { - return this.length === 0 + isEmpty() { + return this.length === 0; } } -export { CircularDoublyLinkedList } +export { CircularDoublyLinkedList };