mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 03:55:28 +08:00
fix: coding style and file format for chapter 11, 12, 13, 14 and 15.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
@@ -1,28 +1,28 @@
|
||||
// stack_general_v2.go
|
||||
// Package collection implements a generic stack.
|
||||
package collection
|
||||
|
||||
// The zero value for Stack is an empty stack ready to use.
|
||||
type Stack struct {
|
||||
data []interface{}
|
||||
}
|
||||
|
||||
// Push adds x to the top of the stack.
|
||||
func (s *Stack) Push(x interface{}) {
|
||||
s.data = append(s.data, x)
|
||||
}
|
||||
|
||||
// Pop removes and returns the top element of the stack.
|
||||
// It's a run-time error to call Pop on an empty stack.
|
||||
func (s *Stack) Pop() interface{} {
|
||||
i := len(s.data) - 1
|
||||
res := s.data[i]
|
||||
s.data[i] = nil // to avoid memory leak
|
||||
s.data = s.data[:i]
|
||||
return res
|
||||
}
|
||||
|
||||
// Size returns the number of elements in the stack.
|
||||
func (s *Stack) Size() int {
|
||||
return len(s.data)
|
||||
}
|
||||
// stack_general_v2.go
|
||||
// Package collection implements a generic stack.
|
||||
package collection
|
||||
|
||||
// The zero value for Stack is an empty stack ready to use.
|
||||
type Stack struct {
|
||||
data []interface{}
|
||||
}
|
||||
|
||||
// Push adds x to the top of the stack.
|
||||
func (s *Stack) Push(x interface{}) {
|
||||
s.data = append(s.data, x)
|
||||
}
|
||||
|
||||
// Pop removes and returns the top element of the stack.
|
||||
// It's a run-time error to call Pop on an empty stack.
|
||||
func (s *Stack) Pop() interface{} {
|
||||
i := len(s.data) - 1
|
||||
res := s.data[i]
|
||||
s.data[i] = nil // to avoid memory leak
|
||||
s.data = s.data[:i]
|
||||
return res
|
||||
}
|
||||
|
||||
// Size returns the number of elements in the stack.
|
||||
func (s *Stack) Size() int {
|
||||
return len(s.data)
|
||||
}
|
||||
|
Reference in New Issue
Block a user