1 Star 0 Fork 0

LyleZhang/first-week

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
circular_deque.go 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
LyleZhang 提交于 2022-10-17 10:30 . 双端队列
package main
import "fmt"
type node struct {
val int
pre, nex *node
}
type MyCircularDeque struct {
cap, size int
head, rear *node
}
func Constructor(k int) MyCircularDeque {
head, rear := &node{}, &node{}
head.nex = rear
rear.pre = head
return MyCircularDeque{
cap: k,
head: head,
rear: rear,
}
}
func (this *MyCircularDeque) InsertFront(value int) bool {
if this.size == this.cap {
return false
} else {
node := node{
val: value,
}
nex := this.head.nex
this.head.nex, node.pre, node.nex, nex.pre = &node, this.head, nex, &node
this.size++
return true
}
}
func (this *MyCircularDeque) InsertLast(value int) bool {
if this.size == this.cap {
return false
} else {
node := node{
val: value,
}
fmt.Println(this.rear)
pre := this.rear.pre
this.rear.pre, node.pre, node.nex, pre.nex = &node, pre, this.rear, &node
this.size++
return true
}
}
func (this *MyCircularDeque) DeleteFront() bool {
if this.size == 0 {
return false
} else {
this.head.nex, this.head.nex.nex.pre = this.head.nex.nex, this.head
this.size--
return true
}
}
func (this *MyCircularDeque) DeleteLast() bool {
if this.size == 0 {
return false
} else {
this.rear.pre, this.rear.pre.pre.nex = this.rear.pre.pre, this.rear
this.size--
return true
}
}
func (this *MyCircularDeque) GetFront() int {
if this.size == 0 {
return -1
} else {
return this.head.nex.val
}
}
func (this *MyCircularDeque) GetRear() int {
if this.size == 0 {
return -1
} else {
return this.rear.pre.val
}
}
func (this *MyCircularDeque) IsEmpty() bool {
return this.size == 0
}
func (this *MyCircularDeque) IsFull() bool {
return this.size == this.cap
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/LyleZhang0824/first-week.git
git@gitee.com:LyleZhang0824/first-week.git
LyleZhang0824
first-week
first-week
main

搜索帮助