-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
69 lines (58 loc) · 1.16 KB
/
index.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
68
69
package design_circular_deque
import "errors"
type MyCircularDeque struct {
capacity int
deque []int
}
func Constructor(capacity int) MyCircularDeque {
if capacity < 1 {
panic(errors.New("k greater than or equal one"))
}
return MyCircularDeque{capacity: capacity}
}
func (d *MyCircularDeque) InsertFront(value int) bool {
if d.IsFull() {
return false
}
d.deque = append([]int{value}, d.deque...)
return true
}
func (d *MyCircularDeque) InsertLast(value int) bool {
if d.IsFull() {
return false
}
d.deque = append(d.deque, value)
return true
}
func (d *MyCircularDeque) DeleteFront() bool {
if d.IsEmpty() {
return false
}
d.deque = d.deque[1:]
return true
}
func (d *MyCircularDeque) DeleteLast() bool {
if d.IsEmpty() {
return false
}
d.deque = d.deque[:len(d.deque)-1]
return true
}
func (d *MyCircularDeque) GetFront() int {
if d.IsEmpty() {
return -1
}
return d.deque[0]
}
func (d *MyCircularDeque) GetRear() int {
if d.IsEmpty() {
return -1
}
return d.deque[len(d.deque)-1]
}
func (d *MyCircularDeque) IsEmpty() bool {
return len(d.deque) == 0
}
func (d *MyCircularDeque) IsFull() bool {
return len(d.deque) >= d.capacity
}