package main
import (
"fmt"
"strconv"
)
type Node struct {
data interface{}
pre *Node
next *Node
}
type DoubleLinkedList struct {
length int
head *Node
tail *Node
}
func NewDoubleLinkedList() DoubleLinkedList {
node := Node{}
l := DoubleLinkedList{
length: 0,
head: &node,
}
l.tail = l.head
return l
}
func (l *DoubleLinkedList) Head() *Node {
return l.head
}
func (l *DoubleLinkedList) Tail() *Node {
return l.tail
}
func (l *DoubleLinkedList) Length() int {
return l.length
}
func (l *DoubleLinkedList) empty() bool {
return l.head.next == nil
}
func (l *DoubleLinkedList) exist(value interface{}) bool {
cur := l.head.next
for {
if cur == nil {
return false
}
if cur.data == value {
return true
}
cur = cur.next
}
}
func (l *DoubleLinkedList) Get(index int) interface{} {
n := 0
cur := l.head.next
if index >= l.length || index < 0 {
return -1
}
for {
if cur == nil {
return false
}
if n == index {
return cur.data
}
cur = cur.next
}
}
func (l *DoubleLinkedList) Add(value interface{}) {
cur := l.head
newNode := Node{
data: value,
}
newNode.next = cur.next
newNode.pre = cur
if cur.next == nil {
l.tail = &newNode
} else {
cur.next.pre = &newNode
}
cur.next = &newNode
l.length++
}
func (l *DoubleLinkedList) Append(value interface{}) {
newNode := Node{data: value}
newNode.pre = l.tail
l.tail.next = &newNode
l.tail = &newNode
l.length++
}
func (l *DoubleLinkedList) Insert(index int, value interface{}) {
if index < 0 {
panic("位置插入不能为:" + strconv.Itoa(index))
}
newNode := Node{data: value}
cur := l.head
n := 0
for {
if cur.next == nil {
break
}
if n == index {
newNode.next = cur.next
newNode.pre = cur
cur.next.pre = cur
cur.next = &newNode
l.length++
return
}
n++
cur = cur.next
}
newNode.pre = cur
cur.next = &newNode
l.tail = &newNode
l.length++
}
func (l *DoubleLinkedList) Index(value interface{}) int {
cur := l.head.next
n := 0
for {
if cur == nil {
return -1
}
if cur.data == value {
return n
}
n++
cur = cur.next
}
}
func (l *DoubleLinkedList) Delete(index int) {
cur := l.head
n := 0
for {
if cur != nil && cur.next != nil {
if n == index {
cur.next = cur.next.next
if cur.next == nil {
l.tail = cur
} else {
cur.next.pre = cur
}
l.length--
}
n++
cur = cur.next
continue
}
panic(fmt.Sprintf("删除下标不存在, %v", index))
}
}
func (l *DoubleLinkedList) Remove(value interface{}) {
cur := l.head
for {
if cur != nil && cur.next != nil {
if cur.next.data == value {
cur.next = cur.next.next
if cur.next == nil {
l.tail = cur
} else {
cur.next.pre = cur
}
l.length--
return
}
cur = cur.next
continue
}
panic(fmt.Sprintf("删除数据不存在, %v", value))
}
}
func (l *DoubleLinkedList) Pop() interface{} {
if l.head.next == nil {
panic("链表为空")
}
cur := l.tail.pre
data := l.tail.data
cur.next = nil
l.tail = cur
l.length--
return data
}
func (l *DoubleLinkedList) Show() {
cur := l.head.next
for {
if cur == nil {
break
}
fmt.Print(cur.data, " ")
cur = cur.next
}
fmt.Println()
}
func main() {
l := NewDoubleLinkedList()
l.Add(1)
l.Add(2)
l.Add(3)
l.Add(4)
l.Append(5)
l.Append(6)
l.Insert(6, 10)
l.Show()
l.Remove(4)
l.Show()
fmt.Println(l.Pop())
fmt.Println(l.Pop())
fmt.Println(l.Tail().data)
l.Show()
}
【数据结构连载一线性表】【双向链表】golang
最新推荐文章于 2024-05-21 22:35:44 发布