Created
September 7, 2018 09:44
-
-
Save LucasMagnum/ee95c7a7f220130845055a2c9fcb3c9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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