From a310651dc3ba1065d052c7e9ccb08ee7fc772e7d Mon Sep 17 00:00:00 2001 From: meironY2008 <67857621+meironY2008@users.noreply.github.com> Date: Tue, 22 Sep 2020 15:41:46 +0300 Subject: [PATCH] finish --- .huskyrc.json | 5 -- .../__test__/PriorityQueue.test.js | 2 +- .../priority-queue/new-priorety-queue.js | 60 +++++++++++++++++++ 3 files changed, 61 insertions(+), 6 deletions(-) delete mode 100644 .huskyrc.json create mode 100644 src/data-structures/priority-queue/new-priorety-queue.js diff --git a/.huskyrc.json b/.huskyrc.json deleted file mode 100644 index 6e9b7b23..00000000 --- a/.huskyrc.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "hooks": { - "pre-commit": "npm run lint && npm run test" - } -} diff --git a/src/data-structures/priority-queue/__test__/PriorityQueue.test.js b/src/data-structures/priority-queue/__test__/PriorityQueue.test.js index 2ffe4acb..5568f6d1 100644 --- a/src/data-structures/priority-queue/__test__/PriorityQueue.test.js +++ b/src/data-structures/priority-queue/__test__/PriorityQueue.test.js @@ -1,4 +1,4 @@ -import PriorityQueue from '../PriorityQueue'; +import PriorityQueue from '../new-priorety-queue'; describe('PriorityQueue', () => { it('should create default priority queue', () => { diff --git a/src/data-structures/priority-queue/new-priorety-queue.js b/src/data-structures/priority-queue/new-priorety-queue.js new file mode 100644 index 00000000..4c73fdf3 --- /dev/null +++ b/src/data-structures/priority-queue/new-priorety-queue.js @@ -0,0 +1,60 @@ + +export default class PriorityQueue { + constructor() { + this.arr=[]; + } + isEmpty(){ + return this.arr.length===0; + } + peek(){ + return this.isEmpty() ? null : this.arr[this.arr.length - 1][0]; + } + add(item , priority = 0){ + if(this.isEmpty()){ + return this.arr =[[item,priority]]; + } + for( let i = 0; i < this.arr.length; i++ ){ + if(this.arr[i][1] <= priority ){ + this.arr.splice(i,0,[item,priority]); + return this.arr; + } + } + return this.arr.push([item,priority]); + } + + poll(){ + if(this.isEmpty()){ + return null; + } + return this.arr.pop()[0]; + } + + changePriority(item, priority = 0) { + if(this.isEmpty()) { + return null; + } + for (let i = 0; i < this.arr.length; i++) { + if (this.arr[i][0] === item) { + this.arr.splice(i, 1); + this.add(item, priority); + return this.arr; + } + } + return null; + } + + hasValue(item) { + if(this.isEmpty()) { + return false; + } + for (let i = 0; i < this.arr.length; i++) { + if (this.arr[i][0] === item) { + return true; + } + } + return false; + } + } + + + \ No newline at end of file