type MyCircularQueue struct {
Parr []int
Head int
Tail int
Length int
Cap int
}
func Constructor(k int) MyCircularQueue {
return MyCircularQueue{
Parr: make([]int, k),
Head: 0,
Tail: 0,
Length: k,
Cap: 0,
}
}
func (this *MyCircularQueue) EnQueue(value int) bool {
if this.Head == this.Tail && this.Cap > 0{
return false
}
this.Parr[this.Tail] = value
this.Tail = this.Tail + 1
if this.Tail == this.Length {
this.Tail = 0
}
this.Cap = this.Cap + 1
return true
}
func (this *MyCircularQueue) DeQueue() bool {
if this.Head == this.Tail && this.Cap == 0 {
return false
}
this.Head = this.Head + 1
if this.Head == this.Length {
this.Head = 0
}
this.Cap = this.Cap - 1
return true
}
func (this *MyCircularQueue) Front() int {
if this.IsEmpty() {
return -1
}
return this.Parr[this.Head]
}
func (this *MyCircularQueue) Rear() int {
if this.IsEmpty() {
return -1
}
index := (this.Tail - 1 + this.Length) % this.Length
return this.Parr[index]
}
func (this *MyCircularQueue) IsEmpty() bool {
if this.Tail == this.Head && this.Cap == 0{
return true
}else {
return false
}
}
func (this *MyCircularQueue) IsFull() bool {
if this.Head == this.Tail && this.Cap > 0{
return true
}else {
return false
}
}
/**
* 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();
*/
循环队列leetcode面试解答
最新推荐文章于 2021-11-08 15:48:02 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)