循环队列
最后更新于:2022-04-02 04:22:05
[TOC]
## golang-链表实现
## golang-链表实现
';
main.go
``` type QueLink struct { val int exist bool next *QueLink } type MyCircularQueue struct { size int hasSize int first *QueLink last *QueLink cur *QueLink } func Constructor(k int) MyCircularQueue { root := &QueLink{} first := &QueLink{} for i := 0; i < k; i++ { if i == 0 { root.next = &QueLink{} first = root root = root.next } else if i == k-1 { root.next = first } else { root.next = &QueLink{} root = root.next } } return MyCircularQueue{ size: k, hasSize: 0, first: first, last: first, cur: first, } } func (this *MyCircularQueue) EnQueue(value int) bool { if this.IsFull() { return false } this.cur.val = value this.cur.exist = true if this.IsEmpty() { this.first = this.cur } this.last = this.cur this.cur = this.cur.next this.hasSize++ return true } func (this *MyCircularQueue) DeQueue() bool { if this.IsEmpty() { return false } this.first.exist = false this.first = this.first.next this.hasSize-- return true } func (this *MyCircularQueue) Front() int { if this.IsEmpty() { return -1 } return this.first.val } func (this *MyCircularQueue) Rear() int { if this.IsEmpty() { return -1 } return this.last.val } func (this *MyCircularQueue) IsEmpty() bool { if this.hasSize == 0 { return true } return false } func (this *MyCircularQueue) IsFull() bool { if this.hasSize == this.size { return true } return false } ```## golang-链表实现