1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-03-01 00:35:28 +08:00

feat: and Clear and IsEmpty func

This commit is contained in:
dudaodong
2022-02-10 17:41:51 +08:00
parent ade567a620
commit 85e1f711f5
8 changed files with 64 additions and 8 deletions

View File

@@ -55,8 +55,8 @@ func (s *ArrayStack[T]) Peak() (*T, error) {
return &s.data[0], nil return &s.data[0], nil
} }
// EmptyStack clear the stack data // Clear the stack data
func (s *ArrayStack[T]) EmptyStack() { func (s *ArrayStack[T]) Clear() {
s.data = []T{} s.data = []T{}
s.length = 0 s.length = 0
} }

View File

@@ -60,8 +60,8 @@ func TestArrayStack_Peak(t *testing.T) {
assert.Equal(expected, stack.Data()) assert.Equal(expected, stack.Data())
} }
func TestArrayStack_Empty(t *testing.T) { func TestArrayStack_Clear(t *testing.T) {
assert := internal.NewAssert(t, "TestArrayStack_Empty") assert := internal.NewAssert(t, "TestArrayStack_Clear")
stack := NewArrayStack[int]() stack := NewArrayStack[int]()
assert.Equal(true, stack.IsEmpty()) assert.Equal(true, stack.IsEmpty())
@@ -71,7 +71,7 @@ func TestArrayStack_Empty(t *testing.T) {
assert.Equal(false, stack.IsEmpty()) assert.Equal(false, stack.IsEmpty())
assert.Equal(1, stack.Size()) assert.Equal(1, stack.Size())
stack.EmptyStack() stack.Clear()
assert.Equal(true, stack.IsEmpty()) assert.Equal(true, stack.IsEmpty())
assert.Equal(0, stack.Size()) assert.Equal(0, stack.Size())
} }

View File

@@ -226,3 +226,14 @@ func (link *DoublyLink[T]) Print() {
info += " ]" info += " ]"
fmt.Println(info) fmt.Println(info)
} }
// IsEmpty checks if link is empty or not
func (link *DoublyLink[T]) IsEmpty() bool {
return link.length == 0
}
// IsEmpty checks if link is empty or not
func (link *DoublyLink[T]) Clear() {
link.Head = nil
link.length = 0
}

View File

@@ -163,3 +163,20 @@ func TestDoublyLink_GetMiddleNode(t *testing.T) {
middle2 := link.GetMiddleNode() middle2 := link.GetMiddleNode()
assert.Equal(4, middle2.Value) assert.Equal(4, middle2.Value)
} }
func TestDoublyLink_Clear(t *testing.T) {
assert := internal.NewAssert(t, "TestDoublyLink_Clear")
link := NewDoublyLink[int]()
assert.Equal(true, link.IsEmpty())
assert.Equal(0, link.Size())
link.InsertAtTail(1)
assert.Equal(false, link.IsEmpty())
assert.Equal(1, link.Size())
link.Clear()
assert.Equal(true, link.IsEmpty())
assert.Equal(0, link.Size())
}

View File

@@ -73,8 +73,8 @@ func (s *LinkedStack[T]) Peak() (*T, error) {
return &s.top.Value, nil return &s.top.Value, nil
} }
// EmptyStack clear the stack data // Clear clear the stack data
func (s *LinkedStack[T]) EmptyStack() { func (s *LinkedStack[T]) Clear() {
s.top = nil s.top = nil
s.length = 0 s.length = 0
} }

View File

@@ -74,7 +74,7 @@ func TestLinkedStack_Empty(t *testing.T) {
assert.Equal(false, stack.IsEmpty()) assert.Equal(false, stack.IsEmpty())
assert.Equal(1, stack.Length()) assert.Equal(1, stack.Length())
stack.EmptyStack() stack.Clear()
assert.Equal(true, stack.IsEmpty()) assert.Equal(true, stack.IsEmpty())
assert.Equal(0, stack.Length()) assert.Equal(0, stack.Length())
} }

View File

@@ -198,6 +198,17 @@ func (link *SinglyLink[T]) Values() []T {
return res return res
} }
// IsEmpty checks if link is empty or not
func (link *SinglyLink[T]) IsEmpty() bool {
return link.length == 0
}
// IsEmpty checks if link is empty or not
func (link *SinglyLink[T]) Clear() {
link.Head = nil
link.length = 0
}
// Print all nodes info of a linked list // Print all nodes info of a linked list
func (link *SinglyLink[T]) Print() { func (link *SinglyLink[T]) Print() {
current := link.Head current := link.Head

View File

@@ -163,3 +163,20 @@ func TestSinglyLink_GetMiddleNode(t *testing.T) {
middle2 := link.GetMiddleNode() middle2 := link.GetMiddleNode()
assert.Equal(4, middle2.Value) assert.Equal(4, middle2.Value)
} }
func TestSinglyLink_Clear(t *testing.T) {
assert := internal.NewAssert(t, "TestSinglyLink_Clear")
link := NewSinglyLink[int]()
assert.Equal(true, link.IsEmpty())
assert.Equal(0, link.Size())
link.InsertAtTail(1)
assert.Equal(false, link.IsEmpty())
assert.Equal(1, link.Size())
link.Clear()
assert.Equal(true, link.IsEmpty())
assert.Equal(0, link.Size())
}