1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52: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
}
// 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
}

View File

@@ -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())
}

View File

@@ -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
}

View File

@@ -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())
}

View File

@@ -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
}

View File

@@ -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())
}

View File

@@ -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

View File

@@ -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())
}