From 3169ebc0dfa349a1dd1ece17ee86dd266bd2525b Mon Sep 17 00:00:00 2001 From: KMininkov Date: Tue, 21 Sep 2021 23:31:30 +0300 Subject: [PATCH 1/8] Added doubly linked list push, pop, unshift methods --- index.html | 12 +++++++++ main.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 index.html create mode 100644 main.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..9107f7f --- /dev/null +++ b/index.html @@ -0,0 +1,12 @@ + + + + + + + Document + + + + + \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..65492d1 --- /dev/null +++ b/main.js @@ -0,0 +1,76 @@ +class Node { + constructor(value) { + this.value = value; + this.prev = null; + this.next = null; + } +} + +class DoublyLinkedList { + constructor() { + this.head = null; + this.tail = null; + this.length = 0; + } + + // add element to the end + push(value) { + let node = new Node(value) + // if list is empty + if(!this.head) { + this.head = node; + this.tail = node; + } else { + let temp = this.tail; + this.tail = node; + node.prev = temp; + temp.next = node; + } + this.length++; + return this; + } + + // delete last element and return his value + pop() { + console.log(this.tail); + if(!this.head) return undefined; + let temp = this.tail; + if(this.length === 1) { + this.head = null; + this.tail = null; + } else { + this.tail = temp.prev; + this.tail.next = null; + temp.prev = null; + } + this.length--; + return list; + } + + // add element in front + unshift(value) { + let node = new Node(value); + if(!this.head) { + this.head = node; + this.tail = node; + } else { + let temp = this.head; + this.head = node; + node.next = temp; + temp.prev = node; + } + this.length++; + return this; + } + +} + + +let list = new DoublyLinkedList; +/* list.push(1); +list.push(2); +list.push(3); +list.push(4); */ +//list.pop(); +list.unshift(0); +console.log(list); \ No newline at end of file From c8a5e2df0d67046ff66e0fd984fc50939d668bd8 Mon Sep 17 00:00:00 2001 From: KMininkov Date: Tue, 21 Sep 2021 23:44:39 +0300 Subject: [PATCH 2/8] Added shift method --- main.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/main.js b/main.js index 65492d1..599b522 100644 --- a/main.js +++ b/main.js @@ -46,7 +46,7 @@ class DoublyLinkedList { this.length--; return list; } - + // add element in front unshift(value) { let node = new Node(value); @@ -63,14 +63,29 @@ class DoublyLinkedList { return this; } + // delete front element and return his value + shift() { + if(!this.head) return undefined; + let temp = this.head; + if(this.length === 1) { + this.head = null; + this.tail = null; + } else { + this.head = temp.next; + this.head.prev = null; + temp.next = null; + } + this.length--; + return this; + } } let list = new DoublyLinkedList; -/* list.push(1); +list.push(1); list.push(2); list.push(3); -list.push(4); */ -//list.pop(); -list.unshift(0); +list.push(4); +list.shift(); + console.log(list); \ No newline at end of file From 08e72f90887ed9a1611bf22f09f95f3f867dc027 Mon Sep 17 00:00:00 2001 From: KMininkov Date: Tue, 21 Sep 2021 23:57:35 +0300 Subject: [PATCH 3/8] Added isEmpty method --- main.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/main.js b/main.js index 599b522..7d52221 100644 --- a/main.js +++ b/main.js @@ -78,14 +78,21 @@ class DoublyLinkedList { this.length--; return this; } + + isEmpty() { + if(!this.head || !this.tail) { + return true; + } else { + return false; + } + } } let list = new DoublyLinkedList; -list.push(1); -list.push(2); -list.push(3); -list.push(4); -list.shift(); +list.unshift(1); +list.unshift(2); +list.unshift(3); +console.log(`Is list empty: ${list.isEmpty()}`); console.log(list); \ No newline at end of file From 9841d7f71926d133c5f252c7c46a587c6883087e Mon Sep 17 00:00:00 2001 From: KMininkov Date: Wed, 22 Sep 2021 01:22:50 +0300 Subject: [PATCH 4/8] Added tests --- index.html | 20 +++++++++++++++++++- main.js | 26 ++++++++++++++++---------- tests.js | 23 +++++++++++++++++++++++ 3 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 tests.js diff --git a/index.html b/index.html index 9107f7f..3efe6b8 100644 --- a/index.html +++ b/index.html @@ -4,9 +4,27 @@ - Document + Deque + +
+ + + + + + + \ No newline at end of file diff --git a/main.js b/main.js index 7d52221..a02a4ba 100644 --- a/main.js +++ b/main.js @@ -32,7 +32,8 @@ class DoublyLinkedList { // delete last element and return his value pop() { - console.log(this.tail); + let lastElement = this.tail; + console.log(lastElement); if(!this.head) return undefined; let temp = this.tail; if(this.length === 1) { @@ -44,7 +45,7 @@ class DoublyLinkedList { temp.prev = null; } this.length--; - return list; + return lastElement; } // add element in front @@ -65,6 +66,8 @@ class DoublyLinkedList { // delete front element and return his value shift() { + let firstElement = this.head; + console.log(firstElement); if(!this.head) return undefined; let temp = this.head; if(this.length === 1) { @@ -76,23 +79,26 @@ class DoublyLinkedList { temp.next = null; } this.length--; - return this; + return firstElement; } isEmpty() { if(!this.head || !this.tail) { - return true; + return console.log('Is deque empty', true); } else { - return false; + return console.log('Is deque empty', false); } } } let list = new DoublyLinkedList; -list.unshift(1); -list.unshift(2); -list.unshift(3); -console.log(`Is list empty: ${list.isEmpty()}`); -console.log(list); \ No newline at end of file +list.push(1); +list.push(2); +list.push(3); +list.unshift(0); + +list.isEmpty(); + +console.log('Current deque: ', list); diff --git a/tests.js b/tests.js new file mode 100644 index 0000000..6911931 --- /dev/null +++ b/tests.js @@ -0,0 +1,23 @@ +const chai = window.chai; +const assert = chai.assert; + +var testingList = new DoublyLinkedList; + +testingList.push(1); +testingList.push(2); +testingList.push(3); +var lastElem = testingList.tail; +var firstElem = testingList.head; +console.log('Deque for tests: ', testingList); + + +describe('Deque based on doubly linked list', () => { + it('should pass all tests', () => { + console.log('test pop(): '); + assert.equal(testingList.pop(), lastElem); // delete last element and return his value + console.log('test shift(): '); + assert.equal(testingList.shift(), firstElem); // delete first element and return his value + console.log('test isEmpty(): '); + assert.equal(testingList.isEmpty(), console.log(false)); // check is deque empty + }); +}); \ No newline at end of file From c1ce7e918b8dc8d8a6de7ce8bda79ee0e48970f7 Mon Sep 17 00:00:00 2001 From: KMininkov <74874576+Wordllban@users.noreply.github.com> Date: Wed, 22 Sep 2021 01:33:17 +0300 Subject: [PATCH 5/8] Update README.md --- README.md | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7230b8c..e4597f5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,25 @@ +# Lab-2 -## Algorithms +--- - My lab works of Algorithms classes as a student of "Internet of Things" program in Lviv Polytechnic National University. +## Task + +Implement Deque based on Doubly-linked list with basics methods thats belong to data structure. + +--- + +### Requiered methods + + Unshift - add element in front + + Shift - delete front element and return his value + + Push - add element in back + + Pop - delete element in back and return his value + + isEmpty - check is deque empty + +### Code must be covered with tests (60%+) + +--- + +## Installation + + Download the project and open index.html file + + Click right mouse button and select "Inspect" + + Now you can use `list.push(*yourValue*)`, `list.unshift(*yourValue*)`, `list.pop()`, `list.shift()` in console From 80a19d16e69c7cd79829f0076dc0dfc146f0d81b Mon Sep 17 00:00:00 2001 From: KMininkov <74874576+Wordllban@users.noreply.github.com> Date: Wed, 22 Sep 2021 01:34:07 +0300 Subject: [PATCH 6/8] Update README.md --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index e4597f5..00c9784 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,14 @@ Implement Deque based on Doubly-linked list with basics methods thats belong to --- -### Requiered methods - + Unshift - add element in front - + Shift - delete front element and return his value - + Push - add element in back - + Pop - delete element in back and return his value - + isEmpty - check is deque empty - -### Code must be covered with tests (60%+) + ### Requiered methods + + Unshift - add element in front + + Shift - delete front element and return his value + + Push - add element in back + + Pop - delete element in back and return his value + + isEmpty - check is deque empty + + ### Code must be covered with tests (60%+) --- From 730f38c1a4758f817676318d209e5d46ce6cd543 Mon Sep 17 00:00:00 2001 From: KMininkov Date: Wed, 22 Sep 2021 01:34:53 +0300 Subject: [PATCH 7/8] Added comment with tip --- main.js | 1 + 1 file changed, 1 insertion(+) diff --git a/main.js b/main.js index a02a4ba..7957914 100644 --- a/main.js +++ b/main.js @@ -82,6 +82,7 @@ class DoublyLinkedList { return firstElement; } + // check is deque empty isEmpty() { if(!this.head || !this.tail) { return console.log('Is deque empty', true); From 505a427402049e67b644b7141f02545cbbbb6ad3 Mon Sep 17 00:00:00 2001 From: KMininkov <74874576+Wordllban@users.noreply.github.com> Date: Fri, 10 Dec 2021 22:04:03 +0300 Subject: [PATCH 8/8] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 00c9784..379767a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Implement Deque based on Doubly-linked list with basics methods thats belong to --- - ### Requiered methods + ### Required methods + Unshift - add element in front + Shift - delete front element and return his value + Push - add element in back