-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.ts
More file actions
34 lines (27 loc) · 884 Bytes
/
queue.ts
File metadata and controls
34 lines (27 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import LinkedList from '../linked-list/linked-list'
export default class Queue<T> {
constructor(public linkedList: LinkedList<T> = new LinkedList()) {}
isEmpty(): boolean {
return !this.linkedList.head
}
// Read the element at the front of the queue
peek(): T | undefined | null {
if (this.isEmpty()) {
return null
}
return this.linkedList.head?.value
}
// Add element to the end of queue (the tail of the linked list)
enqueue(value: T): void {
this.linkedList.append(value)
}
// Remove the element at the front of queue (the head of the linked list)
dequeue(): T | null {
const removeHead = this.linkedList.deleteHead()
return removeHead ? removeHead.value : null
}
// String representation of the queue's linked list
toString(callback?: (value: T) => string) {
return this.linkedList.toString(callback)
}
}