diff --git a/README.md b/README.md index 7230b8c..379767a 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. + +--- + + ### Required 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 diff --git a/index.html b/index.html new file mode 100644 index 0000000..3efe6b8 --- /dev/null +++ b/index.html @@ -0,0 +1,30 @@ + + + + + + + Deque + + + +
+ + + + + + + + + + \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..7957914 --- /dev/null +++ b/main.js @@ -0,0 +1,105 @@ +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() { + let lastElement = this.tail; + console.log(lastElement); + 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 lastElement; + } + + // 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; + } + + // 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) { + this.head = null; + this.tail = null; + } else { + this.head = temp.next; + this.head.prev = null; + temp.next = null; + } + this.length--; + return firstElement; + } + + // check is deque empty + isEmpty() { + if(!this.head || !this.tail) { + return console.log('Is deque empty', true); + } else { + return console.log('Is deque empty', false); + } + } +} + + +let list = new DoublyLinkedList; + +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