-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp641.go
67 lines (57 loc) · 1.42 KB
/
p641.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package p641
type MyCircularDeque struct {
front, rear int
elements []int
}
func Constructor(k int) MyCircularDeque {
return MyCircularDeque{elements: make([]int, k+1)}
}
func (this *MyCircularDeque) InsertFront(value int) bool {
if this.IsFull() {
return false
}
this.front = (this.front - 1 + len(this.elements)) % len(this.elements)
this.elements[this.front] = value
return true
}
func (this *MyCircularDeque) InsertLast(value int) bool {
if this.IsFull() {
return false
}
this.elements[this.rear] = value
this.rear = (this.rear + 1 + len(this.elements)) % len(this.elements)
return true
}
func (this *MyCircularDeque) DeleteFront() bool {
if this.IsEmpty() {
return false
}
this.front = (this.front + 1 + len(this.elements)) % len(this.elements)
return true
}
func (this *MyCircularDeque) DeleteLast() bool {
if this.IsEmpty() {
return false
}
this.rear = (this.rear - 1 + len(this.elements)) % len(this.elements)
return true
}
func (this *MyCircularDeque) GetFront() int {
if this.IsEmpty() {
return -1
}
return this.elements[this.front]
}
func (this *MyCircularDeque) GetRear() int {
if this.IsEmpty() {
return -1
}
idx := (this.rear - 1 + len(this.elements)) % len(this.elements)
return this.elements[idx]
}
func (this *MyCircularDeque) IsEmpty() bool {
return this.front == this.rear
}
func (this *MyCircularDeque) IsFull() bool {
return (this.rear+1)%len(this.elements) == this.front
}