mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-19 08:59:05 +08:00
Add Queue.
This commit is contained in:
28
src/data-structures/queue/Queue.js
Normal file
28
src/data-structures/queue/Queue.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import LinkedList from '../linked-list/LinkedList';
|
||||
|
||||
export default class Queue {
|
||||
constructor() {
|
||||
this.linkedList = new LinkedList();
|
||||
}
|
||||
|
||||
isEmpty() {
|
||||
return !this.linkedList.tail;
|
||||
}
|
||||
|
||||
peek() {
|
||||
if (!this.linkedList.tail) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.linkedList.tail.value;
|
||||
}
|
||||
|
||||
add(value) {
|
||||
this.linkedList.append({ value });
|
||||
}
|
||||
|
||||
remove() {
|
||||
const removedTail = this.linkedList.deleteTail();
|
||||
return removedTail ? removedTail.value : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user