Skip to content

Instantly share code, notes, and snippets.

@LucasMagnum
Created September 7, 2018 09:44
Show Gist options
  • Save LucasMagnum/ee95c7a7f220130845055a2c9fcb3c9c to your computer and use it in GitHub Desktop.
Save LucasMagnum/ee95c7a7f220130845055a2c9fcb3c9c to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Stack struct {
size int
position int
items []int
}
func newStack(size int) Stack {
return Stack{position: -1, items: make([]int, size)}
}
func (stack *Stack) push(value int) {
stack.position++
stack.items[stack.position] = value;
}
func (stack *Stack) pop() int {
value := stack.items[stack.position]
stack.position--
return value
}
func (stack Stack) isEmpty() bool {
if stack.position >= 0 {
return false
}
return true
}
func (stack Stack) top() int{
return stack.items[stack.position]
}
func main() {
stack := newStack(10)
stack.push(1)
stack.pop()
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
for !stack.isEmpty(){
value := stack.pop()
fmt.Println(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment