[DEV] Completed example

This commit is contained in:
Jian Han 2018-04-14 22:28:24 +10:00
parent 39f2be3c13
commit 5fe2f0aa7f

View File

@ -3,8 +3,8 @@ package main
import "fmt" import "fmt"
type stackEntry struct { type stackEntry struct {
next *stackEntry Next *stackEntry
value interface{} Value interface{}
} }
type stack struct { type stack struct {
@ -13,8 +13,8 @@ type stack struct {
func (s *stack) Push(v interface{}) { func (s *stack) Push(v interface{}) {
var e stackEntry var e stackEntry
e.value = v e.Value = v
e.next = s.top e.Next = s.top
s.top = &e s.top = &e
} }
@ -22,15 +22,19 @@ func (s *stack) Pop() interface{} {
if s.top == nil { if s.top == nil {
return nil return nil
} }
v := s.top.value v := s.top.Value
s.top = s.top.next s.top = s.top.Next
return v return v
} }
func (s *stack) GetTop() *stackEntry {
return s.top
}
func main() { func main() {
s := &stack{} s := &stack{}
s.Push("one") s.Push("one")
s.Push("two") s.Push("two")
s.Push("three") s.Push("three")
fmt.Printf("%#v", s) fmt.Printf("%#v", s.GetTop().Next)
} }