-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.go
More file actions
36 lines (30 loc) · 692 Bytes
/
stack.go
File metadata and controls
36 lines (30 loc) · 692 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
//stack.go taken from gist.github.com/bemasher/
package main
import ()
type Stack struct {
top *Element
size int
}
type Element struct {
value interface{} // All types satisfy the empty interface, so we can store anything here.
next *Element
}
// Return the stack's length
func (s *Stack) Len() int {
return s.size
}
// Push a new element onto the stack
func (s *Stack) Push(value interface{}) {
s.top = &Element{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 *Stack) Pop() (value interface{}) {
if s.size > 0 {
value, s.top = s.top.value, s.top.next
s.size--
return
}
return nil
}