mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Merge da2e9aa92b
into 2c67b48c21
This commit is contained in:
commit
e024c4ac6f
@ -1,4 +1,4 @@
|
||||
import PriorityQueue from '../PriorityQueue';
|
||||
import PriorityQueue from '../new-priorety-queue';
|
||||
|
||||
describe('PriorityQueue', () => {
|
||||
it('should create default priority queue', () => {
|
||||
|
60
src/data-structures/priority-queue/new-priorety-queue.js
Normal file
60
src/data-structures/priority-queue/new-priorety-queue.js
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user