Documentation
¶
Overview ¶
List implementations like linkedlist, arraylist, etc.
Index ¶
- type ArrayList
- func (arr *ArrayList[T]) Add(value T)
- func (arr *ArrayList[T]) AddAll(values ...T) bool
- func (arr *ArrayList[T]) AddAllAt(index int, values ...T) bool
- func (arr *ArrayList[T]) AddAt(index int, value T)
- func (arr *ArrayList[T]) Clear()
- func (arr *ArrayList[T]) Clone() ArrayList[T]
- func (arr *ArrayList[T]) Contains(value T) bool
- func (arr *ArrayList[T]) Get(index int) T
- func (arr *ArrayList[T]) IndexOf(value T) int
- func (arr *ArrayList[T]) IsEmpty() bool
- func (arr *ArrayList[T]) LastIndexOf(value T) int
- func (arr *ArrayList[T]) Remove(value T) bool
- func (arr *ArrayList[T]) RemoveAll(values ...T) bool
- func (arr *ArrayList[T]) RemoveAt(index int) T
- func (arr *ArrayList[T]) Set(index int, value T) T
- func (arr ArrayList[T]) Size() int
- func (arr *ArrayList[T]) Sort()
- func (arr ArrayList[T]) String() string
- func (arr ArrayList[T]) ToArray() []T
- type LinkedList
- func (ll *LinkedList[T]) Add(element T)
- func (ll *LinkedList[T]) AddAll(elements ...T)
- func (ll *LinkedList[T]) Clear()
- func (ll *LinkedList[T]) Get(index int) T
- func (ll *LinkedList[T]) Pop() T
- func (ll *LinkedList[T]) Remove(index int) T
- func (ll LinkedList[T]) Size() int
- func (ll LinkedList[T]) String() string
- func (ll *LinkedList[T]) ToArray() []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArrayList ¶
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 (*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 ¶
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 ¶
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 ¶
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]) Contains ¶
Returns true if this ArrayList contains the specified element.
o - element whose presence in this ArrayList is to be tested.
func (*ArrayList[T]) IndexOf ¶
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]) LastIndexOf ¶
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 ¶
Removes the first occurrence of the specified element from this list, if it is present.
func (*ArrayList[T]) RemoveAll ¶
Removes from this list all of its elements that are contained in the specified collection.
func (*ArrayList[T]) Set ¶
Replaces the element at the specified position in this list with the specified element.
type LinkedList ¶
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).