🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## golang-链表实现 <details> <summary>main.go </summary> ``` 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 } ``` </details> <br /> ## golang-链表实现 <details> <summary> main.go</summary> ``` type MyCircularQueue struct { size int hasSize int cur int last int first int list []int } func Constructor(k int) MyCircularQueue { return MyCircularQueue{ size: k, hasSize: 0, list: make([]int, k), first: 0, last: 0, cur: 0, } } func (this *MyCircularQueue) EnQueue(value int) bool { if this.IsFull() { return false } this.list[this.cur] = value this.last = this.cur this.cur++ if this.cur >= this.size { this.cur = 0 } this.hasSize++ return true } func (this *MyCircularQueue) DeQueue() bool { if this.IsEmpty() { return false } this.first++ if this.first >= this.size { this.first = 0 } this.hasSize-- return true } func (this *MyCircularQueue) Front() int { if this.IsEmpty() { return -1 } return this.list[this.first] } func (this *MyCircularQueue) Rear() int { if this.IsEmpty() { return -1 } return this.list[this.last] } 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 } ``` </details> <br />