q5/listhead.go
aozhiwei 96e7c20cf0 1
2023-08-11 13:12:33 +08:00

107 lines
1.8 KiB
Go

package q5
type ListHead_Foreach_Func func(interface{}) bool
type ListHead struct {
next *ListHead
prev *ListHead
data interface{}
}
func (this *ListHead) Init(data interface{}) {
this.next = this
this.prev = this
this.data = data
}
func (this *ListHead) GetData() interface{} {
return this.data
}
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) AddHead(pnew *ListHead) {
prev := this
next := this.prev
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
}
func (this *ListHead) ReplaceInit(pnew *ListHead) {
this.Replace(pnew)
this.next = this
this.prev = this
}
func (this *ListHead) Empty() bool {
return this.next == this
}
func (this *ListHead) DelInit() {
this.Del()
this.next = this
this.prev = this
}
func (this *ListHead) Next() *ListHead {
return this.next
}
func (this *ListHead) ForEachFrom(from *ListHead,
cb ListHead_Foreach_Func) {
for pos := from; pos != this; pos = pos.next {
if !cb(pos.data) {
break
}
}
}
func (this *ListHead) ForEach(cb ListHead_Foreach_Func) {
for pos := this.next; pos != this; pos = pos.next {
if !cb(pos.data) {
break
}
}
}
func (this *ListHead) ForEach_r(cb ListHead_Foreach_Func) {
for pos := this.prev; pos != this; pos = pos.prev {
if !cb(pos.data) {
break
}
}
}
func MakeListHead () *ListHead{
l := new(ListHead)
l.Init(nil)
return l
}