ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[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 />