This commit is contained in:
meironY2008 2020-09-22 15:41:46 +03:00
parent 07bc4a4b97
commit a310651dc3
3 changed files with 61 additions and 6 deletions

View File

@ -1,5 +0,0 @@
{
"hooks": {
"pre-commit": "npm run lint && npm run test"
}
}

View File

@ -1,4 +1,4 @@
import PriorityQueue from '../PriorityQueue'; import PriorityQueue from '../new-priorety-queue';
describe('PriorityQueue', () => { describe('PriorityQueue', () => {
it('should create default priority queue', () => { it('should create default priority queue', () => {

View File

@ -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;
}
}