双向链表(golang)

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
	*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值