Queue.uts
533 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
35
36
37
export default class Queue<T> {
items: T[]
constructor(items: T[] = []) {
this.items = items
}
enqueue(element: T) {
this.items.push(element)
}
dequeue(): T | null {
if (this.isEmpty()) {
return null
}
return this.items.shift()
}
front(): T | null {
if (this.isEmpty()) {
return null
}
return this.items[0]
}
isEmpty(): boolean {
return this.items.length == 0
}
get size(): number {
return this.items.length
}
clear() {
this.items = []
}
}