From 74e61b1aff180b4ffb407014c0dda2b816a0812e Mon Sep 17 00:00:00 2001 From: volkov-ilya-ip51 Date: Tue, 7 Mar 2017 18:50:40 +0200 Subject: [PATCH 1/2] double list with classes --- .eslintrc.yml | 2 +- JavaScript/2-doubly.js | 62 ++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 31 deletions(-) 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..51ceafc 100644 --- a/JavaScript/2-doubly.js +++ b/JavaScript/2-doubly.js @@ -1,38 +1,40 @@ 'use strict'; -function LinkedList() { - this.first = null; - this.last = null; - this.length = 0; +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; + } + } } -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 Node{ + constructor(list, data) { + this.list = list; + this.data = data; + this.prev = null; + this.next = null; } -}; - -function Node(list, data) { - this.list = list; - this.data = data; - this.prev = null; - this.next = null; } const list = new LinkedList(); From 8de4a0a98ddc979b5dabcf20001be861ebaf8828 Mon Sep 17 00:00:00 2001 From: volkov-ilya-ip51 Date: Tue, 7 Mar 2017 19:18:27 +0200 Subject: [PATCH 2/2] search methods --- JavaScript/2-doubly.js | 44 +++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/JavaScript/2-doubly.js b/JavaScript/2-doubly.js index 51ceafc..8560133 100644 --- a/JavaScript/2-doubly.js +++ b/JavaScript/2-doubly.js @@ -1,5 +1,14 @@ 'use strict'; +class Node { + constructor(list, data) { + this.list = list; + this.data = data; + this.prev = null; + this.next = null; + } +} + class LinkedList { constructor() { this.first = null; @@ -13,7 +22,7 @@ class LinkedList { else this.last.next = node; this.last = node; this.length++; - return node; + return node; } pop() { if (this.length > 0) { @@ -26,14 +35,31 @@ class LinkedList { return node.data; } } -} - -class Node{ - constructor(list, data) { - this.list = list; - this.data = data; - this.prev = null; - this.next = null; + 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; } }