mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-08 19:06:00 +00:00
33 lines
571 B
JavaScript
33 lines
571 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;
|
|
}
|
|
|
|
toString(callback) {
|
|
return this.linkedList.toString(callback);
|
|
}
|
|
}
|