list

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2025 License: BSD-3-Clause Imports: 2 Imported by: 2

README

List Package

English | 中文

A generic doubly-linked list implementation in Go, providing a flexible and efficient data structure for managing ordered collections.

Features

  • Generic type support
  • Doubly-linked list implementation
  • Thread-safe operations
  • Rich set of operations:
    • PushFront/PushBack
    • InsertBefore/InsertAfter
    • MoveToFront/MoveToBack
    • MoveBefore/MoveAfter
    • Remove
    • Iteration support

Installation

go get github.com/danielhookx/xcontainer/list

Usage

package main

import "github.com/danielhookx/xcontainer/list"

func main() {
    // Create a new list
    l := list.New[int]()
    
    // Add elements
    l.PushBack(1)
    l.PushBack(2)
    l.PushBack(3)
    
    // Iterate over elements
    for e := l.Front(); e != nil; e = e.Next() {
        fmt.Println(e.Value)
    }
    
    // Use generic iteration
    for item := range l.Iter() {
        fmt.Println(item)
    }
}

API Reference

NewT *List[T]

Creates a new empty list.

List Methods
  • PushFront(v T) *Element[T]
  • PushBack(v T) *Element[T]
  • InsertBefore(v T, mark *Element[T]) *Element[T]
  • InsertAfter(v T, mark *Element[T]) *Element[T]
  • MoveToFront(e *Element[T])
  • MoveToBack(e *Element[T])
  • MoveBefore(e, mark *Element[T])
  • MoveAfter(e, mark *Element[T])
  • Remove(e *Element[T]) T
  • Len() int
  • Front() *Element[T]
  • Back() *Element[T]
  • Iter() <-chan T
Element Methods
  • Next() *Element[T]
  • Prev() *Element[T]
  • Value T

License

This project is licensed under the BSD License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element[T any] struct {
	Value T
	// contains filtered or unexported fields
}

func (*Element[T]) Next

func (e *Element[T]) Next() *Element[T]

Next returns the next list element or nil.

func (*Element[T]) Prev

func (e *Element[T]) Prev() *Element[T]

Prev returns the previous list element or nil.

type List

type List[T any] struct {
	// contains filtered or unexported fields
}

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

func New

func New[T any]() *List[T]

New returns an initialized list.

func (*List[T]) Back

func (l *List[T]) Back() *Element[T]

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

func (*List[T]) Front

func (l *List[T]) Front() *Element[T]

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

func (*List[T]) Init

func (l *List[T]) Init() *List[T]

Init initializes or clears list l.

func (*List[T]) InsertAfter

func (l *List[T]) InsertAfter(v T, mark *Element[T]) *Element[T]

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 (*List[T]) InsertBefore

func (l *List[T]) InsertBefore(v T, mark *Element[T]) *Element[T]

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 (*List[T]) Iter

func (l *List[T]) Iter() iter.Seq[T]

Iter returns an iterator over the elements of the list.

func (*List[T]) Len

func (l *List[T]) Len() int

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

func (*List[T]) MoveAfter

func (l *List[T]) MoveAfter(e, mark *Element[T])

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 (*List[T]) MoveBefore

func (l *List[T]) MoveBefore(e, mark *Element[T])

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 (*List[T]) MoveToBack

func (l *List[T]) MoveToBack(e *Element[T])

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 (*List[T]) MoveToFront

func (l *List[T]) MoveToFront(e *Element[T])

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 (*List[T]) PushBack

func (l *List[T]) PushBack(v T) *Element[T]

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

func (*List[T]) PushBackList

func (l *List[T]) PushBackList(other *List[T])

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

func (*List[T]) PushFront

func (l *List[T]) PushFront(v T) *Element[T]

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

func (*List[T]) PushFrontList

func (l *List[T]) PushFrontList(other *List[T])

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

func (*List[T]) Remove

func (l *List[T]) Remove(e *Element[T]) T

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.

type SetList

type SetList[T comparable] struct {
	// contains filtered or unexported fields
}

SetList 节点值不重复的单链表封装

func NewSetList

func NewSetList[T comparable]() *SetList[T]

func (*SetList[T]) Add

func (sl *SetList[T]) Add(val T)

func (*SetList[T]) Iter

func (sl *SetList[T]) Iter() iter.Seq[T]

func (*SetList[T]) Len

func (sl *SetList[T]) Len() int

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

Jump to

Keyboard shortcuts

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