This package provides two different queue implementations in Go: Array-based and LinkedList-based. All implementations are designed to be thread-safe and support generic types.
- Array Queue (Dynamic array-based implementation with generic type support)
- LinkedList Queue (Linked list-based implementation with generic type support)
- Enqueue: Add element to the queue
- Dequeue: Remove element from the queue
- List: Get all elements
- Print: Display elements
- Safe read/write operations with RWMutex
- Concurrent access support for all structures
- Deadlock prevention mechanisms
- Support for any comparable type
- Type-safe operations
- Custom type support with proper comparison functions
// Create a new integer queue
intQueue := NewArrayQueue[int]()
// Add elements
intQueue.Enqueue(1)
intQueue.Enqueue(2)
intQueue.Enqueue(3)
// Remove element from front
intQueue.Dequeue()
// List elements
elements := intQueue.List()
intQueue.Print()
// Create a new string queue
strQueue := NewArrayQueue[string]()
// Add elements
strQueue.Enqueue("a")
strQueue.Enqueue("b")
strQueue.Enqueue("c")
// Remove element from front
strQueue.Dequeue()
// List elements
elements = strQueue.List()
strQueue.Print()
// Create a queue with custom type
type Person struct {
Name string
Age int
}
personQueue := NewArrayQueue[Person]()
personQueue.Enqueue(Person{Name: "John", Age: 30})
personQueue.Enqueue(Person{Name: "Jane", Age: 25})
// Create a new integer queue
intQueue := NewLinkedListQueue[int](0)
// Add elements
intQueue.Enqueue(1)
intQueue.Enqueue(2)
intQueue.Enqueue(3)
// Remove element from front
intQueue.Dequeue()
// List elements
elements := intQueue.List()
intQueue.Print()
// Create a new string queue
strQueue := NewLinkedListQueue[string]("")
// Add elements
strQueue.Enqueue("a")
strQueue.Enqueue("b")
strQueue.Enqueue("c")
// Remove element from front
strQueue.Dequeue()
// List elements
elements = strQueue.List()
strQueue.Print()
// Create a queue with custom type
type Person struct {
Name string
Age int
}
personQueue := NewLinkedListQueue[Person](Person{Name: "", Age: 0})
personQueue.Enqueue(Person{Name: "John", Age: 30})
personQueue.Enqueue(Person{Name: "Jane", Age: 25})
- Generic type support with comparable constraint
- Dynamic array-based implementation
- Auto-resizing capability (grows and shrinks)
- Efficient memory management with reordering
- First and last index tracking
- Generic type support with comparable constraint
- Node-based implementation
- Dynamic memory allocation
- Single direction linking
- No size limitations
- Enqueue: O(1) amortized, O(n) worst case when resizing
- Dequeue: O(1) amortized, O(n) worst case when reordering
- List: O(n)
- Space: O(n)
- Enqueue: O(n) - needs to traverse to end
- Dequeue: O(1)
- List: O(n)
- Space: O(n)
- Dynamic array resizing (doubles when full)
- Array shrinking (halves when 1/4 full)
- Automatic reordering to optimize space
- Efficient memory utilization
- Dynamic node allocation
- No pre-allocated memory
- Memory freed on dequeue
- No explicit size limitations
- RLock for read operations (List, Print)
- Lock for write operations (Enqueue, Dequeue)
- Automatic unlock with defer
- Safe design for concurrent access
- Types must satisfy the
comparable
interface - Support for built-in types (int, string, etc.)
- Support for custom types that implement
comparable
- Type safety at compile time
The package comes with comprehensive test coverage for various types. To run tests:
go test ./...
Contributions are welcome! Please ensure that any new features or modifications come with:
- Proper documentation
- Thread safety considerations
- Comprehensive test cases
- Example usage
- Performance analysis
- Generic type support considerations
This package is distributed under the MIT license. See the LICENSE file for more details.