add zero value to queue so GC can reclaim pointers
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
datastructures
|
||||
main.go
|
||||
scratch
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user