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.headNode = node
}
4. AddFromTail()
func (l *DoublyLoopLinkedList) AddFromTail(data any) {
node := &DLNode{Data: data}
if l.IsEmpty() {
l.headNode = node
return
}
currentNode := l.headNode
for currentNode != l.headNode {
currentNode = currentNode.Next
}
currentNode.Next = node
l.headNode.Prev = node
node.Prev = currentNode
node.Next = l.headNode
}
5. Insert()
func (l *DoublyLoopLinkedList) Insert(position int, data any) {
if position <= 0 {
l.AddFromHead(data)
return
} else if position >= l.Length()-1 {
l.AddFromTail(data)
return
} else {
node := &DLNode{Data: data}
count := 0
currentNode := l.headNode
for count != position {
count++
currentNode = currentNode.Next
}
node.Next = currentNode
node.Prev = currentNode.Prev
currentNode.Prev.Next = node
currentNode.Prev = node
}
}
6. DeleteHead()
func (l *DoublyLoopLinkedList) DeleteHead() {
if l.IsEmpty() {
fmt.Println("Doubly Linked List is Empty")
return
}
currentNode := l.headNode
if currentNode.Next == l.headNode {
l.headNode = nil
return
} else {
for currentNode.Next != l.headNode {
currentNode = currentNode.Next
}
currentNode.Next = l.headNode.Next
l.headNode.Next.Prev = currentNode
l.headNode = l.headNode.Next
}
}
7. DeleteTail()
func (l *DoublyLoopLinkedList) DeleteTail() {
if l.IsEmpty() {
fmt.Println("Doubly Linked List is Empty")
return
}
currentNode := l.headNode
if currentNode.Next == l.headNode {
l.headNode = nil
return
} else {
for currentNode.Next != l.headNode {
currentNode = currentNode.Next
}
currentNode.Prev.Next = l.headNode
l.headNode.Prev = currentNode.Prev
}
}
8. Remove()
func (l *DoublyLoopLinkedList) Remove(position int) {
if position <= 0 {
l.DeleteHead()
return
} else if position >= l.Length()-1 {
l.DeleteTail()
return
} else {
count := 0
currentNode := l.headNode
for count != position {
count++
currentNode = currentNode.Next
}
currentNode.Prev.Next = currentNode.Next
currentNode.Next.Prev = currentNode.Prev
}
}
9. RemoveByValue()
func (l *DoublyLoopLinkedList) RemoveByValue(data any) {
if l.IsEmpty() {
fmt.Println("Doubly Linked List is Empty")
return
}
currentNode := l.headNode
if currentNode.Data == data {
l.DeleteHead()
return
}
for currentNode.Next != l.headNode {
if currentNode.Next.Data == data {
currentNode.Next.Prev.Next = currentNode.Next.Next
currentNode.Next.Next.Prev = currentNode.Next.Prev
return
} else {
currentNode = currentNode.Next
}
}
}
10. Contain()
func (l *DoublyLoopLinkedList) Contain(data any) bool {
if l.IsEmpty() {
return false
}
currentNode := l.headNode
for currentNode.Next != l.headNode {
if currentNode.Data == data {
return true
} else {
currentNode = currentNode.Next
}
}
if currentNode.Data == data {
return true
}
return false
}
11. Traverse()
func (l *DoublyLoopLinkedList) Traverse() {
if l.IsEmpty() {
fmt.Println("Doubly Linked List is Empty")
return
}
currentNode := l.headNode
for currentNode.Next != l.headNode {
fmt.Printf("%v -> ", currentNode.Data)
currentNode = currentNode.Next
}
fmt.Printf("%v\n", currentNode.Data)
}