type MyCircularQueue struct {
arr []int
maxsize int
front int
rear int
}
/*Initialize your data structure here. Set the size of the queue to be k. */
func Constructor(k int) MyCircularQueue {
return MyCircularQueue{
arr: make([]int, k),
maxsize:k,
}
}
/*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.arr[this.rear] = value
this.rear = (this.rear + 1)%this.maxsize
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.arr[this.front] = 0
this.front = (this.front + this.maxsize + 1) % this.maxsize
return true
}
/*Get the front item from the queue. */
func (this *MyCircularQueue) Front() int {
return this.arr[this.front]
}
/*Get the last item from the queue. */
func (this *MyCircularQueue) Rear() int {
return this.arr[(this.rear + this.maxsize)%this.maxsize]
}
/*Checks whether the circular queue is empty or not. */
func (this *MyCircularQueue) IsEmpty() bool {
return this.rear== this.front
}
/*Checks whether the circular queue is full or not. */
func (this *MyCircularQueue) IsFull() bool {
return (this.rear+this.maxsize-this.front)%this.maxsize == this.maxsize -1
}
func (this *MyCircularQueue) Show() {
for i := this.front; i < this.front + (this.rear-this.front+this.maxsize)%this.maxsize; i++ {
fmt.Println("index is %d, val is %d", (i)%this.maxsize, this.arr[i%this.maxsize])
}
}
队列概念:有序列表,可用数组或链表实现,先入先出数组实现:需要环形队列,否则不能重复利用数组实现环形队列实现(golang实现)type MyCircularQueue struct { arr []int maxsize int front int rear int}/*Initialize your data structure here. Set the size of the queue to be k. */func Constructor(k i