package main
import (
"errors"
"fmt"
)
type LinkNode struct {
Value interface{}
Last *LinkNode //上一个节点地址
Next *LinkNode //下一个节点地址
}
type LinkList struct {
Head *LinkNode
Size int //链表的中的元素个数
Num int //链表最大度
}
func NewLinkList(num int) *LinkList {
if num <= 0 {
num = 1000
}
return &LinkList{
Num: num,
}
}
//在头部插入
func (this *LinkList) First(value interface{}) (bool, error) {
if this.Size >= this.Num {
return false, errors.New("please expand,capacity reached the upper limit")
}
if this.Head == nil {
node := &LinkNode{
Value: value,
}
this.Head = node
this.Size++
} else {
node := &LinkNode{
Value: value,
Last: nil,
Next: this.Head,
}
this.Head.Last = node
this.Head = node
this.Size++
}
return true, nil
}
//在尾部插入
func (this *LinkList) After(value interface{}) (bool, error) {
if this.Size >= this.Num {
return false, errors.New("please expand,capacity reached the upper limit")
}
head := this.Head
for {
if head.Next != nil {
head = head.Next
} else {
break
}
}
node := &LinkNode{
Value: value,
Last: head,
}
head.Next = node
this.Size++
return true, nil
}
//遍历链表
func (this *LinkList) LRange() (list []interface{}) {
head := this.Head
if head == nil {
return
}
list = append(list, head.Value)
for {
if head.Next != nil {
head = head.Next
list = append(list, head.Value)
} else {
break
}
}
return
}
//检查链表是否为空
func (this *LinkList) IsEmpty() bool {
if this.Head == nil {
return true
}
return false
}
func (this *LinkList) GetSize() int {
return this.Size
}
//根据索引位置删除
func (this *LinkList) DeleteByIndex(index int) (bool, error) {
if index < 0 || index >= this.Size {
return false, errors.New("index out of bounds")
}
head := this.Head
if head == nil {
return false, errors.New("list is nil")
}
if index == 0 {
//fmt.Printf("%#v\n",head)
if head.Next != nil {
this.Head = head.Next
this.Head.Last = nil
this.Size--
} else {
this.Head = nil
this.Size--
}
return true, nil
}
var count int = 1
for {
head = head.Next
//head = head.Next
if count == index {
break
}
count++
}
if head.Next != nil {
head.Value = head.Next.Value
head.Next = head.Next.Next
} else {
head.Last.Next = nil
}
this.Size--
return true, nil
}
//根据值来删除
func (this *LinkList) DeleteByValue(value interface{}) (bool, error) {
head := this.Head
if head == nil {
return false, errors.New("list is nil")
}
if head.Value == value {
if head.Next != nil {
this.Head = this.Head.Next
this.Head.Last = nil
} else {
this.Head = nil
}
this.Size--
return true, nil
}
for {
head = head.Next
if head == nil {
break
}
if head.Value == value {
break
}
}
if head.Value != value {
return false, errors.New("%v not found in list")
}
if head.Next != nil {
head.Value = head.Next.Value
head.Next = head.Next.Next
} else {
head.Last.Next = nil
//head.Last = nil
}
this.Size--
return true, nil
}
func main() {
this := NewLinkList(-1)
this.First(10)
this.First(40)
this.After(20)
this.After(30)
this.First(1)
/*fmt.Printf("len:%d,arr:%v\n\n", this.GetSize(), this.LRange())
for i := 5; i > 0; i-- {
this.DeleteByIndex(this.GetSize()-1)
fmt.Printf("len:%d,arr:%v\n", this.GetSize(), this.LRange())
}*/
fmt.Printf("len:%d,arr:%v\n\n", this.GetSize(), this.LRange())
for i := 0; i < 5; i++ {
this.DeleteByIndex(0)
fmt.Printf("len:%d,arr:%v\n", this.GetSize(), this.LRange())
}
/*fmt.Printf("this size:%d\n",this.GetSize())
fmt.Println(this.LRange())// 1,40,10,20,30
fmt.Println()
this.DeleteByValue(1)
fmt.Println(this.LRange())// 40,10,20,30
fmt.Printf("this size:%d\n",this.GetSize())
fmt.Println()
this.DeleteByValue(20)
fmt.Println(this.LRange())// 40,10,30
fmt.Printf("this size:%d\n",this.GetSize())
fmt.Println()
this.DeleteByValue(30)
fmt.Println(this.LRange())// 40,10
fmt.Printf("this size:%d\n",this.GetSize())
fmt.Println()
this.DeleteByValue(10)
fmt.Println(this.LRange())// 40
fmt.Printf("this size:%d\n",this.GetSize())
fmt.Println()
this.DeleteByValue(40)
fmt.Println(this.LRange())// []
fmt.Printf("this size:%d\n",this.GetSize())
*/
fmt.Println("-------------------------------")
/*
fmt.Println(this.LRange())//1,40,10,20,30
this.DeleteByIndex(0)
//fmt.Printf("this size:%d\n",this.GetSize())
fmt.Println(this.LRange())//40,10,20,30
fmt.Println()
this.DeleteByIndex(this.GetSize()-1)
fmt.Println(this.LRange())// 40,10,20
fmt.Println()
this.DeleteByIndex(1)
fmt.Println(this.LRange())// 40,20
fmt.Println()
this.DeleteByIndex(1)
fmt.Println(this.LRange())// 40
fmt.Printf("this size:%d\n",this.GetSize()) //1
fmt.Println()
_,err :=this.DeleteByIndex(this.GetSize()-1)
fmt.Printf("del error:%v\n",err)
fmt.Println(this.LRange())// 40
fmt.Printf("this size:%d\n",this.GetSize()) //1
fmt.Println()
_,err =this.DeleteByIndex(0)
fmt.Printf("del error:%v\n",err)
fmt.Println(this.LRange())// 40
fmt.Printf("this size:%d\n",this.GetSize()) //1
*/
}
双向链表(golang)
最新推荐文章于 2024-05-21 13:08:02 发布