stack_struct.go 508 B

1234567891011121314151617181920212223242526272829303132
  1. // stack_struct.go
  2. package stack
  3. import "strconv"
  4. const LIMIT = 10
  5. type Stack struct {
  6. ix int // first free position, so data[ix] == 0
  7. data [LIMIT]int
  8. }
  9. func (st *Stack) Push(n int) {
  10. if st.ix+1 > LIMIT {
  11. return // stack is full!
  12. }
  13. st.data[st.ix] = n
  14. st.ix++
  15. }
  16. func (st *Stack) Pop() int {
  17. st.ix--
  18. return st.data[st.ix]
  19. }
  20. func (st Stack) String() string {
  21. str := ""
  22. for ix := 0; ix < st.ix; ix++ {
  23. str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(st.data[ix]) + "] "
  24. }
  25. return str
  26. }