diff --git a/phrasebook/ch04/generic_data_structure/main.go b/phrasebook/ch04/generic_data_structure/main.go new file mode 100644 index 0000000..0a38f18 --- /dev/null +++ b/phrasebook/ch04/generic_data_structure/main.go @@ -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) +}