From 386b82500e192dffe98a8673ca001d28180e4d65 Mon Sep 17 00:00:00 2001 From: Acid Date: Sun, 5 Apr 2026 01:46:53 -0400 Subject: [PATCH] mved stack into linear category --- linear/stack.go | 62 +++++++++++++++++++++++ linear/stack_test.go | 17 +++++++ main.go | 72 ++++++++++++++++++++++++++ stack.go | 117 ------------------------------------------- 4 files changed, 151 insertions(+), 117 deletions(-) create mode 100644 linear/stack.go create mode 100644 linear/stack_test.go create mode 100644 main.go delete mode 100644 stack.go diff --git a/linear/stack.go b/linear/stack.go new file mode 100644 index 0000000..ed3d7f9 --- /dev/null +++ b/linear/stack.go @@ -0,0 +1,62 @@ +package stack + +import ( + "errors" +) + +// Stack a Stack with no size +type Stack[T any] struct { + Capasity int + Container []T +} + +// StackFixed creates Stack with a fixed size +func StackFixed[T any](value int) *Stack[T] { + + return &Stack[T]{ + Capasity: value, + Container: make([]T, 0, value), + } + +} + +// Push Appends value to stack returns error +// If using StackFixed errors when over capasity +func (s *Stack[T]) Push(value T) error { + if s.Capasity > 0 && (len(s.Container) >= s.Capasity) { + return errors.New("Error max capasity exeded") + } + + s.Container = append(s.Container, value) + return nil +} + +// Pop returns value,error and removes last index +func (s *Stack[T]) Pop() (T, error) { + + var zero T + + if len(s.Container) <= 0 { + return zero, errors.New("Error Empty Stack") + } + + last := s.Container[len(s.Container)-1] + s.Container = s.Container[:len(s.Container)-1] + + return last, nil +} + +// Clear clears the stack +func (s *Stack[T]) Clear() { + s.Container = []T{} +} + +// Peek returns top item +func (s *Stack[T]) Peek() T { + return s.Container[len(s.Container)-1] +} + +// First returns bottom item +func (s *Stack[T]) First() T { + return s.Container[0] +} diff --git a/linear/stack_test.go b/linear/stack_test.go new file mode 100644 index 0000000..4b37b5b --- /dev/null +++ b/linear/stack_test.go @@ -0,0 +1,17 @@ +package stack + +import ( + "testing" +) + +func TestPush(t *testing.T) { + s := Stack[int]{} + s.Push(1) + s.Push(2) + s.Push(3) + + if len(s.Container) != 3 { + t.Errorf("expected 2 items, got %d", len(s.Container)) + } + +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..dee553c --- /dev/null +++ b/main.go @@ -0,0 +1,72 @@ +package main + +import ( + linear "datastructures/linear" + "fmt" +) + +func main() { + + testFixedStack() + +} + +func testFixedStack() { + + fixedStack := linear.StackFixed[int](3) + val, err := fixedStack.Pop() + if err != nil { + fmt.Println(err, val) + } + + fixedStack.Push(1) + fixedStack.Push(2) + fixedStack.Push(3) + err = fixedStack.Push(4) + + if err != nil { + fmt.Println(err) + } + + fixedStack.Push(6) + fixedStack.Push(5) + fixedStack.Push(7) + + fmt.Printf("fixedStack: %v\n", fixedStack.Container) +} + +func testStack() { + + que := linear.Stack[int]{} + + que.Container = append(que.Container, 1, 2, 4, 5) + + fmt.Printf("%v\n", que.Container) + que.Push(6) + + x, y := que.Pop() + fmt.Printf("%v %v\n", x, y) + + fmt.Printf("%v\n", que.Container) + fmt.Printf("after pop %v\n", que.Container) + + que.Clear() + fmt.Printf("ceared: %v\n", que.Container) + + que.Push(7) + que.Push(8) + que.Push(9) + que.Push(10) + + fmt.Printf("peek: %v\n", que.Peek()) + fmt.Printf("first: %v\n", que.First()) + + words := linear.Stack[string]{} + + words.Push("a") + words.Push("b") + words.Push("c") + words.Push("e") + + fmt.Printf("words: %v\n", words) +} diff --git a/stack.go b/stack.go deleted file mode 100644 index 9853431..0000000 --- a/stack.go +++ /dev/null @@ -1,117 +0,0 @@ -package main - -import ( - "errors" - "fmt" -) - -type Stack[T any] struct { - capasity int - container []T -} - -func (s *Stack[T]) push(value T) { - if s.capasity > 0 && (len(s.container) >= s.capasity) { - panic("Error max capasity exeded") - } - - s.container = append(s.container, value) -} - -func (s *Stack[T]) pop() (T, error) { - - var zero T - - if len(s.container) <= 0 { - return zero, errors.New("Empty Stack") - } - - last := s.container[len(s.container)-1] - s.container = s.container[:len(s.container)-1] - - return last, nil -} - -func (s *Stack[T]) clear() { - s.container = []T{} -} - -func (s *Stack[T]) peek() T { - - return s.container[len(s.container)-1] -} - -func (s *Stack[T]) first() T { - - return s.container[0] -} - -func StackFixed[T any](value int) *Stack[T] { - - return &Stack[T]{ - capasity: value, - container: make([]T, 0, value), - } - -} - -func (s *Stack[T]) setSize(value int) { - s.capasity = value -} - -func main() { - - testFixedStack() - -} - -func testFixedStack() { - - fixedStack := StackFixed[int](3) - - fixedStack.push(1) - fixedStack.push(2) - fixedStack.push(3) - fixedStack.pop() - fixedStack.push(4) - // fixedStack.push(5) - // fixedStack.push(6) - - fmt.Printf("fixedStack: %v\n", fixedStack.container) -} - -func testStack() { - - que := Stack[int]{} - - que.container = append(que.container, 1, 2, 4, 5) - - fmt.Printf("%v\n", que.container) - que.push(6) - - x, y := que.pop() - fmt.Printf("%v %v\n", x, y) - - fmt.Printf("%v\n", que.container) - fmt.Printf("after pop %v\n", que.container) - - que.clear() - fmt.Printf("ceared: %v\n", que.container) - - que.push(7) - que.push(8) - que.push(9) - que.push(10) - - fmt.Printf("peek: %v\n", que.peek()) - fmt.Printf("first: %v\n", que.first()) - - words := Stack[string]{} - - words.push("a") - words.push("b") - words.push("c") - words.push("e") - - fmt.Printf("words: %v\n", words) -}