Golang-链表

一.链表的实现

1.1 代码

package Algorithm

import (
	_ "container/list"
	"errors"
	"fmt"
)
type Node struct {
	data   interface{}
	pre   *Node
	next  *Node
}

type List struct {
	head *Node
	tail *Node
	length uint64
}

func GetList() *List{
	list := &List{
		head: nil,
		tail:nil,
		length:0,
	}
	return list
}

func (list *List)GetSize() uint64{
		return list.length
}

func (list *List) Add(data interface{}) error{
	node := &Node{data:data}
	if list.length == 0 && list.head == nil {
		list.head = node
		list.tail = node
		node.next = nil
		node.pre = nil
	} else {
		list.tail.next = node
		node.pre = list.tail
		node.next = nil
		list.tail = node
	}
	list.length++
	return nil
}

func (list *List) Find(node *Node) (error, bool) {
	if node == nil {
		return errors.New("node is nil"), false
	}
	if list.length == 0 || list.head == nil || list.tail == nil{
		return errors.New("list is nil"), false
	}
	pHead := list.head
	for pHead != nil {
		if node == pHead {
			return nil, true
		}
		pHead = pHead.next
	}
	return errors.New("not find the node"), false
}

func (list *List) Get(index uint64) (error, interface{}) {
	if index < 0 || index > list.length -1 {
		return errors.New("the index is invalid"), nil
	}
	pHead := list.head
	var count uint64 = 0
	for pHead != nil {
		if count == index {
			return nil, pHead.data
		}

		pHead = pHead.next
		count++
	}
	return nil, nil
}

func (list * List) insertNode(index uint64, node *Node) error {
	pHead := list.head
	count := 0
	for  pHead != nil {
		if index == uint64(count +1) {
			node.next = pHead.next
			pHead.next = node
			node.next.pre = node
			node.pre = pHead
			list.length++
			return nil
		}
		pHead = pHead.next
		count++
	}
	return nil
}

func  (list *List) Insert(index uint64, data interface{}) error {
	if index > list.length -1 || index < 0 {
		return errors.New("the index is invalid")
	}
	if index == list.length -1 || index == 0 {
		return list.Add(data)
	}
	node := &Node{
		data:data,
	}
	return list.insertNode(index, node)
}

func (list *List) remove(node *Node) {
	node.pre.next = node.next
	node.next.pre = node.pre
	node.next = nil
	node.pre = nil
	list.length--
}
func (list *List) Delete(index uint64) error {
	if index < 0 || index > list.length-1 {
		return errors.New("index is invalid")
	}
	pHead := list.head
	var count uint64 = 0
	for pHead != nil {
		if count == index {
			if count == 0 {
				list.head = list.head.next
				list.head.pre = nil
				list.length--
				return nil
			}
			if count == list.length -1 {
				list.tail.pre.next = nil
				list.tail = list.tail.pre
				list.length--
				return nil
			}
			list.remove(pHead)
			list.length--
			return nil
		}
		pHead = pHead.next
		count++
	}
	return nil

}

func (list *List) Update(index uint64, data interface{}) error {
	if index > list.length -1 || index < 0 {
		return errors.New("the index is invalid")
	}
	pHead := list.head
	var count uint64 = 0
	for pHead != nil {
		if count == index {
			pHead.data = data
			return nil
		}
		pHead = pHead.next
		count++
	}
	return nil
}

func (list *List) Show() {
	pHead := list.head
	for pHead != nil {
		fmt.Println(pHead.data)
		pHead = pHead.next
	}
}

1.2 测试

package main

import (
	"./Algorithm"
	"encoding/json"
	"fmt"
)

func main() {
	list := Algorithm.GetList()
	for i := 0; i < 51; i++ {
		list.Add(i)
	}

	fmt.Println("before delete, the list size is:", list.GetSize())
	//list.Show()
	fmt.Println(list.Insert(48, 666))
	list.Show()
	fmt.Printf("\n")
	fmt.Println(list.Delete(0))
	fmt.Println(list.Update(50, 999))
	fmt.Println(list.Delete(2))
	list.Show()
	fmt.Println("after delete, the list size is:", list.GetSize())
	fmt.Println(list.Get(25))


	fmt.Println(json.Marshal(list))
	return
}

1.3 输出

GOROOT=C:\Go #gosetup
GOPATH=E:\GoCode;C:\Users\84553\go #gosetup
C:\Go\bin\go.exe build -o C:\Users\84553\AppData\Local\Temp\___8go_build_main_go.exe E:\GoCode\test\main.go #gosetup
C:\Users\84553\AppData\Local\Temp\___8go_build_main_go.exe #gosetup
before delete, the list size is: 51
<nil>
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
666
48
49
50

<nil>
<nil>
<nil>
1
2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
666
48
49
999
after delete, the list size is: 49
<nil> 27
[123 125] <nil>

Process finished with exit code 0

二.链表的操作

2.1 链表的反转

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值