From 85e1f711f5277a2302e526936c326a198b2a7b89 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Thu, 10 Feb 2022 17:41:51 +0800 Subject: [PATCH] feat: and Clear and IsEmpty func --- datastructure/arraystack.go | 4 ++-- datastructure/arraystack_test.go | 6 +++--- datastructure/doublylink.go | 11 +++++++++++ datastructure/doublylink_test.go | 17 +++++++++++++++++ datastructure/linkedstack.go | 4 ++-- datastructure/linkedstack_test.go | 2 +- datastructure/singlylink.go | 11 +++++++++++ datastructure/singlylink_test.go | 17 +++++++++++++++++ 8 files changed, 64 insertions(+), 8 deletions(-) diff --git a/datastructure/arraystack.go b/datastructure/arraystack.go index e96e569..8dd682b 100644 --- a/datastructure/arraystack.go +++ b/datastructure/arraystack.go @@ -55,8 +55,8 @@ func (s *ArrayStack[T]) Peak() (*T, error) { return &s.data[0], nil } -// EmptyStack clear the stack data -func (s *ArrayStack[T]) EmptyStack() { +// Clear the stack data +func (s *ArrayStack[T]) Clear() { s.data = []T{} s.length = 0 } diff --git a/datastructure/arraystack_test.go b/datastructure/arraystack_test.go index 1e55ceb..99a77c9 100644 --- a/datastructure/arraystack_test.go +++ b/datastructure/arraystack_test.go @@ -60,8 +60,8 @@ func TestArrayStack_Peak(t *testing.T) { assert.Equal(expected, stack.Data()) } -func TestArrayStack_Empty(t *testing.T) { - assert := internal.NewAssert(t, "TestArrayStack_Empty") +func TestArrayStack_Clear(t *testing.T) { + assert := internal.NewAssert(t, "TestArrayStack_Clear") stack := NewArrayStack[int]() assert.Equal(true, stack.IsEmpty()) @@ -71,7 +71,7 @@ func TestArrayStack_Empty(t *testing.T) { assert.Equal(false, stack.IsEmpty()) assert.Equal(1, stack.Size()) - stack.EmptyStack() + stack.Clear() assert.Equal(true, stack.IsEmpty()) assert.Equal(0, stack.Size()) } diff --git a/datastructure/doublylink.go b/datastructure/doublylink.go index 144ea55..5b46ae4 100644 --- a/datastructure/doublylink.go +++ b/datastructure/doublylink.go @@ -226,3 +226,14 @@ func (link *DoublyLink[T]) Print() { 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 +} diff --git a/datastructure/doublylink_test.go b/datastructure/doublylink_test.go index 7518966..4e1d19c 100644 --- a/datastructure/doublylink_test.go +++ b/datastructure/doublylink_test.go @@ -163,3 +163,20 @@ func TestDoublyLink_GetMiddleNode(t *testing.T) { middle2 := link.GetMiddleNode() 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()) +} diff --git a/datastructure/linkedstack.go b/datastructure/linkedstack.go index e575f20..966b8e7 100644 --- a/datastructure/linkedstack.go +++ b/datastructure/linkedstack.go @@ -73,8 +73,8 @@ func (s *LinkedStack[T]) Peak() (*T, error) { return &s.top.Value, nil } -// EmptyStack clear the stack data -func (s *LinkedStack[T]) EmptyStack() { +// Clear clear the stack data +func (s *LinkedStack[T]) Clear() { s.top = nil s.length = 0 } diff --git a/datastructure/linkedstack_test.go b/datastructure/linkedstack_test.go index c41f7fa..d6e88e4 100644 --- a/datastructure/linkedstack_test.go +++ b/datastructure/linkedstack_test.go @@ -74,7 +74,7 @@ func TestLinkedStack_Empty(t *testing.T) { assert.Equal(false, stack.IsEmpty()) assert.Equal(1, stack.Length()) - stack.EmptyStack() + stack.Clear() assert.Equal(true, stack.IsEmpty()) assert.Equal(0, stack.Length()) } diff --git a/datastructure/singlylink.go b/datastructure/singlylink.go index a948206..ffb7718 100644 --- a/datastructure/singlylink.go +++ b/datastructure/singlylink.go @@ -198,6 +198,17 @@ func (link *SinglyLink[T]) Values() []T { 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 func (link *SinglyLink[T]) Print() { current := link.Head diff --git a/datastructure/singlylink_test.go b/datastructure/singlylink_test.go index 5c813fe..d2b9e5e 100644 --- a/datastructure/singlylink_test.go +++ b/datastructure/singlylink_test.go @@ -163,3 +163,20 @@ func TestSinglyLink_GetMiddleNode(t *testing.T) { middle2 := link.GetMiddleNode() 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()) +}