awesome-patterns/phrasebook/ch04/generic_data_structure/main.go

41 lines
526 B
Go
Raw Normal View History

2018-04-14 15:05:00 +03:00
package main
import "fmt"
type stackEntry struct {
2018-04-14 15:28:24 +03:00
Next *stackEntry
Value interface{}
2018-04-14 15:05:00 +03:00
}
type stack struct {
top *stackEntry
}
func (s *stack) Push(v interface{}) {
var e stackEntry
2018-04-14 15:28:24 +03:00
e.Value = v
e.Next = s.top
2018-04-14 15:05:00 +03:00
s.top = &e
}
func (s *stack) Pop() interface{} {
if s.top == nil {
return nil
}
2018-04-14 15:28:24 +03:00
v := s.top.Value
s.top = s.top.Next
2018-04-14 15:05:00 +03:00
return v
}
2018-04-14 15:28:24 +03:00
func (s *stack) GetTop() *stackEntry {
return s.top
}
2018-04-14 15:05:00 +03:00
func main() {
s := &stack{}
s.Push("one")
s.Push("two")
s.Push("three")
2018-04-14 15:28:24 +03:00
fmt.Printf("%#v", s.GetTop().Next)
2018-04-14 15:05:00 +03:00
}