From 0f67559f3c005d1793fb51b3cc30654b15d0f908 Mon Sep 17 00:00:00 2001 From: Austin M Crane Date: Wed, 3 Oct 2018 06:56:14 -0500 Subject: [PATCH] Implemented a simple stack algorithm in golang Created a simple program the uses a integer stack with the following methods: Push - adds an item to the stack Pop - removes top item off the stack Size - gets the size of the stack --- data_structures/stacks/stack.go | 68 +++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 data_structures/stacks/stack.go diff --git a/data_structures/stacks/stack.go b/data_structures/stacks/stack.go new file mode 100644 index 0000000..19171c5 --- /dev/null +++ b/data_structures/stacks/stack.go @@ -0,0 +1,68 @@ +// Author: Austin Crane +package main + +import ( + "errors" + "fmt" +) + +// Stack is a simple int stack this gives a simple +// implementation of how stacks are implemented +type Stack struct { + items []int +} + +// Push moves a item onto the top of the stack +func (s *Stack) Push(n int) { + s.items = append(s.items, n) +} + +// Pop gets the top item out of the stack and updates +// the stack to exclude that "popped" item out of the +// stack +func (s *Stack) Pop() (int, error) { + if s.Size() == 0 { + return 0, errors.New("end of stack reached") + } + + var x int + x, s.items = s.items[0], s.items[1:] + return x, nil +} + +// Size returns count of items in the stack +func (s *Stack) Size() int { + return len(s.items) +} + +// simple helper method to print the popped item +func popAndPrint(stack *Stack) { + n, err := stack.Pop() + // there is an error returned if end of stack + // is reached + if err != nil { + fmt.Println(err.Error()) + return + } + + fmt.Printf("Popped: %d\n", n) +} + +func main() { + // create an empty stack + s := Stack{} + + // add some items to the top of the stack + s.Push(1) + s.Push(2) + + // print stack size: 2 + fmt.Printf("Stack size: %d\n", s.Size()) + + // pop off 2 + popAndPrint(&s) + // pop off 3 + popAndPrint(&s) + // end of stack is returned + popAndPrint(&s) +}