glinklist

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

简介

glinklist 包提供了线程安全的双向链表实现。

功能特性

  • 双向链表:支持前后遍历
  • 线程安全:所有操作都是线程安全的
  • 灵活插入:支持头部、尾部、指定位置插入

安装

go get github.com/snail007/gmc/util/linklist

快速开始

package main

import (
    "fmt"
    "github.com/snail007/gmc/util/linklist"
)

func main() {
    list := glinklist.New()
    
    // 添加元素
    list.Append(1)
    list.Append(2)
    list.Append(3)
    
    // 遍历
    list.Range(func(v interface{}) bool {
        fmt.Println(v)
        return true
    })
    
    // 获取长度
    fmt.Println("Length:", list.Len())
}

相关链接

Documentation

Overview

Package glinklist implements a doubly linked list.

To iterate over a list (where l is a *LinkList):

for e := l.Front(); e != nil; e = e.Next() {
	// do something with e.Value
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element struct {

	// The value stored with this element.
	Value interface{}
	// contains filtered or unexported fields
}

Element is an element of a linked list.

func (*Element) Next

func (e *Element) Next() *Element

Next returns the next list element or nil.

func (*Element) Prev

func (e *Element) Prev() *Element

Prev returns the previous list element or nil.

type LinkList struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

LinkList represents a doubly linked list. The zero value for LinkList is an empty list ready to use.

func New

func New() *LinkList

New returns an initialized list.

func (*LinkList) Back

func (l *LinkList) Back() *Element

Back returns the last element of list l or nil if the list is empty.

func (*LinkList) Clone

func (l *LinkList) Clone() *LinkList

Clone creates a copy of current list.

func (*LinkList) Front

func (l *LinkList) Front() *Element

Front returns the first element of list l or nil if the list is empty.

func (*LinkList) IndexOf

func (l *LinkList) IndexOf(v interface{}) int

IndexOf returns the value first occurs index in the linklist, index starts with 0. if not found return -1.

func (*LinkList) Init

func (l *LinkList) Init() *LinkList

Init initializes or clears list l.

func (*LinkList) InsertAfter

func (l *LinkList) InsertAfter(v interface{}, mark *Element) *Element

InsertAfter inserts a new element e with value v immediately after mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.

func (*LinkList) InsertBefore

func (l *LinkList) InsertBefore(v interface{}, mark *Element) *Element

InsertBefore inserts a new element e with value v immediately before mark and returns e. If mark is not an element of l, the list is not modified. The mark must not be nil.

func (*LinkList) Len

func (l *LinkList) Len() int64

Len returns the number of elements of list l. The complexity is O(1).

func (*LinkList) MoveAfter

func (l *LinkList) MoveAfter(e, mark *Element)

MoveAfter moves element e to its new position after mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.

func (*LinkList) MoveBefore

func (l *LinkList) MoveBefore(e, mark *Element)

MoveBefore moves element e to its new position before mark. If e or mark is not an element of l, or e == mark, the list is not modified. The element and mark must not be nil.

func (*LinkList) MoveToBack

func (l *LinkList) MoveToBack(e *Element)

MoveToBack moves element e to the back of list l. If e is not an element of l, the list is not modified. The element must not be nil.

func (*LinkList) MoveToFront

func (l *LinkList) MoveToFront(e *Element)

MoveToFront moves element e to the front of list l. If e is not an element of l, the list is not modified. The element must not be nil.

func (*LinkList) PushBack

func (l *LinkList) PushBack(v interface{}) *Element

PushBack inserts a new element e with value v at the back of list l and returns e.

func (*LinkList) PushBackList

func (l *LinkList) PushBackList(other *LinkList)

PushBackList inserts a copy of an other list at the back of list l. The lists l and other may be the same. They must not be nil.

func (*LinkList) PushFront

func (l *LinkList) PushFront(v interface{}) *Element

PushFront inserts a new element e with value v at the front of list l and returns e.

func (*LinkList) PushFrontList

func (l *LinkList) PushFrontList(other *LinkList)

PushFrontList inserts a copy of an other list at the front of list l. The lists l and other may be the same. They must not be nil.

func (*LinkList) Range

func (l *LinkList) Range(f func(v interface{}) bool)

Range calls f sequentially for each key and value present in the linklist. If f returns false, range stops the iteration. Don't modify the map during the Range call, that will cause deadlock.

func (*LinkList) Remove

func (l *LinkList) Remove(e *Element) interface{}

Remove removes e from l if e is an element of list l. It returns the element value e.Value. The element must not be nil.

Jump to

Keyboard shortcuts

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