Skip to content

Commit d55c3ef

Browse files
link PriorityQueue.js
1 parent a96c894 commit d55c3ef

File tree

1 file changed

+38
-39
lines changed

1 file changed

+38
-39
lines changed

data-structures/Queue/Queue.js

+38-39
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
1-
import LinkedList from './LinkedList.js';
1+
import LinkedList from "./LinkedList.js";
22

33
export default class Queue {
4-
constructor(maxSize = Infinity){
5-
this.queue = new LinkedList();
6-
this.maxSize = maxSize;
7-
this.size = 0;
4+
constructor(maxSize = Infinity) {
5+
this.queue = new LinkedList();
6+
this.maxSize = maxSize;
7+
this.size = 0;
8+
}
9+
10+
isEmpty() {
11+
return this.size === 0;
12+
}
13+
14+
hasRoom() {
15+
return this.size < this.maxSize;
16+
}
17+
18+
enqueue(data) {
19+
if (this.hasRoom()) {
20+
this.queue.addToTail(data);
21+
this.size++;
22+
} else {
23+
throw new Error("Queue is full");
824
}
9-
10-
isEmpty(){
11-
return this.size === 0;
12-
}
13-
14-
hasRoom(){
15-
return this.size < this.maxSize;
16-
}
17-
18-
enqueue(data){
19-
if(this.hasRoom()){
20-
this.queue.addToTail(data);
21-
this.size++;
22-
23-
} else {
24-
throw new Error ("Queue is full");
25-
}
25+
}
26+
27+
dequeue() {
28+
if (!this.isEmpty()) {
29+
const data = this.queue.removeHead();
30+
this.size--;
31+
return data;
32+
} else {
33+
throw new Error("Queue is empty");
2634
}
35+
}
2736

28-
dequeue(){
29-
if(!this.isEmpty()){
30-
const data = this.queue.removeHead();
31-
this.size--;
32-
return data;
33-
} else {
34-
throw new Error ("Queue is empty");
35-
}
36-
}
37+
peek() {
38+
return !this.isEmpty() ? this.queue.head.data : null;
39+
}
3740

38-
peek(){
39-
return !this.isEmpty() ? this.queue.head.data : null;
40-
}
41-
42-
print(){
43-
return this.queue.printList();
44-
}
45-
}
41+
print() {
42+
return this.queue.printList();
43+
}
44+
}

0 commit comments

Comments
 (0)