Skip to content

Commit

Permalink
更新API文档
Browse files Browse the repository at this point in the history
  • Loading branch information
CC11001100 committed Nov 3, 2022
1 parent 0beb1a5 commit 3fdd723
Showing 1 changed file with 145 additions and 145 deletions.
290 changes: 145 additions & 145 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ Push(values ...T)
Example:

```go
stack := NewArrayStack[int]()
stack.Push(1)
stack := NewArrayStack[int]()
stack.Push(1)
```

## 出栈
Expand All @@ -66,36 +66,36 @@ Pop Example:

```go
type User struct {
}
stack := NewArrayStack[*User]()
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
// Output:
// <nil>
// &{}
}
stack := NewArrayStack[*User]()
fmt.Println(stack.Pop())
u := &User{}
stack.Push(u)
fmt.Println(stack.Pop())
// Output:
// <nil>
// &{}
```

PopE Example:

```go
stack := NewArrayStack[int]()
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PopE()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(element)
// Output:
// stack empty!
// 1
element, err := stack.PopE()
if errors.Is(err, ErrStackEmpty) {
fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PopE()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(element)
// Output:
// stack empty!
// 1
```

## 查看栈顶元素
Expand All @@ -108,37 +108,37 @@ PeekE() (T, error)
Peek Example:

```go
type User struct {
}
stack := NewArrayStack[*User]()
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
// Output:
// <nil>
// &{}
type User struct {
}
stack := NewArrayStack[*User]()
fmt.Println(stack.Peek())
u := &User{}
stack.Push(u)
fmt.Println(stack.Peek())
// Output:
// <nil>
// &{}
```

PeekE Example:

```go
stack := NewArrayStack[int]()
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PeekE()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(element)
// Output:
// stack empty!
// 1
element, err := stack.PeekE()
if errors.Is(err, ErrStackEmpty) {
fmt.Println("stack empty!")
}

stack.Push(1)
element, err = stack.PeekE()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(element)
// Output:
// stack empty!
// 1
```

## 判断栈是否空
Expand All @@ -151,25 +151,25 @@ IsNotEmpty() bool
IsEmpty Example:

```go
stack := NewArrayStack[int]()
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
// Output:
// true
// false
stack := NewArrayStack[int]()
fmt.Println(stack.IsEmpty())
stack.Push(1)
fmt.Println(stack.IsEmpty())
// Output:
// true
// false
```

IsNotEmpty Example:

```go
stack := NewArrayStack[int]()
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
// Output:
// false
// true
stack := NewArrayStack[int]()
fmt.Println(stack.IsNotEmpty())
stack.Push(1)
fmt.Println(stack.IsNotEmpty())
// Output:
// false
// true
```

## 栈中元素个数
Expand All @@ -181,11 +181,11 @@ Size() int
Example:

```go
stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
// Output:
// 1
stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
// Output:
// 1
```

## 清空栈
Expand All @@ -197,14 +197,14 @@ Clear()
Example:

```go
stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
// Output:
// 1
// 0
stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.Size())
stack.Clear()
fmt.Println(stack.Size())
// Output:
// 1
// 0
```

## String
Expand All @@ -214,11 +214,11 @@ Example:
Example:

```go
stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.String())
// Output:
// [1]
stack := NewArrayStack[int]()
stack.Push(1)
fmt.Println(stack.String())
// Output:
// [1]
```

# ArrayStack
Expand All @@ -228,10 +228,10 @@ Example:
Example:

```go
stack := NewArrayStack[int]()
fmt.Println(stack.String())
// Output:
// []
stack := NewArrayStack[int]()
fmt.Println(stack.String())
// Output:
// []
```

# LinkedStack
Expand All @@ -241,10 +241,10 @@ Example:
Example:

```go
stack := NewLinkedStack[int]()
fmt.Println(stack.String())
// Output:
// []
stack := NewLinkedStack[int]()
fmt.Println(stack.String())
// Output:
// []
```

# 五、最大栈 & 最小栈
Expand All @@ -261,41 +261,41 @@ GetMinE() (T, error)
GetMin Example:

```go
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMinE()
assert.ErrorIs(t, err, ErrStackEmpty)
_, err := stack.GetMinE()
assert.ErrorIs(t, err, ErrStackEmpty)

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMinE()
assert.Nil(t, err)
assert.Equal(t, 7, element)
stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMinE()
assert.Nil(t, err)
assert.Equal(t, 7, element)
```

GetMinE Example:

```go
stack := NewSyncMinStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMinE()
if errors.Is(err, ErrStackEmpty) {
fmt.Println("stack empty!")
}

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMinE()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(element)
// Output:
// stack empty!
// 7
_, err := stack.GetMinE()
if errors.Is(err, ErrStackEmpty) {
fmt.Println("stack empty!")
}

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMinE()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(element)
// Output:
// stack empty!
// 7
```

## MaxStack && SyncMaxStack
Expand All @@ -310,41 +310,41 @@ GetMaxE() (T, error)
GetMax Example:

```go
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMaxE()
assert.ErrorIs(t, err, ErrStackEmpty)
_, err := stack.GetMaxE()
assert.ErrorIs(t, err, ErrStackEmpty)

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMaxE()
assert.Nil(t, err)
assert.Equal(t, 10, element)
stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMaxE()
assert.Nil(t, err)
assert.Equal(t, 10, element)
```

GetMaxE Example:

```go
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMaxE()
if errors.Is(err, ErrStackEmpty) {
fmt.Println("stack empty!")
}

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMaxE()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(element)
// Output:
// stack empty!
// 10
stack := NewSyncMaxStack[int](func(a, b int) int { return a - b })

_, err := stack.GetMaxE()
if errors.Is(err, ErrStackEmpty) {
fmt.Println("stack empty!")
}

stack.Push(10)
stack.Push(7)
stack.Push(9)
element, err := stack.GetMaxE()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(element)
// Output:
// stack empty!
// 10
```

# TODO
Expand Down

0 comments on commit 3fdd723

Please sign in to comment.