-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommonB2GrowableStack.go
More file actions
42 lines (35 loc) · 857 Bytes
/
CommonB2GrowableStack.go
File metadata and controls
42 lines (35 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package box2d
// Adapted from https://gist.github.com/bemasher/1777766
type B2GrowableStack struct {
top *StackElement
size int
}
func NewB2GrowableStack() *B2GrowableStack {
return &B2GrowableStack{
top: nil,
size: 0,
}
}
type StackElement struct {
value interface{} // All types satisfy the empty interface, so we can store anything here.
next *StackElement
}
// Return the stack's length
func (s B2GrowableStack) GetCount() int {
return s.size
}
// Push a new element onto the stack
func (s *B2GrowableStack) Push(value interface{}) {
s.top = &StackElement{value, s.top}
s.size++
}
// Remove the top element from the stack and return it's value
// If the stack is empty, return nil
func (s *B2GrowableStack) Pop() (value interface{}) {
if s.size > 0 {
value, s.top = s.top.value, s.top.next
s.size--
return
}
return nil
}