-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path622.-设计循环队列.ts
43 lines (36 loc) · 1.04 KB
/
622.-设计循环队列.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
class MyCircularQueue {
private n: number = 0
private headIndex: number = 0
private count: number = 0
private queue: number[] = []
constructor(k: number) {
this.n = k
}
enQueue(value: number): boolean {
if(this.count === this.n) return false
this.queue[this.headIndex + this.count === this.n ? 0 : this.headIndex + this.count] = value
this.count++
return true
}
deQueue(): boolean {
if(this.count === 0) return false
this.headIndex = this.headIndex +1 === this.n ? 0 : this.headIndex +1
this.count--
return true
}
Front(): number {
if(this.isEmpty()) return -1
return this.queue[this.headIndex]
}
Rear(): number {
if(this.isEmpty()) return -1
let idx = this.headIndex + (this.count-1) === this.n ? 0 : this.headIndex + (this.count-1)
return this.queue[idx]
}
isEmpty(): boolean {
return this.count === 0
}
isFull(): boolean {
return this.count === this.n
}
}