add zero value to queue so GC can reclaim pointers

This commit is contained in:
Acid
2026-06-20 23:21:58 -04:00
parent a17cb4fe83
commit a25babd3b1
3 changed files with 6 additions and 2 deletions
+1
View File
@@ -1,2 +1,3 @@
datastructures
main.go
scratch
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"datastructures/trees"
)
// BstTree() => Breadth first search implementation for Binary trees,
// BstTree() => Breadth first search implementation for Binary trees O(n) ,
// usage var mytree trees.BSTree[[int]] ,
// BstTree(mytree);
func BstTree[T trees.NumericTypes](node trees.BSTree[T]) {
+4 -1
View File
@@ -41,12 +41,15 @@ func (q *Queue[T]) Peek() (T, bool) {
// Pop() -> returns the upcoming item and removes it from Queue
func (q *Queue[T]) Pop() (T, error) {
var zero T
if len(q.container) <= 0 {
var zero T
return zero, errors.New("Queue is empty")
}
upcoming := q.container[0]
// if zero is a pointer the GC clears it
q.container[0] = zero
q.container = q.container[1:]
return upcoming, nil