mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-08 19:06:00 +00:00
29 lines
500 B
JavaScript
29 lines
500 B
JavaScript
import LinkedList from '../linked-list/LinkedList';
|
|
|
|
export default class Queue {
|
|
constructor() {
|
|
this.linkedList = new LinkedList();
|
|
}
|
|
|
|
isEmpty() {
|
|
return !this.linkedList.tail;
|
|
}
|
|
|
|
peek() {
|
|
if (!this.linkedList.head) {
|
|
return null;
|
|
}
|
|
|
|
return this.linkedList.head.value;
|
|
}
|
|
|
|
enqueue(value) {
|
|
this.linkedList.append({ value });
|
|
}
|
|
|
|
dequeue() {
|
|
const removedHead = this.linkedList.deleteHead();
|
|
return removedHead ? removedHead.value : null;
|
|
}
|
|
}
|