forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularQueue.ts
51 lines (43 loc) · 1.12 KB
/
CircularQueue.ts
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
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* 基于数组的循环队列
* 为了方便判断队列为空的情况,这里最好引入元素个数
*/
class CircularQueue<T> {
private items: T[] = []
private readonly n: number = 0
private head: number = 0
private tail: number = 0
// 队列的实际元素大小
size: number = 0
constructor(capacity: number) {
this.n = capacity
}
public enqueue(item: T): boolean {
// 表示队列已经满了
if (this.size === this.n) return false
this.items[this.tail] = item
this.tail = (this.tail + 1) % this.n
this.size++
return true
}
public dequeue(): T | null {
if (!this.size) return null
const item = this.items[this.head]
this.head = (this.head + 1) % this.n
this.size--
return item
}
}
const circularQueue = new CircularQueue<number>(3)
circularQueue.enqueue(1)
circularQueue.enqueue(2)
circularQueue.enqueue(3)
circularQueue.enqueue(4)
const value = circularQueue.dequeue()
const value1 = circularQueue.dequeue()
const value2 = circularQueue.dequeue()
const value3 = circularQueue.dequeue()
// null
console.log(value3)
// 0
console.log(circularQueue.size)