mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2025-04-09 03:52:03 +00:00
add example for stack entry
This commit is contained in:
parent
57942eb8a0
commit
39f2be3c13
1 changed files with 36 additions and 0 deletions
36
phrasebook/ch04/generic_data_structure/main.go
Normal file
36
phrasebook/ch04/generic_data_structure/main.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type stackEntry struct {
|
||||
next *stackEntry
|
||||
value interface{}
|
||||
}
|
||||
|
||||
type stack struct {
|
||||
top *stackEntry
|
||||
}
|
||||
|
||||
func (s *stack) Push(v interface{}) {
|
||||
var e stackEntry
|
||||
e.value = v
|
||||
e.next = s.top
|
||||
s.top = &e
|
||||
}
|
||||
|
||||
func (s *stack) Pop() interface{} {
|
||||
if s.top == nil {
|
||||
return nil
|
||||
}
|
||||
v := s.top.value
|
||||
s.top = s.top.next
|
||||
return v
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := &stack{}
|
||||
s.Push("one")
|
||||
s.Push("two")
|
||||
s.Push("three")
|
||||
fmt.Printf("%#v", s)
|
||||
}
|
Loading…
Add table
Reference in a new issue