From 05c782b47868c92a0b4822c7647ed72e5209f8d8 Mon Sep 17 00:00:00 2001 From: mmestiyak Date: Tue, 22 Feb 2022 12:22:20 +0600 Subject: [PATCH 1/2] replaced constructor function with es6 class syntax --- .../Linked-List/DoublyLinkedList.js | 134 +++++++++--------- 1 file changed, 70 insertions(+), 64 deletions(-) diff --git a/Data-Structures/Linked-List/DoublyLinkedList.js b/Data-Structures/Linked-List/DoublyLinkedList.js index 4bfb065198..4ca821bf00 100644 --- a/Data-Structures/Linked-List/DoublyLinkedList.js +++ b/Data-Structures/Linked-List/DoublyLinkedList.js @@ -1,54 +1,60 @@ -// Hamza chabchoub contribution for a university project -function DoubleLinkedList () { - const Node = function (element) { - this.element = element - this.next = null - this.prev = null +class Node { + constructor(element){ + this.element = element; + this.next = null; + this.prev = null; + } +} + + +class DoubleLinkedList { + + constructor(){ + this.length = 0; + this.head = null; + this.tail = null; } - let length = 0 - let head = null - let tail = null // Add new element - this.append = function (element) { + append(element) { const node = new Node(element) - if (!head) { - head = node - tail = node + if (!this.head) { + this.head = node + this.tail = node } else { - node.prev = tail - tail.next = node - tail = node + node.prev = this.tail + this.tail.next = node + this.tail = node } - length++ + this.length++ } // Add element - this.insert = function (position, element) { + insert(position, element) { // Check of out-of-bound values - if (position >= 0 && position <= length) { + if (position >= 0 && position <= this.length) { const node = new Node(element) - let current = head + let current = this.head let previous = 0 let index = 0 if (position === 0) { - if (!head) { - head = node - tail = node + if (!this.head) { + this.head = node + this.tail = node } else { node.next = current current.prev = node - head = node + this.head = node } - } else if (position === length) { - current = tail + } else if (position === this.length) { + current = this.tail current.next = node node.prev = current - tail = node + this.tail = node } else { while (index++ < position) { previous = current @@ -63,7 +69,7 @@ function DoubleLinkedList () { node.prev = previous } - length++ + this.length++ return true } else { return false @@ -71,27 +77,27 @@ function DoubleLinkedList () { } // Remove element at any position - this.removeAt = function (position) { + removeAt(position) { // look for out-of-bounds value - if (position > -1 && position < length) { - let current = head + if (position > -1 && position < this.length) { + let current = this.head let previous = 0 let index = 0 // Removing first item if (position === 0) { - head = current.next + this.head = current.next - // if there is only one item, update tail //NEW - if (length === 1) { - tail = null + // if there is only one item, update this.tail //NEW + if (this.length === 1) { + this.tail = null } else { - head.prev = null + this.head.prev = null } - } else if (position === length - 1) { - current = tail - tail = current.prev - tail.next = null + } else if (position === this.length - 1) { + current = this.tail + this.tail = current.prev + this.tail.next = null } else { while (index++ < position) { previous = current @@ -103,7 +109,7 @@ function DoubleLinkedList () { current.next.prev = previous } - length-- + this.length-- return current.element } else { return null @@ -111,8 +117,8 @@ function DoubleLinkedList () { } // Get the indexOf item - this.indexOf = function (elm) { - let current = head + indexOf(elm) { + let current = this.head let index = -1 // If element found then return its position @@ -130,28 +136,28 @@ function DoubleLinkedList () { } // Find the item in the list - this.isPresent = (elm) => { + isPresent(elm) { return this.indexOf(elm) !== -1 } // Delete an item from the list - this.delete = (elm) => { + delete(elm){ return this.removeAt(this.indexOf(elm)) } // Delete first item from the list - this.deleteHead = function () { + deleteHead() { this.removeAt(0) } // Delete last item from the list - this.deleteTail = function () { - this.removeAt(length - 1) + deleteTail() { + this.removeAt(this.length - 1) } // Print item of the string - this.toString = function () { - let current = head + toString() { + let current = this.head let string = '' while (current) { @@ -163,9 +169,9 @@ function DoubleLinkedList () { } // Convert list to array - this.toArray = function () { + toArray() { const arr = [] - let current = head + let current = this.head while (current) { arr.push(current.element) @@ -176,27 +182,27 @@ function DoubleLinkedList () { } // Check if list is empty - this.isEmpty = function () { - return length === 0 + isEmpty() { + return this.length === 0 } // Get the size of the list - this.size = function () { - return length + size() { + return this.length } - // Get the head - this.getHead = function () { - return head + // Get the this.head + getHead() { + return this.head } - // Get the tail - this.getTail = function () { - return tail + // Get the this.tail + getTail() { + return this.tail } // Method to iterate over the LinkedList - this.iterator = function () { + iterator() { let currentNode = this.getHead() if (currentNode === null) return -1 @@ -211,7 +217,7 @@ function DoubleLinkedList () { // Method to log the LinkedList, for debugging // it' a circular structure, so can't use stringify to debug the whole structure - this.log = function () { + log() { let currentNode = this.getHead() while (currentNode) { console.log(currentNode.element) From d23c479ed32f27c81fa61d5884de177898d872f0 Mon Sep 17 00:00:00 2001 From: mmestiyak Date: Wed, 23 Feb 2022 08:59:45 +0600 Subject: [PATCH 2/2] formatted code with standard.js --- .../Linked-List/DoublyLinkedList.js | 53 +++++++++---------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/Data-Structures/Linked-List/DoublyLinkedList.js b/Data-Structures/Linked-List/DoublyLinkedList.js index 4ca821bf00..aeaaeaf085 100644 --- a/Data-Structures/Linked-List/DoublyLinkedList.js +++ b/Data-Structures/Linked-List/DoublyLinkedList.js @@ -1,23 +1,20 @@ class Node { - constructor(element){ - this.element = element; - this.next = null; - this.prev = null; + constructor (element) { + this.element = element + this.next = null + this.prev = null } } - -class DoubleLinkedList { - - constructor(){ - this.length = 0; - this.head = null; - this.tail = null; +class DoubleLinkedList { + constructor () { + this.length = 0 + this.head = null + this.tail = null } - // Add new element - append(element) { + append (element) { const node = new Node(element) if (!this.head) { @@ -33,7 +30,7 @@ class DoubleLinkedList { } // Add element - insert(position, element) { + insert (position, element) { // Check of out-of-bound values if (position >= 0 && position <= this.length) { const node = new Node(element) @@ -77,7 +74,7 @@ class DoubleLinkedList { } // Remove element at any position - removeAt(position) { + removeAt (position) { // look for out-of-bounds value if (position > -1 && position < this.length) { let current = this.head @@ -117,7 +114,7 @@ class DoubleLinkedList { } // Get the indexOf item - indexOf(elm) { + indexOf (elm) { let current = this.head let index = -1 @@ -136,27 +133,27 @@ class DoubleLinkedList { } // Find the item in the list - isPresent(elm) { + isPresent (elm) { return this.indexOf(elm) !== -1 } // Delete an item from the list - delete(elm){ + delete (elm) { return this.removeAt(this.indexOf(elm)) } // Delete first item from the list - deleteHead() { + deleteHead () { this.removeAt(0) } // Delete last item from the list - deleteTail() { + deleteTail () { this.removeAt(this.length - 1) } // Print item of the string - toString() { + toString () { let current = this.head let string = '' @@ -169,7 +166,7 @@ class DoubleLinkedList { } // Convert list to array - toArray() { + toArray () { const arr = [] let current = this.head @@ -182,27 +179,27 @@ class DoubleLinkedList { } // Check if list is empty - isEmpty() { + isEmpty () { return this.length === 0 } // Get the size of the list - size() { + size () { return this.length } // Get the this.head - getHead() { + getHead () { return this.head } // Get the this.tail - getTail() { + getTail () { return this.tail } // Method to iterate over the LinkedList - iterator() { + iterator () { let currentNode = this.getHead() if (currentNode === null) return -1 @@ -217,7 +214,7 @@ class DoubleLinkedList { // Method to log the LinkedList, for debugging // it' a circular structure, so can't use stringify to debug the whole structure - log() { + log () { let currentNode = this.getHead() while (currentNode) { console.log(currentNode.element) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy