mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add stackarray.go implements stack with array
This commit is contained in:
@@ -1,13 +1,24 @@
|
||||
package datastructure
|
||||
|
||||
// LinkNode is a linkedlist node, which have a value and Pre points to previous node, Next points to a next node of the link.
|
||||
// LinkNode is a linkedlist node, which have a Value and Pre points to previous node, Next points to a next node of the link.
|
||||
type LinkNode[T any] struct {
|
||||
Value T
|
||||
Pre *LinkNode[T]
|
||||
Next *LinkNode[T]
|
||||
}
|
||||
|
||||
// NewLinkNode return a LinkNode pointer
|
||||
func NewLinkNode[T any](value T) *LinkNode[T] {
|
||||
return &LinkNode[T]{value, nil, nil}
|
||||
}
|
||||
|
||||
// StackNode is a node in stack, which have a Value and Pre points to previous node in the stack.
|
||||
type StackNode[T any] struct {
|
||||
Value T
|
||||
Pre *StackNode[T]
|
||||
}
|
||||
|
||||
// NewStackNode return a StackNode pointer
|
||||
func NewStackNode[T any](value T) *StackNode[T] {
|
||||
return &StackNode[T]{value, nil}
|
||||
}
|
||||
|
||||
62
datastructure/stackarray.go
Normal file
62
datastructure/stackarray.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package datastructure
|
||||
|
||||
import "errors"
|
||||
|
||||
// StackArray is a linear table, implemented with slice
|
||||
type StackArray[T any] struct {
|
||||
data []T
|
||||
length int
|
||||
}
|
||||
|
||||
// NewStackArray return a empty StackArray pointer
|
||||
func NewStackArray[T any]() *StackArray[T] {
|
||||
return &StackArray[T]{data: []T{}, length: 0}
|
||||
}
|
||||
|
||||
// Data return stack data
|
||||
func (s *StackArray[T]) Data() []T {
|
||||
return s.data
|
||||
}
|
||||
|
||||
// Length return length of stack data
|
||||
func (s *StackArray[T]) Length() int {
|
||||
return s.length
|
||||
}
|
||||
|
||||
// IsEmpty checks if stack is empty or not
|
||||
func (s *StackArray[T]) IsEmpty() bool {
|
||||
return s.length == 0
|
||||
}
|
||||
|
||||
// Push element into stack
|
||||
func (s *StackArray[T]) Push(value T) {
|
||||
s.data = append([]T{value}, s.data...)
|
||||
s.length++
|
||||
}
|
||||
|
||||
// Pop delete the top element of stack then return it, if stack is empty, return nil and error
|
||||
func (s *StackArray[T]) Pop() (*T, error) {
|
||||
if s.IsEmpty() {
|
||||
return nil, errors.New("stack is empty")
|
||||
}
|
||||
|
||||
topItem := s.data[0]
|
||||
s.data = s.data[1:]
|
||||
s.length--
|
||||
|
||||
return &topItem, nil
|
||||
}
|
||||
|
||||
// Peak return the top element of stack then return it
|
||||
func (s *StackArray[T]) Peak() (*T, error) {
|
||||
if s.IsEmpty() {
|
||||
return nil, errors.New("stack is empty")
|
||||
}
|
||||
return &s.data[0], nil
|
||||
}
|
||||
|
||||
// EmptyStack clear the stack data
|
||||
func (s *StackArray[T]) EmptyStack() {
|
||||
s.data = []T{}
|
||||
s.length = 0
|
||||
}
|
||||
77
datastructure/stackarray_test.go
Normal file
77
datastructure/stackarray_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package datastructure
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/duke-git/lancet/internal"
|
||||
)
|
||||
|
||||
func TestStackArray_Push(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestStackArray_Push")
|
||||
|
||||
stack := NewStackArray[int]()
|
||||
stack.Push(1)
|
||||
stack.Push(2)
|
||||
stack.Push(3)
|
||||
|
||||
expected := []int{3, 2, 1}
|
||||
values := stack.Data()
|
||||
length := stack.Length()
|
||||
|
||||
assert.Equal(expected, values)
|
||||
assert.Equal(3, length)
|
||||
}
|
||||
|
||||
func TestStackArray_Pop(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestStackArray_Pop")
|
||||
|
||||
stack := NewStackArray[int]()
|
||||
topItem, err := stack.Pop()
|
||||
assert.IsNotNil(err)
|
||||
|
||||
stack.Push(1)
|
||||
stack.Push(2)
|
||||
stack.Push(3)
|
||||
|
||||
topItem, err = stack.Pop()
|
||||
assert.IsNil(err)
|
||||
assert.Equal(3, *topItem)
|
||||
|
||||
expected := []int{2, 1}
|
||||
assert.Equal(expected, stack.Data())
|
||||
}
|
||||
|
||||
func TestStackArray_Peak(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestStackArray_Peak")
|
||||
|
||||
stack := NewStackArray[int]()
|
||||
topItem, err := stack.Peak()
|
||||
assert.IsNotNil(err)
|
||||
|
||||
stack.Push(1)
|
||||
stack.Push(2)
|
||||
stack.Push(3)
|
||||
|
||||
topItem, err = stack.Peak()
|
||||
assert.IsNil(err)
|
||||
assert.Equal(3, *topItem)
|
||||
|
||||
expected := []int{3, 2, 1}
|
||||
assert.Equal(expected, stack.Data())
|
||||
}
|
||||
|
||||
func TestStackArray_Empty(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestStackArray_Empty")
|
||||
|
||||
stack := NewStackArray[int]()
|
||||
assert.Equal(true, stack.IsEmpty())
|
||||
assert.Equal(0, stack.Length())
|
||||
|
||||
stack.Push(1)
|
||||
assert.Equal(false, stack.IsEmpty())
|
||||
assert.Equal(1, stack.Length())
|
||||
|
||||
stack.EmptyStack()
|
||||
assert.Equal(true, stack.IsEmpty())
|
||||
assert.Equal(0, stack.Length())
|
||||
}
|
||||
Reference in New Issue
Block a user