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