mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 02:02:27 +08:00
feat: add Contain function for LinkedQueue
This commit is contained in:
@@ -3,6 +3,7 @@ package datastructure
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/datastructure"
|
"github.com/duke-git/lancet/v2/datastructure"
|
||||||
)
|
)
|
||||||
@@ -41,6 +42,7 @@ func (q *LinkedQueue[T]) IsEmpty() bool {
|
|||||||
return q.length == 0
|
return q.length == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Enqueue put element into queue
|
// Enqueue put element into queue
|
||||||
func (q *LinkedQueue[T]) Enqueue(value T) {
|
func (q *LinkedQueue[T]) Enqueue(value T) {
|
||||||
newNode := datastructure.NewQueueNode(value)
|
newNode := datastructure.NewQueueNode(value)
|
||||||
@@ -102,3 +104,15 @@ func (q *LinkedQueue[T]) Print() {
|
|||||||
info += " ]"
|
info += " ]"
|
||||||
fmt.Println(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
|
||||||
|
}
|
||||||
|
|||||||
@@ -82,3 +82,16 @@ func TestLinkedQueue_Clear(t *testing.T) {
|
|||||||
queue.Clear()
|
queue.Clear()
|
||||||
assert.Equal(true, queue.IsEmpty())
|
assert.Equal(true, queue.IsEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLinkedQueue_Contain(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestLinkedQueue_Contain")
|
||||||
|
|
||||||
|
queue := NewLinkedQueue[int]()
|
||||||
|
|
||||||
|
queue.Enqueue(1)
|
||||||
|
queue.Enqueue(2)
|
||||||
|
queue.Enqueue(3)
|
||||||
|
|
||||||
|
assert.Equal(true, queue.Contain(1))
|
||||||
|
assert.Equal(false, queue.Contain(4))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user