feat: add Contain function for LinkedQueue

This commit is contained in:
dudaodong
2022-06-05 16:41:16 +08:00
parent 52ecde7e24
commit 2ef9b56d22
2 changed files with 27 additions and 0 deletions

View File

@@ -3,6 +3,7 @@ package datastructure
import (
"errors"
"fmt"
"reflect"
"github.com/duke-git/lancet/v2/datastructure"
)
@@ -41,6 +42,7 @@ func (q *LinkedQueue[T]) IsEmpty() bool {
return q.length == 0
}
// Enqueue put element into queue
func (q *LinkedQueue[T]) Enqueue(value T) {
newNode := datastructure.NewQueueNode(value)
@@ -102,3 +104,15 @@ func (q *LinkedQueue[T]) Print() {
info += " ]"
fmt.Println(info)
}
// Contain checks if the value is in queue or not
func (q *LinkedQueue[T]) Contain(value T) bool {
current := q.head
for current != nil {
if reflect.DeepEqual(current.Value, value) {
return true
}
current = current.Next
}
return false
}