Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Deque</title>
<link
href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css"
rel="stylesheet"
/>
</head>
<body>
<div id="mocha"></div>
<script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
<script src="https://cdn.rawgit.com/chaijs/chai/3.5.0/chai.js"></script>
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>

<script src="main.js"></script>
<script>
const mocha = window.mocha;
mocha.setup("bdd");
</script>
<script src="tests.js"></script>
<script>
mocha.checkLeaks();
mocha.run();
</script>
</body>
</html>
105 changes: 105 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -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);
23 changes: 23 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
@@ -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
});
});