fix: coding style and file format for chapter 11, 12, 13, 14 and 15.

This commit is contained in:
Bo-Yi Wu
2017-02-11 12:32:16 +08:00
parent c5413075c1
commit 4abbfabb52
63 changed files with 2662 additions and 2622 deletions

View File

@@ -1,39 +1,39 @@
// stack.go
package stack
import "errors"
type Stack []interface{}
func (stack Stack) Len() int {
return len(stack)
}
func (stack Stack) Cap() int {
return cap(stack)
}
func (stack Stack) IsEmpty() bool {
return len(stack) == 0
}
func (stack *Stack) Push(e interface{}) {
*stack = append(*stack, e)
}
func (stack Stack) Top() (interface{}, error) {
if len(stack) == 0 {
return nil, errors.New("stack is empty")
}
return stack[len(stack)-1], nil
}
func (stack *Stack) Pop() (interface{}, error) {
stk := *stack // dereference to a local variable stk
if len(stk) == 0 {
return nil, errors.New("stack is empty")
}
top := stk[len(stk)-1]
*stack = stk[:len(stk)-1] // shrink the stack
return top, nil
}
// stack.go
package stack
import "errors"
type Stack []interface{}
func (stack Stack) Len() int {
return len(stack)
}
func (stack Stack) Cap() int {
return cap(stack)
}
func (stack Stack) IsEmpty() bool {
return len(stack) == 0
}
func (stack *Stack) Push(e interface{}) {
*stack = append(*stack, e)
}
func (stack Stack) Top() (interface{}, error) {
if len(stack) == 0 {
return nil, errors.New("stack is empty")
}
return stack[len(stack)-1], nil
}
func (stack *Stack) Pop() (interface{}, error) {
stk := *stack // dereference to a local variable stk
if len(stk) == 0 {
return nil, errors.New("stack is empty")
}
top := stk[len(stk)-1]
*stack = stk[:len(stk)-1] // shrink the stack
return top, nil
}