mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:21:38 +08:00
fix: coding style and file format for chapter 10.
This commit is contained in:
@@ -1,59 +1,60 @@
|
||||
// stack_struct.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const LIMIT = 4
|
||||
type Stack struct {
|
||||
ix int // first free position, so data[ix] == 0
|
||||
data [LIMIT]int
|
||||
}
|
||||
|
||||
func main() {
|
||||
st1 := new(Stack)
|
||||
fmt.Printf("%v\n", st1)
|
||||
st1.Push(3)
|
||||
fmt.Printf("%v\n", st1)
|
||||
st1.Push(7)
|
||||
fmt.Printf("%v\n", st1)
|
||||
st1.Push(10)
|
||||
fmt.Printf("%v\n", st1)
|
||||
st1.Push(99)
|
||||
fmt.Printf("%v\n", st1)
|
||||
p := st1.Pop()
|
||||
fmt.Printf("Popped %d\n", p)
|
||||
fmt.Printf("%v\n", st1)
|
||||
p = st1.Pop()
|
||||
fmt.Printf("Popped %d\n", p)
|
||||
fmt.Printf("%v\n", st1)
|
||||
p = st1.Pop()
|
||||
fmt.Printf("Popped %d\n", p)
|
||||
fmt.Printf("%v\n", st1)
|
||||
p = st1.Pop()
|
||||
fmt.Printf("Popped %d\n", p)
|
||||
fmt.Printf("%v\n", st1)
|
||||
}
|
||||
|
||||
func (st *Stack) Push(n int) {
|
||||
if (st.ix + 1 > LIMIT) {
|
||||
return // stack is full!
|
||||
}
|
||||
st.data[st.ix] = n
|
||||
st.ix++
|
||||
}
|
||||
|
||||
func (st *Stack) Pop() int {
|
||||
st.ix--
|
||||
return st.data[st.ix]
|
||||
}
|
||||
|
||||
func (st Stack) String() string {
|
||||
str := ""
|
||||
for ix:=0; ix<st.ix; ix++ {
|
||||
str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(st.data[ix]) + "] "
|
||||
}
|
||||
return str
|
||||
}
|
||||
// stack_struct.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const LIMIT = 4
|
||||
|
||||
type Stack struct {
|
||||
ix int // first free position, so data[ix] == 0
|
||||
data [LIMIT]int
|
||||
}
|
||||
|
||||
func main() {
|
||||
st1 := new(Stack)
|
||||
fmt.Printf("%v\n", st1)
|
||||
st1.Push(3)
|
||||
fmt.Printf("%v\n", st1)
|
||||
st1.Push(7)
|
||||
fmt.Printf("%v\n", st1)
|
||||
st1.Push(10)
|
||||
fmt.Printf("%v\n", st1)
|
||||
st1.Push(99)
|
||||
fmt.Printf("%v\n", st1)
|
||||
p := st1.Pop()
|
||||
fmt.Printf("Popped %d\n", p)
|
||||
fmt.Printf("%v\n", st1)
|
||||
p = st1.Pop()
|
||||
fmt.Printf("Popped %d\n", p)
|
||||
fmt.Printf("%v\n", st1)
|
||||
p = st1.Pop()
|
||||
fmt.Printf("Popped %d\n", p)
|
||||
fmt.Printf("%v\n", st1)
|
||||
p = st1.Pop()
|
||||
fmt.Printf("Popped %d\n", p)
|
||||
fmt.Printf("%v\n", st1)
|
||||
}
|
||||
|
||||
func (st *Stack) Push(n int) {
|
||||
if st.ix+1 > LIMIT {
|
||||
return // stack is full!
|
||||
}
|
||||
st.data[st.ix] = n
|
||||
st.ix++
|
||||
}
|
||||
|
||||
func (st *Stack) Pop() int {
|
||||
st.ix--
|
||||
return st.data[st.ix]
|
||||
}
|
||||
|
||||
func (st Stack) String() string {
|
||||
str := ""
|
||||
for ix := 0; ix < st.ix; ix++ {
|
||||
str += "[" + strconv.Itoa(ix) + ":" + strconv.Itoa(st.data[ix]) + "] "
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
Reference in New Issue
Block a user