A package with generic data structures for Go applications.
(Queues, linked lists, binary trees and more)
Collections is a Go package that gives you useful data structures. These are ready-to-use building blocks for organizing and storing data in your programs.
- Queue - First in, first out (like a line at a store)
- Linked List - Items connected in a chain (can be circular too)
- Binary Tree - Items organized in a tree shape
go get github.com/carlosgrillet/collectionsA queue works like a line at a store - the first person in line gets served first.
package main
import "github.com/carlosgrillet/collections"
func main() {
// Create a new queue
q := collections.NewQueue[int]()
// Add items to the queue
q.Enqueue(1)
q.Enqueue(2)
q.Enqueue(3)
// Remove and get the first item
first, ok := q.Next() // Returns 1
// Look at the next item without removing it
next, ok := q.Peek() // Returns 2
// Get the queue size
size := q.Len() // Returns 2
}Queue features:
Enqueue(item)- Add an item to the backNext()- Remove and return the first itemPeek()- Look at the first item without removing itPeekLast()- Look at the last itemClear()- Remove all itemsLen()- Get the number of itemsIsEmpty()- Check if the queue is emptyIsFull()- Check if a bounded queue is fullContains(item)- Check if an item existsToSlice()- Convert to a sliceEnqueueAll(items)- Add multiple items at onceDequeueN(n)- Remove and return n itemsClone()- Make a copy of the queueForEach(fn)- Run a function on each itemFilter(fn)- Create a new queue with only matching items
Bounded Queue:
You can create a queue with a size limit:
q := collections.NewBoundedQueue[int](5) // Max 5 items
q.Enqueue(1) // Returns true
q.Enqueue(2) // Returns true
// ... add 3 more items
q.Enqueue(6) // Returns false (queue is full)A linked list is like a chain where each item points to the next one.
package main
import "github.com/carlosgrillet/collections"
func main() {
// Create a new linked list
list := collections.NewLinkedList[string]()
// Add items
list.Append("first") // Add to the end
list.Append("second")
list.Prepend("zero") // Add to the beginning
// Get items
first, ok := list.GetFirst() // Returns "zero"
last, ok := list.GetLast() // Returns "second"
item, ok := list.Get(1) // Returns "first"
// Remove items
removed, ok := list.RemoveFirst() // Removes "zero"
list.Remove("second") // Removes "second"
// Check what's in the list
exists := list.Contains("first") // Returns true
index := list.IndexOf("first") // Returns 0
}Linked List features:
Append(item)- Add to the endPrepend(item)- Add to the beginningInsertAt(index, item)- Add at a specific positionGet(index)- Get item at positionGetFirst()- Get the first itemGetLast()- Get the last itemRemove(item)- Remove first occurrence of itemRemoveFirst()- Remove the first itemRemoveLast()- Remove the last itemRemoveAt(index)- Remove item at positionContains(item)- Check if item existsIndexOf(item)- Find the position of an itemFind(item)- Find the node containing the itemLen()- Get the number of itemsIsEmpty()- Check if the list is emptyClear()- Remove all itemsReverse()- Flip the order of all itemsToSlice()- Convert to a sliceForEach(fn)- Run a function on each item
Circular Linked List:
A circular list is like a regular list, but the last item points back to the first one (like a circle).
// Create a circular list
list := collections.NewCircularLinkedList[int]()
list.Append(1)
list.Append(2)
list.Append(3)
// Convert a regular list to circular
regularList := collections.NewLinkedList[int]()
regularList.Append(1)
regularList.MakeCircular()
// Convert back to a regular list
regularList.BreakCircle()
// Check if a list is circular
isCircular := regularList.IsCircular() // Returns falseA tree is like an upside-down family tree where each item can have a left and right child.
package main
import "github.com/carlosgrillet/collections"
func main() {
// Create a new tree
tree := collections.NewTree[int]()
// Add items (adds level by level, left to right)
tree.Insert(1)
tree.Insert(2)
tree.Insert(3)
tree.Insert(4)
// Tree looks like:
// 1
// / \
// 2 3
// /
// 4
// Get information about the tree
depth := tree.MaxDepth() // Returns 3
minDepth := tree.MinDepth() // Returns 2
size := tree.Size() // Returns 4
leaves := tree.CountLeaves() // Returns 2 (nodes 3 and 4)
// Search for items
exists := tree.Contains(3) // Returns true
node, found := tree.Search(2) // Returns the node with value 2
// Get items in different orders
inOrder := tree.InOrder() // [4, 2, 1, 3]
preOrder := tree.PreOrder() // [1, 2, 4, 3]
postOrder := tree.PostOrder() // [4, 2, 3, 1]
levelOrder := tree.LevelOrder() // [1, 2, 3, 4]
}Binary Tree features:
Insert(item)- Add an item to the treeContains(item)- Check if an item existsSearch(item)- Find the node with the itemMaxDepth()- Get the maximum depth (height)MinDepth()- Get the minimum depth to a leafSize()- Get the number of itemsCountLeaves()- Count leaf nodes (nodes with no children)IsEmpty()- Check if the tree is emptyClear()- Remove all itemsInOrder()- Get items in order: Left, Root, RightPreOrder()- Get items in order: Root, Left, RightPostOrder()- Get items in order: Left, Right, RootLevelOrder()- Get items level by level (breadth-first)String()- Get a text view of the tree
All data structures work with any type you want:
// With numbers
queueInt := collections.NewQueue[int]()
// With text
queueString := collections.NewQueue[string]()
// With your own types
type Person struct {
Name string
Age int
}
listPeople := collections.NewLinkedList[Person]()
listPeople.Append(Person{Name: "Alice", Age: 30})go testFor more details:
go test -vMIT License - see LICENSE file for details.