0. 定义节点
type DLNode struct {
Data any
Prev, Next *DLNode
}
type DoublyLoopLinkedList struct {
headNode *DLNode
}
1. IsEmpty()
func (l *DoublyLoopLinkedList) IsEmpty() bool {
if l.headNode == nil {
return true
}
return false
}
2. Length()
func (l *DoublyLoopLinkedList) Length() int {
if l.IsEmpty() {
return 0
}
currentNode := l.headNode
count := 1
for currentNode.Next != l.headNode {
count++
currentNode = currentNode.Next
}
return count
}
3. AddFromHead()
func (l *DoublyLoopLinkedList) AddFromHead(data any) {
node := &DLNode{
Data: data}
if l.IsEmpty() {
l.headNode = node
return
}
currentNode := l.headNode
for currentNode.Next != l.headNode {
currentNode = currentNode.Next
}
currentNode.Next = node
l.headNode.Prev = node
node.Prev = currentNode
node.Next = l.headNode
l.headN