container package provides implementations of container data structures. It includes three subpackages for different container types.
Subpackages
container/heap
Provides heap operations for any type that implementsheap.Interface.
Interface:
container/list
Implements a doubly linked list. Type:container/ring
Implements operations on circular lists. Type:Key Methods
heap Functions
Init(h Interface)- Establish heap invariantsPush(h Interface, x interface{})- Push element onto heapPop(h Interface)- Remove and return minimum elementRemove(h Interface, i int)- Remove element at index iFix(h Interface, i int)- Re-establish heap after element at i changed
list Methods
PushFront(v interface{})- Insert at frontPushBack(v interface{})- Insert at backInsertBefore(v interface{}, mark *Element)- Insert before markInsertAfter(v interface{}, mark *Element)- Insert after markRemove(e *Element)- Remove elementMoveToFront(e *Element)- Move element to frontMoveToBack(e *Element)- Move element to backFront()- Return first elementBack()- Return last element
ring Methods
New(n int)- Create ring of n elementsNext()- Return next ring elementPrev()- Return previous ring elementMove(n int)- Move n elements forward (negative n moves backward)Link(s *Ring)- Connect r and sUnlink(n int)- Remove n elements starting from r.Next()Do(f func(interface{}))- Call function for each element
Performance Characteristics
Heap
- Push: O(log n)
- Pop: O(log n)
- Peek (access min): O(1)
- Init: O(n)
List
- PushFront/PushBack: O(1)
- Remove: O(1) with element reference
- Access by index: O(n)
- Search: O(n)
Ring
- Access: O(1)
- Insert/Remove: O(1)
- Traverse: O(n)
Common Use Cases
Heap
- Priority queues
- Task scheduling
- Finding k smallest/largest elements
- Dijkstra’s algorithm
List
- LRU caches
- Browser history (back/forward)
- Undo/redo functionality
- Music playlists
Ring
- Round-robin scheduling
- Circular buffers
- Load balancing
- Token rotation