-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package kgo | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
) | ||
|
||
type Stack[T any] struct { | ||
elements []any | ||
} | ||
|
||
// NewStack 创建一个新的栈 | ||
func NewStack[T any]() *Stack[T] { | ||
return &Stack[T]{} | ||
} | ||
|
||
// Push 向栈中添加元素 | ||
func (s *Stack[T]) Push(element T) { | ||
s.elements = append(s.elements, element) | ||
} | ||
|
||
// Pop 从栈中移除并返回栈顶元素 | ||
func (s *Stack[T]) Pop() (T, error) { | ||
if len(s.elements) == 0 { | ||
return reflect.Zero(reflect.TypeOf(new(T))).Interface().(T), errors.New("stack is empty") | ||
} | ||
element := s.elements[len(s.elements)-1] | ||
s.elements = s.elements[:len(s.elements)-1] | ||
return element.(T), nil | ||
} | ||
|
||
// Peek 返回栈顶元素但不移除它 | ||
func (s *Stack[T]) Peek() (T, error) { | ||
if len(s.elements) == 0 { | ||
return reflect.Zero(reflect.TypeOf(new(T))).Interface().(T), errors.New("stack is empty") | ||
} | ||
return s.elements[len(s.elements)-1].(T), nil | ||
} | ||
|
||
// Size 返回栈中元素的数量 | ||
func (s *Stack[T]) Size() int { | ||
return len(s.elements) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package kgo | ||
|
||
import "testing" | ||
|
||
func TestNewStack(t *testing.T) { | ||
stack := NewStack[int]() | ||
if stack != nil { | ||
t.Log("stack is not nil") | ||
} else { | ||
t.Errorf("stack is nil") | ||
} | ||
} | ||
|
||
func TestStack_Push(t *testing.T) { | ||
stack := NewStack[int]() | ||
stack.Push(1) | ||
stack.Push(2) | ||
stack.Push(3) | ||
if stack.Size() != 3 { | ||
t.Errorf("stack size is not 3") | ||
} else { | ||
t.Log("stack size is 3") | ||
} | ||
} | ||
|
||
func TestStack_Pop(t *testing.T) { | ||
stack := NewStack[int]() | ||
stack.Push(1) | ||
stack.Push(2) | ||
stack.Push(3) | ||
element, err := stack.Pop() | ||
if err != nil { | ||
t.Errorf("pop error: %s", err) | ||
} else if element != 3 { | ||
t.Errorf("pop element is not 3") | ||
} | ||
} | ||
|
||
func TestStack_Peek(t *testing.T) { | ||
stack := NewStack[int]() | ||
stack.Push(1) | ||
stack.Push(2) | ||
stack.Push(3) | ||
element, err := stack.Peek() | ||
if err != nil { | ||
t.Errorf("peek error: %s", err) | ||
} else if element != 3 { | ||
t.Errorf("peek element is not 3") | ||
} | ||
} | ||
|
||
func TestStack_Size(t *testing.T) { | ||
stack := NewStack[int]() | ||
stack.Push(1) | ||
stack.Push(2) | ||
stack.Push(3) | ||
if stack.Size() != 3 { | ||
t.Errorf("stack size is not 3") | ||
} else { | ||
t.Log("stack size is 3") | ||
} | ||
} |