|
@@ -0,0 +1,72 @@
|
|
|
+type MyCircularQueue struct {
|
|
|
+ queue []int
|
|
|
+ size int
|
|
|
+ length int
|
|
|
+ beg int
|
|
|
+ end int
|
|
|
+}
|
|
|
+
|
|
|
+/** Initialize your data structure here. Set the size of the queue to be k. */
|
|
|
+func Constructor(k int) (queue MyCircularQueue) {
|
|
|
+ queue.queue = make([]int, k)
|
|
|
+ queue.size = k
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+/** Insert an element into the circular queue. Return true if the operation is successful. */
|
|
|
+func (this *MyCircularQueue) EnQueue(value int) bool {
|
|
|
+ if this.IsFull() {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ this.queue[this.end] = value
|
|
|
+ this.end = (this.end + 1) % this.size
|
|
|
+ this.length++
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+/** Delete an element from the circular queue. Return true if the operation is successful. */
|
|
|
+func (this *MyCircularQueue) DeQueue() bool {
|
|
|
+ if this.IsEmpty() {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ this.beg = (this.beg + 1) % this.size
|
|
|
+ this.length--
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+/** Get the front item from the queue. */
|
|
|
+func (this *MyCircularQueue) Front() int {
|
|
|
+ if this.IsEmpty() {
|
|
|
+ return -1
|
|
|
+ }
|
|
|
+ return this.queue[this.beg]
|
|
|
+}
|
|
|
+
|
|
|
+/** Get the last item from the queue. */
|
|
|
+func (this *MyCircularQueue) Rear() int {
|
|
|
+ if this.IsEmpty() {
|
|
|
+ return -1
|
|
|
+ }
|
|
|
+ return this.queue[(this.end+this.size-1)%this.size]
|
|
|
+}
|
|
|
+
|
|
|
+/** Checks whether the circular queue is empty or not. */
|
|
|
+func (this *MyCircularQueue) IsEmpty() bool {
|
|
|
+ return this.length == 0
|
|
|
+}
|
|
|
+
|
|
|
+/** Checks whether the circular queue is full or not. */
|
|
|
+func (this *MyCircularQueue) IsFull() bool {
|
|
|
+ return this.length == this.size
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Your MyCircularQueue object will be instantiated and called as such:
|
|
|
+ * obj := Constructor(k);
|
|
|
+ * param_1 := obj.EnQueue(value);
|
|
|
+ * param_2 := obj.DeQueue();
|
|
|
+ * param_3 := obj.Front();
|
|
|
+ * param_4 := obj.Rear();
|
|
|
+ * param_5 := obj.IsEmpty();
|
|
|
+ * param_6 := obj.IsFull();
|
|
|
+ */
|