diff --git a/codes/go/chapter_stack_and_queue/deque.go b/codes/go/chapter_stack_and_queue/deque.go index fb419a6..3ac070e 100644 --- a/codes/go/chapter_stack_and_queue/deque.go +++ b/codes/go/chapter_stack_and_queue/deque.go @@ -6,22 +6,21 @@ package chapter_stack_and_queue type Deque interface { // OfferFirst 元素入队 - OfferFirst(num int) + OfferFirst(num any) // OfferLast 元素入队 - OfferLast(num int) - + OfferLast(num any) // PeekFirst 访问首元素 - PeekFirst() int + PeekFirst() any // PeekLast 访问尾元素 - PeekLast() int - + PeekLast() any // PollFirst 元素出队 - PollFirst() int + PollFirst() any // PollLast 元素出队 - PollLast() int - + PollLast() any // Size 获取队列长度 Size() int // IsEmpty 队列是否为空 IsEmpty() bool + // toString 队列输出为字符串 + toString() string } diff --git a/codes/go/chapter_stack_and_queue/linkedlist_queue.go b/codes/go/chapter_stack_and_queue/linkedlist_queue.go index 6c71f6f..5b62ac3 100644 --- a/codes/go/chapter_stack_and_queue/linkedlist_queue.go +++ b/codes/go/chapter_stack_and_queue/linkedlist_queue.go @@ -10,7 +10,7 @@ import ( "strings" ) -// LinkedListQueue 基于链表实现的栈, 使用内置包 list 来实现栈 +// LinkedListQueue 基于链表实现的队列, 使用内置包 list 来实现栈 type LinkedListQueue struct { list *list.List } @@ -60,11 +60,10 @@ func (s *LinkedListQueue) Print() { fmt.Println(s.toString()) } -func (s *LinkedListQueue) toString() any { +func (s *LinkedListQueue) toString() string { var builder strings.Builder if s.IsEmpty() { fmt.Println("empty stack") - return nil } e := s.list.Front() // 强转为 string, 会影响效率 diff --git a/codes/go/chapter_stack_and_queue/linkedlist_stack.go b/codes/go/chapter_stack_and_queue/linkedlist_stack.go index 88fe4dc..24f46ac 100644 --- a/codes/go/chapter_stack_and_queue/linkedlist_stack.go +++ b/codes/go/chapter_stack_and_queue/linkedlist_stack.go @@ -23,7 +23,7 @@ func NewLinkedListStack() *LinkedListStack { } // Push 入栈 -func (s *LinkedListStack) Push(value any) { +func (s *LinkedListStack) Push(value int) { s.list.PushBack(value) } @@ -60,11 +60,10 @@ func (s *LinkedListStack) Print() { fmt.Println(s.toString()) } -func (s *LinkedListStack) toString() any { +func (s *LinkedListStack) toString() string { var builder strings.Builder if s.IsEmpty() { fmt.Println("empty stack") - return nil } e := s.list.Back() // 强转为 string, 会影响效率 diff --git a/codes/go/chapter_stack_and_queue/queue.go b/codes/go/chapter_stack_and_queue/queue.go index c8f16c8..03b97e5 100644 --- a/codes/go/chapter_stack_and_queue/queue.go +++ b/codes/go/chapter_stack_and_queue/queue.go @@ -6,13 +6,15 @@ package chapter_stack_and_queue type Queue interface { // Offer 元素入队 - Offer(num int) + Offer(num any) // Peek 访问首元素 - Peek() int + Peek() any // Poll 元素出队 - Poll() int + Poll() any // Size 获取队列长度 Size() int // IsEmpty 队列是否为空 IsEmpty() bool + // toString 队列输出为字符串 + toString() string } diff --git a/codes/go/chapter_stack_and_queue/queue_test.go b/codes/go/chapter_stack_and_queue/queue_test.go index f4734a5..996b0bc 100644 --- a/codes/go/chapter_stack_and_queue/queue_test.go +++ b/codes/go/chapter_stack_and_queue/queue_test.go @@ -7,9 +7,11 @@ package chapter_stack_and_queue import "testing" func TestArrayQueue(t *testing.T) { - // 初始化队 + + // 初始化队列,使用队列的通用接口 + var queue Queue capacity := 10 - queue := NewArrayQueue(capacity) + queue = NewArrayQueue(capacity) // 元素入队 queue.Offer(1) @@ -38,7 +40,8 @@ func TestArrayQueue(t *testing.T) { func TestLinkedListQueue(t *testing.T) { // 初始化队 - queue := NewLinkedListQueue() + var queue Queue + queue = NewLinkedListQueue() // 元素入队 queue.Offer(1) diff --git a/codes/go/chapter_stack_and_queue/stack.go b/codes/go/chapter_stack_and_queue/stack.go index 7215224..7795387 100644 --- a/codes/go/chapter_stack_and_queue/stack.go +++ b/codes/go/chapter_stack_and_queue/stack.go @@ -8,11 +8,13 @@ type Stack interface { // Push 元素入栈 Push(num int) // Peek 访问栈顶元素 - Peek() int + Peek() any // Pop 元素出栈 - Pop() int + Pop() any // Size 栈的长度 Size() int // IsEmpty 栈是否为空 IsEmpty() bool + // toString 栈输出为字符串 + toString() string } diff --git a/codes/go/chapter_stack_and_queue/stack_test.go b/codes/go/chapter_stack_and_queue/stack_test.go index b546d55..95c6d24 100644 --- a/codes/go/chapter_stack_and_queue/stack_test.go +++ b/codes/go/chapter_stack_and_queue/stack_test.go @@ -7,8 +7,9 @@ package chapter_stack_and_queue import "testing" func TestArrayStack(t *testing.T) { - // 初始化栈 - stack := NewArrayStack() + // 初始化栈, 使用接口承接 + var stack Stack + stack = NewArrayStack() // 元素入栈 stack.Push(1) @@ -37,8 +38,8 @@ func TestArrayStack(t *testing.T) { func TestLinkedListStack(t *testing.T) { // 初始化栈 - stack := NewLinkedListStack() - + var stack Stack + stack = NewLinkedListStack() // 元素入栈 stack.Push(1) stack.Push(2)