package main
import (
"fmt"
"strconv"
)
type Node struct {
data interface { }
pre * Node
next * Node
}
type DoubleLinkedList struct {
length int
head * Node
}
func NewDoubleLinkedList ( ) DoubleLinkedList {
node := Node{ }
l := DoubleLinkedList{
length: 0 ,
head: & node,
}
l. head. next = l. head
l. head. pre = l. head
return l
}
func ( l * DoubleLinkedList) Head ( ) * Node {
return l. head
}
func ( l * DoubleLinkedList) Tail ( ) * Node {
return l. head. pre
}
func ( l * DoubleLinkedList) Length ( ) int {
return l. length
}
func ( l * DoubleLinkedList) empty ( ) bool {
return l. head. next == l. head
}
func ( l * DoubleLinkedList) exist ( value interface { } ) bool {
cur := l. head. next
for {
if cur == l. head {
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 == l. head {
return - 1
}
if n == index {
return cur. data
}
cur = cur. next
n++
}
}
func ( l * DoubleLinkedList) Add ( value interface { } ) {
cur := l. head
newNode := Node{
data: value,
}
newNode. next = cur. next
newNode. pre = cur
cur. next. pre = & newNode
cur. next = & newNode
l. length++
}
func ( l * DoubleLinkedList) Append ( value interface { } ) {
newNode := Node{ data: value}
newNode. pre = l. head. pre
newNode. next = l. head
l. head. pre. next = & newNode
l. head. pre = & 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 == l. head {
break
}
if n == index {
break
}
n++
cur = cur. next
}
newNode. pre = cur
newNode. next = cur. next
cur. next. pre = & newNode
cur. next = & newNode
l. length++
}
func ( l * DoubleLinkedList) Index ( value interface { } ) int {
cur := l. head. next
n := 0
for {
if cur == l. head {
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. next != l. head {
if n == index {
cur. next. pre = cur
cur. next = cur. next. next
l. length--
return
}
n++
cur = cur. next
continue
}
panic ( fmt. Sprintf ( "删除下标不存在, %v" , index) )
}
}
func ( l * DoubleLinkedList) Remove ( value interface { } ) {
cur := l. head
for {
if cur. next != l. head {
if cur. next. data == value {
cur. next. pre = cur
cur. next = cur. next. next
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. head. pre
data := cur. data
cur. pre. next = l. head
l. head. pre = cur. pre
l. length--
return data
}
func ( l * DoubleLinkedList) Show ( ) {
cur := l. head. next
for {
if cur == l. head {
break
}
fmt. Print ( cur. data, " " )
cur = cur. next
}
fmt. Println ( )
}
func main ( ) {
l := NewDoubleLinkedList ( )
fmt. Println ( l. empty ( ) )
l. Add ( 1 )
l. Add ( 2 )
l. Add ( 3 )
l. Add ( 4 )
l. Append ( 5 )
l. Append ( 6 )
l. Insert ( 2 , 10 )
l. Show ( )
fmt. Println ( l. Pop ( ) )
fmt. Println ( l. Pop ( ) )
fmt. Println ( l. Tail ( ) . data)
l. Show ( )
}