From 92b081511643aa930dbc49b24f8acc3e2904214d Mon Sep 17 00:00:00 2001 From: Anna Yasenova Date: Sun, 5 Mar 2017 16:15:24 +0200 Subject: [PATCH 1/3] Implemented with ES6 classes, additional functionality. --- JavaScript/3-classes.js | 141 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 JavaScript/3-classes.js diff --git a/JavaScript/3-classes.js b/JavaScript/3-classes.js new file mode 100644 index 0000000..a4e5650 --- /dev/null +++ b/JavaScript/3-classes.js @@ -0,0 +1,141 @@ +'use strict'; + +class Node { + constructor(list, name, data) { + this.list = list; + this.data = data; + this.name = name; + this.prev = null; + this.next = null; + } +} + +class LinkedList { + + constructor() { + this.first = null; + this.last = null; + this.length = 0; + } + + push(name, data) { + const node = new Node(this, name, 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; + } + + } + + searchPos(searchPosition) { + + let current = this.first, + position = 0; + + if (this.length === 0 || + searchPosition < 0 || + searchPosition >= this.length) { + throw new Error('Position out of bounds.'); + } + + while (position < searchPosition) { + current = current.next; + ++position; + } + + return current; + } + + remove(removePosition) { + let current = this.first, + position = 0; + + if (this.length < 0 || removePosition >= this.length) { + throw new Error('Position out of bounds.'); + } + + if (removePosition === 0) { + this.first = current.next; + this.first.prev = null; + this.length--; + return; + } + + while (position < removePosition) { + current = current.next; + ++position; + } + + current.prev.next = current.next; + current.next.prev = current.prev; + this.length--; + } + + findFirst(name) { + let current = this.first; + + while (current !== null && current.name !== name) { + current = current.next; + } + return current; + } + + findAll(name) { + let current = this.first; + const arr = []; + + while (current !== null) { + if (current.name === name) { + arr.push(current); + } + current = current.next; + } + return arr; + } + + find(name, callback) { + const arr = this.findAll(name); + let i; + for (i = 0; i < arr.length; ++i) { + callback(arr[i]); + } + } +} + + + + + + +const list1 = new LinkedList(); +list1.push('first', 1); +list1.push('second', 2); +list1.push('third', 3); +list1.push('second', 5); + +console.dir(list1.findFirst('first')); +console.dir(list1.findAll('second')); +list1.find('second', n => console.log(n)); +console.dir(list1.searchPos(-2)); +console.dir(list1.searchPos(1)); + +console.dir(list1); +list1.remove(0); +console.dir(list1); From 74a655af7613dbe1a31845e01007bd9396a1af92 Mon Sep 17 00:00:00 2001 From: Anna Yasenova Date: Mon, 6 Mar 2017 15:57:00 +0200 Subject: [PATCH 2/3] Variables declared separately. --- JavaScript/3-classes.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/JavaScript/3-classes.js b/JavaScript/3-classes.js index a4e5650..361bf39 100644 --- a/JavaScript/3-classes.js +++ b/JavaScript/3-classes.js @@ -46,8 +46,8 @@ class LinkedList { searchPos(searchPosition) { - let current = this.first, - position = 0; + let current = this.first; + let position = 0; if (this.length === 0 || searchPosition < 0 || @@ -64,8 +64,8 @@ class LinkedList { } remove(removePosition) { - let current = this.first, - position = 0; + let current = this.first; + let position = 0; if (this.length < 0 || removePosition >= this.length) { throw new Error('Position out of bounds.'); @@ -133,7 +133,7 @@ list1.push('second', 5); console.dir(list1.findFirst('first')); console.dir(list1.findAll('second')); list1.find('second', n => console.log(n)); -console.dir(list1.searchPos(-2)); +//console.dir(list1.searchPos(-2)); console.dir(list1.searchPos(1)); console.dir(list1); From 386eb2bc401ef635d5cb5a3c97a3377c8ed6aaa5 Mon Sep 17 00:00:00 2001 From: Anna Yasenova Date: Tue, 7 Mar 2017 16:10:04 +0200 Subject: [PATCH 3/3] Condition shorter (52 row), row 92 optimized. --- JavaScript/3-classes.js | 42 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/JavaScript/3-classes.js b/JavaScript/3-classes.js index 361bf39..a71fb57 100644 --- a/JavaScript/3-classes.js +++ b/JavaScript/3-classes.js @@ -44,41 +44,39 @@ class LinkedList { } - searchPos(searchPosition) { + itemAtPos(pos) { let current = this.first; - let position = 0; + let counter = 0; - if (this.length === 0 || - searchPosition < 0 || - searchPosition >= this.length) { - throw new Error('Position out of bounds.'); + if (!(this.length) || pos < 0 || pos >= this.length) { + return false; } - while (position < searchPosition) { + while (counter < pos) { current = current.next; - ++position; + ++counter; } return current; } - remove(removePosition) { + remove(remPos) { let current = this.first; let position = 0; - if (this.length < 0 || removePosition >= this.length) { - throw new Error('Position out of bounds.'); + if (this.length < 0 || remPos >= this.length) { + return false; } - if (removePosition === 0) { + if (remPos === 0) { this.first = current.next; this.first.prev = null; this.length--; return; } - while (position < removePosition) { + while (position < remPos) { current = current.next; ++position; } @@ -91,7 +89,7 @@ class LinkedList { findFirst(name) { let current = this.first; - while (current !== null && current.name !== name) { + while (current && current.name !== name) { current = current.next; } return current; @@ -130,12 +128,12 @@ list1.push('second', 2); list1.push('third', 3); list1.push('second', 5); -console.dir(list1.findFirst('first')); -console.dir(list1.findAll('second')); -list1.find('second', n => console.log(n)); -//console.dir(list1.searchPos(-2)); -console.dir(list1.searchPos(1)); +// console.dir(list1.findFirst('first')); +// console.dir(list1.findAll('second')); +// list1.find('second', n => console.log(n)); +// //console.dir(list1.searchPos(-2)); +console.dir(list1.itemAtPos(1)); -console.dir(list1); -list1.remove(0); -console.dir(list1); +// console.dir(list1); +// list1.remove(0); +// console.dir(list1);