q5/listhead.go
aozhiwei 190668be5f 1
2020-10-26 12:58:22 +08:00

55 lines
877 B
Go

package q5
type ListHead struct {
next *ListHead
prev *ListHead
data interface{}
}
func (this *ListHead) Init() {
this.next = this
this.prev = this
this.data = nil
}
func (this *ListHead) Del() {
this.next.prev = this.prev
this.prev.next = this.next
}
func (this *ListHead) AddTail(pnew *ListHead) {
prev := this.prev
next := this
next.prev = pnew
pnew.next = next
pnew.prev = prev
prev.next = pnew
}
func (this *ListHead) FirstEntry() interface{} {
return this.next.data
}
func (this *ListHead) Replace(pnew *ListHead) {
pnew.next = this.next
pnew.next.prev = pnew
pnew.prev = this.prev
pnew.prev.next = pnew
pnew.data = this.data
}
func (this *ListHead) ReplaceInit(pnew *ListHead) {
this.Replace(pnew)
this.Init()
}
func (this *ListHead) Empty() bool {
return this.next == this
}
func (this *ListHead) DelInit() {
this.Del()
this.Init()
}