list

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

List implementations like linkedlist, arraylist, etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayList

type ArrayList[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

Package mypackage provides utilities for working with dynamic lists.

Use slices in Go when you need a dynamic, resizable array-like structure. Unlike arrays, slices can grow and shrink in size as needed.

Slices should be used when: - You need a flexible collection that can change in size. - You want efficient iteration and indexing. - You need to pass subsets of data without copying underlying elements.

Example:

numbers := []int{1, 2, 3}  // Create a slice
numbers = append(numbers, 4, 5)  // Dynamically grow the slice
fmt.Println(numbers)  // Output: [1 2 3 4 5]

func NewArrayList

func NewArrayList[T cmp.Ordered]() ArrayList[T]

Creating a new arraylist

func (*ArrayList[T]) Add

func (arr *ArrayList[T]) Add(value T)

Add a element to the ArrayList.

value - element to be appended to this list

func (*ArrayList[T]) AddAll

func (arr *ArrayList[T]) AddAll(values ...T) bool

Appends all of the elements in the specified array to the end of this ArrayList, in the order that they are returned by the specified array's Iterator. The behavior of this operation is undefined if the specified array is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified array is this ArrayList, and this ArrayList is nonempty.)

values - array containing elements to be added to this ArrayList.

func (*ArrayList[T]) AddAllAt

func (arr *ArrayList[T]) AddAllAt(index int, values ...T) bool

Inserts all of the elements in the specified array into this ArrayList, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the ArrayList in the order that they are returned by the specified array's iterator.

index - index at which to insert the first element from the specified array values - array containing elements to be added to this ArrayList

func (*ArrayList[T]) AddAt

func (arr *ArrayList[T]) AddAt(index int, value T)

Inserts the specified element at the specified position in this list.

index - index at which the specified element is to be inserted. value - element to be inserted.

func (*ArrayList[T]) Clear

func (arr *ArrayList[T]) Clear()

Removes all of the elements from this list. The list will be empty after this call returns.

func (*ArrayList[T]) Clone

func (arr *ArrayList[T]) Clone() ArrayList[T]

Returns a copy of this ArrayList instance.

func (*ArrayList[T]) Contains

func (arr *ArrayList[T]) Contains(value T) bool

Returns true if this ArrayList contains the specified element.

o - element whose presence in this ArrayList is to be tested.

func (*ArrayList[T]) Get

func (arr *ArrayList[T]) Get(index int) T

Return a element from the ArrayList

func (*ArrayList[T]) IndexOf

func (arr *ArrayList[T]) IndexOf(value T) int

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

value - element to search for

func (*ArrayList[T]) IsEmpty

func (arr *ArrayList[T]) IsEmpty() bool

Returns true if this list contains no elements.

func (*ArrayList[T]) LastIndexOf

func (arr *ArrayList[T]) LastIndexOf(value T) int

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

value - element to search for

func (*ArrayList[T]) Remove

func (arr *ArrayList[T]) Remove(value T) bool

Removes the first occurrence of the specified element from this list, if it is present.

func (*ArrayList[T]) RemoveAll

func (arr *ArrayList[T]) RemoveAll(values ...T) bool

Removes from this list all of its elements that are contained in the specified collection.

func (*ArrayList[T]) RemoveAt

func (arr *ArrayList[T]) RemoveAt(index int) T

Removes the element at the specified position in this list.

func (*ArrayList[T]) Set

func (arr *ArrayList[T]) Set(index int, value T) T

Replaces the element at the specified position in this list with the specified element.

func (ArrayList[T]) Size

func (arr ArrayList[T]) Size() int

Returns the number of elements in this list.

func (*ArrayList[T]) Sort

func (arr *ArrayList[T]) Sort()

This function sort the list

func (ArrayList[T]) String added in v0.0.2

func (arr ArrayList[T]) String() string

String representation for arraylist

func (ArrayList[T]) ToArray

func (arr ArrayList[T]) ToArray() []T

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

type LinkedList

type LinkedList[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

linkedlist implements a singly linked list in Go.

A linked list is a dynamic data structure consisting of nodes, where each node contains a value and a reference to the next node in the sequence. Unlike arrays, linked lists allow efficient insertions and deletions without needing to resize or shift elements.

When to Use: When you need dynamic memory allocation without a fixed size (unlike slices). When frequent insertions and deletions are required (e.g., queues, stacks). When memory fragmentation is a concern, as linked lists can allocate nodes anywhere in memory.

Limitations: Higher memory usage due to additional pointer storage. Slower random access (O(n) lookup time) compared to arrays (O(1) lookup).

func NewLinkedList

func NewLinkedList[T cmp.Ordered]() LinkedList[T]

Constructs an empty LinkedList.

func (*LinkedList[T]) Add

func (ll *LinkedList[T]) Add(element T)

func (*LinkedList[T]) AddAll

func (ll *LinkedList[T]) AddAll(elements ...T)

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.

func (*LinkedList[T]) Clear

func (ll *LinkedList[T]) Clear()

Removes all of the elements from this list.

func (*LinkedList[T]) Get

func (ll *LinkedList[T]) Get(index int) T

Returns the element at the specified position in this list.

func (*LinkedList[T]) Pop added in v0.0.2

func (ll *LinkedList[T]) Pop() T

Pops an element from the stack represented by this list.

func (*LinkedList[T]) Remove added in v0.0.2

func (ll *LinkedList[T]) Remove(index int) T

Removes the element at the specified position in this list.

func (LinkedList[T]) Size added in v0.0.2

func (ll LinkedList[T]) Size() int

Returns the number of elements in this list.

func (LinkedList[T]) String added in v0.0.2

func (ll LinkedList[T]) String() string

String representation for linkedlist

func (*LinkedList[T]) ToArray

func (ll *LinkedList[T]) ToArray() []T

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

Jump to

Keyboard shortcuts

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