list

package
v2.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RED 表示红色
	// RED represents red
	RED = 0

	// BLACK 表示黑色
	// BLACK represents black
	BLACK = 1
)

定义两个常量,表示节点的颜色 Define two constants to represent the color of the node

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List struct {
	// contains filtered or unexported fields
}

List 结构体代表一个双向链表,它有一个头节点和一个尾节点,以及一个记录链表长度的字段。 The List struct represents a doubly linked list. It has a head node, a tail node, and a field to record the length of the list.

func New

func New() *List

New 函数创建并返回一个新的 List 实例。 The New function creates and returns a new instance of List.

func (*List) Back

func (l *List) Back() *Node

Back 方法返回链表的尾节点。 The Back method returns the tail node of the list.

func (*List) Cleanup

func (l *List) Cleanup()

Cleanup 方法清理链表,移除所有节点并重置链表的状态。 The Cleanup method cleans up the list, removes all nodes and resets the state of the list.

func (*List) Front

func (l *List) Front() *Node

Front 方法返回链表的头节点。 The Front method returns the head node of the list.

func (*List) InsertAfter

func (l *List) InsertAfter(node, mark *Node)

InsertAfter 方法将节点 n 插入到节点 mark 的后面。 The InsertAfter method inserts node n after node mark.

func (*List) InsertBefore

func (l *List) InsertBefore(node, mark *Node)

InsertBefore 方法将节点 n 插入到节点 mark 的前面。 The InsertBefore method inserts node n before node mark.

func (*List) Len

func (l *List) Len() int64

Len 方法返回链表的长度。 The Len method returns the length of the list.

func (*List) MoveToBack

func (l *List) MoveToBack(node *Node)

MoveToBack 方法将节点 n 移动到链表的尾部。 The MoveToBack method moves node n to the end of the list.

func (*List) MoveToFront

func (l *List) MoveToFront(node *Node)

MoveToFront 方法将节点 n 移动到链表的头部。 The MoveToFront method moves node n to the front of the list.

func (*List) PopBack

func (l *List) PopBack() *Node

PopBack 方法从链表的尾部移除一个节点并返回它。 The PopBack method removes a node from the end of the list and returns it.

func (*List) PopFront

func (l *List) PopFront() *Node

PopFront 方法从链表的头部移除一个节点并返回它。 The PopFront method removes a node from the beginning of the list and returns it.

func (*List) PushBack

func (l *List) PushBack(node *Node)

PushBack 方法将一个节点添加到链表的尾部。 The PushBack method adds a node to the end of the list.

func (*List) PushFront

func (l *List) PushFront(node *Node)

PushFront 方法将一个节点添加到链表的头部。 The PushFront method adds a node to the beginning of the list.

func (*List) Range

func (l *List) Range(fn func(node *Node) bool)

Range 方法遍历链表,对每个节点执行 fn 函数,如果 fn 返回 false,就停止遍历。 The Range method traverses the list, performs the fn function on each node, and stops traversing if fn returns false.

func (*List) Remove

func (l *List) Remove(node *Node)

Remove 方法从链表中移除节点 n。 The Remove method removes node n from the list.

func (*List) Slice

func (l *List) Slice() []interface{}

Slice 方法将链表转换为一个切片,切片中的元素顺序和链表中的节点顺序一致。 The Slice method converts the list to a slice, the order of the elements in the slice is consistent with the order of the nodes in the list.

func (*List) Swap

func (l *List) Swap(node, mark *Node)

Swap 方法交换链表中的两个节点 n 和 mark 的位置。 The Swap method swaps the positions of two nodes n and mark in the list.

type Node

type Node struct {

	// Priority 是节点的优先级
	// Priority is the Priority of the node
	Priority int64

	// Color 是节点的颜色,可以是 RED 或 BLACK
	// Color is the Color of the node, can be RED or BLACK
	Color int64

	// Left 是节点的左子节点
	// Left is the Left child of the node
	Left *Node

	// Right 是节点的右子节点
	// Right is the Right child of the node
	Right *Node

	// Parent 是节点的父节点
	// Parent is the Parent of the node
	Parent *Node

	// Value 是节点存储的值
	// Value is the value stored in the node
	Value interface{}
	// contains filtered or unexported fields
}

Node 结构体代表一个节点,它可以存储任何类型的值,有一个优先级,以及指向前一个和后一个节点的指针。 Node struct represents a node that can hold a value of any type, has a priority, and pointers to the next and previous nodes.

func NewNode

func NewNode() *Node

NewNode 函数创建并返回一个新的 Node 实例。 The NewNode function creates and returns a new instance of Node.

func (*Node) Reset

func (n *Node) Reset()

Reset 方法重置节点的所有字段,将它们设置为零值。 The Reset method resets all the fields of the node, setting them to their zero values.

type NodePool

type NodePool struct {
	// contains filtered or unexported fields
}

NodePool 结构体是一个节点池,它使用 sync.Pool 来存储和复用 Node 实例。 The NodePool struct is a pool of nodes, it uses sync.Pool to store and reuse Node instances.

func NewNodePool

func NewNodePool() *NodePool

NewNodePool 函数创建并返回一个新的 NodePool 实例。 The NewNodePool function creates and returns a new instance of NodePool.

func (*NodePool) Get

func (p *NodePool) Get() *Node

Get 方法从 NodePool 中获取一个 Node 实例。 The Get method retrieves a Node instance from the NodePool.

func (*NodePool) Put

func (p *NodePool) Put(n *Node)

Put 方法将一个 Node 实例放回到 NodePool 中。 The Put method puts a Node instance back into the NodePool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL