go-collections
is a Go library that provides implementations of common data structures including a double-ended queue (Deque), a linked list, a queue, and a stack. This package offers a simple and efficient way to use these structures in Go, with support for generic types.
You can install the package using the Go module system:
go get github.com/idsulik/go-collections
Here is a brief example of how to use the Deque
:
package main
import (
"fmt"
"github.com/idsulik/go-collections/deque"
)
func main() {
d := deque.New
d.PushBack(1)
d.PushFront(2)
front, _ := d.PopFront()
back, _ := d.PopBack()
fmt.Println(front) // Output: 2
fmt.Println(back) // Output: 1
}
Certainly! Below is a README for the Set
struct, following the format you provided:
A set represents a collection of unique items.
type Set[T comparable]
func New[T comparable]() *Set[T]
Add(item T)
: Adds an item to the set.Remove(item T)
: Removes an item from the set.Has(item T) bool
: Returnstrue
if the set contains the specified item.Clear()
: Removes all items from the set.Len() int
: Returns the number of items in the set.IsEmpty() bool
: Returnstrue
if the set is empty.Elements() []T
: Returns a slice containing all items in the set.AddAll(items ...T)
: Adds multiple items to the set.RemoveAll(items ...T)
: Removes multiple items from the set.Diff(other *Set[T]) *Set[T]
: Returns a new set containing items that are in the receiver set but not in the other set.Intersect(other *Set[T]) *Set[T]
: Returns a new set containing items that are in both the receiver set and the other set.Union(other *Set[T]) *Set[T]
: Returns a new set containing items that are in either the receiver set or the other set.IsSubset(other *Set[T]) bool
: Returnstrue
if the receiver set is a subset of the other set.IsSuperset(other *Set[T]) bool
: Returnstrue
if the receiver set is a superset of the other set.Equal(other *Set[T]) bool
: Returnstrue
if the receiver set is equal to the other set.
A double-ended queue (Deque) allows adding and removing elements from both the front and the back.
-
Constructor:
func New[T any](initialCapacity int) *Deque[T]
-
Methods:
PushFront(item T)
: Adds an item to the front of the deque.PushBack(item T)
: Adds an item to the back of the deque.PopFront() (T, bool)
: Removes and returns the item at the front of the deque.PopBack() (T, bool)
: Removes and returns the item at the back of the deque.PeekFront() (T, bool)
: Returns the item at the front of the deque without removing it.PeekBack() (T, bool)
: Returns the item at the back of the deque without removing it.Len() int
: Returns the number of items in the deque.IsEmpty() bool
: Checks if the deque is empty.Clear()
: Removes all items from the deque.
A singly linked list where elements can be added or removed from both the front and the end.
-
Constructor:
func New[T any]() *LinkedList[T]
-
Methods:
AddFront(value T)
: Adds a new node with the given value to the front of the list.AddBack(value T)
: Adds a new node with the given value to the end of the list.RemoveFront() (T, bool)
: Removes the node from the front of the list and returns its value.RemoveBack() (T, bool)
: Removes the node from the end of the list and returns its value.Iterate(fn func(T) bool)
: Iterates over the linked list and applies a function to each node's value.Size() int
: Returns the number of elements in the list.
A FIFO (first-in, first-out) queue that supports basic queue operations.
-
Constructor:
func New[T any](initialCapacity int) *Queue[T]
-
Methods:
Enqueue(item T)
: Adds an item to the end of the queue.Dequeue() (T, bool)
: Removes and returns the item at the front of the queue.Peek() (T, bool)
: Returns the item at the front of the queue without removing it.Len() int
: Returns the number of items currently in the queue.IsEmpty() bool
: Checks if the queue is empty.Clear()
: Removes all items from the queue.
A LIFO (last-in, first-out) stack that supports standard stack operations.
-
Constructor:
func New[T any](initialCapacity int) *Stack[T]
-
Methods:
Push(item T)
: Adds an item to the top of the stack.Pop() (T, bool)
: Removes and returns the item from the top of the stack.Peek() (T, bool)
: Returns the item at the top of the stack without removing it.Len() int
: Returns the number of items currently in the stack.IsEmpty() bool
: Checks if the stack is empty.Clear()
: Removes all items from the stack.
-
Here's the README following the provided example format strictly:
A Trie (prefix tree) data structure that supports insertion and search operations for words and prefixes.
-
Constructor:
func New() *Trie
-
Methods:
Insert(word string)
: Adds a word to the Trie.Search(word string) bool
: Checks if the word exists in the Trie.StartsWith(prefix string) bool
: Checks if there is any word in the Trie that starts with the given prefix.
This project is licensed under the MIT License - see the LICENSE file for details.