1+ type MyCircularDeque struct {
2+ data []int
3+ cap int
4+ count int
5+ head int
6+ tail int
7+ }
8+
9+
10+ /** Initialize your data structure here. Set the size of the deque to be k. */
11+ func Constructor (k int ) MyCircularDeque {
12+ t := MyCircularDeque {}
13+ t .data = make ([]int , k )
14+ t .cap = k
15+ t .count = 0
16+ t .head = - 1
17+ t .tail = - 1
18+ return t
19+ }
20+
21+
22+ /** Adds an item at the front of Deque. Return true if the operation is successful. */
23+ func (this * MyCircularDeque ) InsertFront (value int ) bool {
24+ if this .count == this .cap {
25+ return false
26+ }
27+ this .head = (this .head - 1 + this .cap ) % this .cap
28+ this .data [this .head ] = value
29+ if this .tail == - 1 {
30+ this .tail = this .head
31+ }
32+ this .count ++
33+ return true
34+ }
35+
36+
37+ /** Adds an item at the rear of Deque. Return true if the operation is successful. */
38+ func (this * MyCircularDeque ) InsertLast (value int ) bool {
39+ if this .count == this .cap {
40+ return false
41+ }
42+ this .tail = (this .tail + 1 + this .cap ) % this .cap
43+ this .data [this .tail ] = value
44+ if this .head == - 1 {
45+ this .head = this .tail
46+ }
47+ this .count ++
48+ return true
49+ }
50+
51+
52+ /** Deletes an item from the front of Deque. Return true if the operation is successful. */
53+ func (this * MyCircularDeque ) DeleteFront () bool {
54+ if this .count == 0 {
55+ return false
56+ }
57+ this .data [this .head ] = 0
58+ this .head = (this .head + 1 + this .cap ) % this .cap
59+ this .count --
60+ return true
61+ }
62+
63+
64+ /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
65+ func (this * MyCircularDeque ) DeleteLast () bool {
66+ if this .count == 0 {
67+ return false
68+ }
69+ this .data [this .tail ] = 0
70+ this .tail = (this .tail - 1 + this .cap ) % this .cap
71+ this .count --
72+ return true
73+ }
74+
75+
76+ /** Get the front item from the deque. */
77+ func (this * MyCircularDeque ) GetFront () int {
78+ if this .count == 0 {
79+ return - 1
80+ }
81+ return this .data [this .head ]
82+ }
83+
84+
85+ /** Get the last item from the deque. */
86+ func (this * MyCircularDeque ) GetRear () int {
87+ if this .count == 0 {
88+ return - 1
89+ }
90+ return this .data [this .tail ]
91+ }
92+
93+
94+ /** Checks whether the circular deque is empty or not. */
95+ func (this * MyCircularDeque ) IsEmpty () bool {
96+ return this .count == 0
97+ }
98+
99+
100+ /** Checks whether the circular deque is full or not. */
101+ func (this * MyCircularDeque ) IsFull () bool {
102+ return this .count == this .cap
103+ }
104+
105+
106+ /**
107+ * Your MyCircularDeque object will be instantiated and called as such:
108+ * obj := Constructor(k);
109+ * param_1 := obj.InsertFront(value);
110+ * param_2 := obj.InsertLast(value);
111+ * param_3 := obj.DeleteFront();
112+ * param_4 := obj.DeleteLast();
113+ * param_5 := obj.GetFront();
114+ * param_6 := obj.GetRear();
115+ * param_7 := obj.IsEmpty();
116+ * param_8 := obj.IsFull();
117+ */
118+
0 commit comments