diff --git a/.eslintrc.yml b/.eslintrc.yml index ddc22cb..ff4265f 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -15,7 +15,7 @@ rules: MemberExpression: 1 linebreak-style: - error - - unix + - windows quotes: - error - single diff --git a/JavaScript/2-doubly.js b/JavaScript/2-doubly.js index 54b7ada..8560133 100644 --- a/JavaScript/2-doubly.js +++ b/JavaScript/2-doubly.js @@ -1,38 +1,66 @@ 'use strict'; -function LinkedList() { - this.first = null; - this.last = null; - this.length = 0; +class Node { + constructor(list, data) { + this.list = list; + this.data = data; + this.prev = null; + this.next = null; + } } -LinkedList.prototype.push = function(data) { - const node = new Node(this, data); - node.prev = this.last; - if (this.length === 0) this.first = node; - else this.last.next = node; - this.last = node; - this.length++; - return node; -}; - -LinkedList.prototype.pop = function() { - if (this.length > 0) { - const node = this.last; - this.last = node.prev; - node.list = null; - node.prev = null; - node.next = null; - this.length--; - return node.data; +class LinkedList { + constructor() { + this.first = null; + this.last = null; + this.length = 0; + } + push(data) { + const node = new Node(this, data); + node.prev = this.last; + if (this.length === 0) this.first = node; + else this.last.next = node; + this.last = node; + this.length++; + return node; + } + pop() { + if (this.length > 0) { + const node = this.last; + this.last = node.prev; + node.list = null; + node.prev = null; + node.next = null; + this.length--; + return node.data; + } + } + findFirst(name) { + if (this.length > 0) { + let node = this.first; + if (node.data.name === name) + return node; + while (node !== this.last) { + node = node.next; + if (node.data.name === name) + return node; + } + } + } + findAll(name) { + const nodeArray = []; + if (this.length > 0) { + let node = this.first; + if (node.data.name === name) + nodeArray.push(node); + while (node !== this.last) { + node = node.next; + if (node.data.name === name) + nodeArray.push(node); + } + } + return nodeArray; } -}; - -function Node(list, data) { - this.list = list; - this.data = data; - this.prev = null; - this.next = null; } const list = new LinkedList();