listx

package
v2.14.7 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/jxskiss/gopkg/v2/collection/listx"
)

func main() {
	// Create a new list and put some numbers in it.
	l := listx.NewList[int]()
	e4 := l.PushBack(4)
	e1 := l.PushFront(1)
	l.InsertBefore(3, e4)
	l.InsertAfter(2, e1)

	// Iterate through list and print its contents.
	for e := l.Front(); e != nil; e = e.Next() {
		fmt.Println(e.Value)
	}

}
Output:

1
2
3
4

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element = list.Element

Element is an element of a linked list.

type List

type List[T any] list.List

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

func NewList

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

NewList returns an initialized list.

func (*List[T]) Back

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

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

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

func (*List[T]) InsertAfter

func (l *List[T]) InsertAfter(v T, 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 (*List[T]) InsertBefore

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

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)

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)

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)

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

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

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

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 Queue

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

Queue is a First In First Out data structure implementation. The zero value for Queue is an empty queue ready to use. A Queue is not safe for concurrent operations.

func NewQueue

func NewQueue[T any]() *Queue[T]

NewQueue creates a new Queue instance.

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() (item T, ok bool)

Dequeue removes and returns the Queue's front item in *O(1)* time complexity.

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(item T)

Enqueue adds an item at the back of the Queue in *O(1)* time complexity.

func (*Queue[T]) Len

func (q *Queue[T]) Len() int

Len returns the size of the Queue.

func (*Queue[T]) Peek

func (q *Queue[T]) Peek() (item T, ok bool)

Peek returns the Queue's front item in *O(1)* time complexity, it does not remove the item from the Queue.

type Stack

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

Stack implements a Last In First Out data structure built upon a doubly-linked list. The zero value for Stack is an empty stack ready to use. A Stack is not safe for concurrent operations.

In most cases, a simple slice based implementation is a better choice.

func NewStack

func NewStack[T any]() *Stack[T]

NewStack creates a new Stack instance.

func (*Stack[T]) Len

func (s *Stack[T]) Len() int

Len returns the size of the Stack.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (item T, ok bool)

Peek returns the item on the top of the Stack in *O(1)* time complexity, it does not remove the item from the Stack.

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (item T, ok bool)

Pop removes and returns the item on the top of the Stack in *O(1)* time complexity.

func (*Stack[T]) Push

func (s *Stack[T]) Push(item T)

Push adds on an item on the top of the Stack in *O(1)* time complexity.

Jump to

Keyboard shortcuts

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