style(go): fix go code style

Make the classes and methods in the package private, in case misuse
This commit is contained in:
reanon
2023-01-03 14:39:31 +08:00
parent e8f7d8f8ba
commit b73ac7bf4b
18 changed files with 255 additions and 254 deletions

View File

@@ -5,16 +5,16 @@
package chapter_stack_and_queue
/* 基于环形数组实现的队列 */
type ArrayQueue struct {
type arrayQueue struct {
data []int // 用于存储队列元素的数组
capacity int // 队列容量(即最多容量的元素个数)
front int // 头指针,指向队首
rear int // 尾指针,指向队尾 + 1
}
// NewArrayQueue 基于环形数组实现的队列
func NewArrayQueue(capacity int) *ArrayQueue {
return &ArrayQueue{
// newArrayQueue 基于环形数组实现的队列
func newArrayQueue(capacity int) *arrayQueue {
return &arrayQueue{
data: make([]int, capacity),
capacity: capacity,
front: 0,
@@ -22,21 +22,21 @@ func NewArrayQueue(capacity int) *ArrayQueue {
}
}
// Size 获取队列的长度
func (q *ArrayQueue) Size() int {
// size 获取队列的长度
func (q *arrayQueue) size() int {
size := (q.capacity + q.rear - q.front) % q.capacity
return size
}
// IsEmpty 判断队列是否为空
func (q *ArrayQueue) IsEmpty() bool {
// isEmpty 判断队列是否为空
func (q *arrayQueue) isEmpty() bool {
return q.rear-q.front == 0
}
// Offer 入队
func (q *ArrayQueue) Offer(v int) {
// offer 入队
func (q *arrayQueue) offer(v int) {
// 当 rear == capacity 表示队列已满
if q.Size() == q.capacity {
if q.size() == q.capacity {
return
}
// 尾结点后添加
@@ -45,9 +45,9 @@ func (q *ArrayQueue) Offer(v int) {
q.rear = (q.rear + 1) % q.capacity
}
// Poll 出队
func (q *ArrayQueue) Poll() any {
if q.IsEmpty() {
// poll 出队
func (q *arrayQueue) poll() any {
if q.isEmpty() {
return nil
}
v := q.data[q.front]
@@ -56,9 +56,9 @@ func (q *ArrayQueue) Poll() any {
return v
}
// Peek 访问队首元素
func (q *ArrayQueue) Peek() any {
if q.IsEmpty() {
// peek 访问队首元素
func (q *arrayQueue) peek() any {
if q.isEmpty() {
return nil
}
v := q.data[q.front]
@@ -66,6 +66,6 @@ func (q *ArrayQueue) Peek() any {
}
// 获取 Slice 用于打印
func (s *ArrayQueue) toSlice() []int {
return s.data[s.front:s.rear]
func (q *arrayQueue) toSlice() []int {
return q.data[q.front:q.rear]
}

View File

@@ -5,47 +5,47 @@
package chapter_stack_and_queue
/* 基于数组实现的栈 */
type ArrayStack struct {
type arrayStack struct {
data []int // 数据
}
func NewArrayStack() *ArrayStack {
return &ArrayStack{
func newArrayStack() *arrayStack {
return &arrayStack{
// 设置栈的长度为 0容量为 16
data: make([]int, 0, 16),
}
}
// Size 栈的长度
func (s *ArrayStack) Size() int {
// size 栈的长度
func (s *arrayStack) size() int {
return len(s.data)
}
// IsEmpty 栈是否为空
func (s *ArrayStack) IsEmpty() bool {
return s.Size() == 0
// isEmpty 栈是否为空
func (s *arrayStack) isEmpty() bool {
return s.size() == 0
}
// Push 入栈
func (s *ArrayStack) Push(v int) {
// push 入栈
func (s *arrayStack) push(v int) {
// 切片会自动扩容
s.data = append(s.data, v)
}
// Pop 出栈
func (s *ArrayStack) Pop() any {
// pop 出栈
func (s *arrayStack) pop() any {
// 弹出栈前,先判断是否为空
if s.IsEmpty() {
if s.isEmpty() {
return nil
}
val := s.Peek()
val := s.peek()
s.data = s.data[:len(s.data)-1]
return val
}
// Peek 获取栈顶元素
func (s *ArrayStack) Peek() any {
if s.IsEmpty() {
// peek 获取栈顶元素
func (s *arrayStack) peek() any {
if s.isEmpty() {
return nil
}
val := s.data[len(s.data)-1]
@@ -53,6 +53,6 @@ func (s *ArrayStack) Peek() any {
}
// 获取 Slice 用于打印
func (s *ArrayStack) toSlice() []int {
func (s *arrayStack) toSlice() []int {
return s.data
}

View File

@@ -51,48 +51,48 @@ func TestDeque(t *testing.T) {
func TestLinkedListDeque(t *testing.T) {
// 初始化队列
deque := NewLinkedListDeque()
deque := newLinkedListDeque()
// 元素入队
deque.OfferLast(2)
deque.OfferLast(5)
deque.OfferLast(4)
deque.OfferFirst(3)
deque.OfferFirst(1)
deque.offerLast(2)
deque.offerLast(5)
deque.offerLast(4)
deque.offerFirst(3)
deque.offerFirst(1)
fmt.Print("队列 deque = ")
PrintList(deque.toList())
// 访问队首元素
front := deque.PeekFirst()
front := deque.peekFirst()
fmt.Println("队首元素 front =", front)
rear := deque.PeekLast()
rear := deque.peekLast()
fmt.Println("队尾元素 rear =", rear)
// 元素出队
pollFirst := deque.PollFirst()
pollFirst := deque.pollFirst()
fmt.Print("队首出队元素 pollFirst = ", pollFirst, ",队首出队后 deque = ")
PrintList(deque.toList())
pollLast := deque.PollLast()
pollLast := deque.pollLast()
fmt.Print("队尾出队元素 pollLast = ", pollLast, ",队尾出队后 deque = ")
PrintList(deque.toList())
// 获取队的长度
size := deque.Size()
size := deque.size()
fmt.Println("队的长度 size =", size)
// 判断是否为空
isEmpty := deque.IsEmpty()
isEmpty := deque.isEmpty()
fmt.Println("队是否为空 =", isEmpty)
}
// BenchmarkArrayQueue 67.92 ns/op in Mac M1 Pro
func BenchmarkLinkedListDeque(b *testing.B) {
stack := NewLinkedListDeque()
stack := newLinkedListDeque()
// use b.N for looping
for i := 0; i < b.N; i++ {
stack.OfferLast(777)
stack.offerLast(777)
}
for i := 0; i < b.N; i++ {
stack.PollFirst()
stack.pollFirst()
}
}

View File

@@ -8,31 +8,31 @@ import (
"container/list"
)
// LinkedListDeque 基于链表实现的双端队列, 使用内置包 list 来实现栈
type LinkedListDeque struct {
// linkedListDeque 基于链表实现的双端队列, 使用内置包 list 来实现栈
type linkedListDeque struct {
data *list.List
}
// NewLinkedListDeque 初始化双端队列
func NewLinkedListDeque() *LinkedListDeque {
return &LinkedListDeque{
// newLinkedListDeque 初始化双端队列
func newLinkedListDeque() *linkedListDeque {
return &linkedListDeque{
data: list.New(),
}
}
// OfferFirst 队首元素入队
func (s *LinkedListDeque) OfferFirst(value any) {
// offerFirst 队首元素入队
func (s *linkedListDeque) offerFirst(value any) {
s.data.PushFront(value)
}
// OfferLast 队尾元素入队
func (s *LinkedListDeque) OfferLast(value any) {
// offerLast 队尾元素入队
func (s *linkedListDeque) offerLast(value any) {
s.data.PushBack(value)
}
// PollFirst 队首元素出队
func (s *LinkedListDeque) PollFirst() any {
if s.IsEmpty() {
// pollFirst 队首元素出队
func (s *linkedListDeque) pollFirst() any {
if s.isEmpty() {
return nil
}
e := s.data.Front()
@@ -40,9 +40,9 @@ func (s *LinkedListDeque) PollFirst() any {
return e.Value
}
// PollLast 队尾元素出队
func (s *LinkedListDeque) PollLast() any {
if s.IsEmpty() {
// pollLast 队尾元素出队
func (s *linkedListDeque) pollLast() any {
if s.isEmpty() {
return nil
}
e := s.data.Back()
@@ -50,35 +50,35 @@ func (s *LinkedListDeque) PollLast() any {
return e.Value
}
// PeekFirst 访问队首元素
func (s *LinkedListDeque) PeekFirst() any {
if s.IsEmpty() {
// peekFirst 访问队首元素
func (s *linkedListDeque) peekFirst() any {
if s.isEmpty() {
return nil
}
e := s.data.Front()
return e.Value
}
// PeekLast 访问队尾元素
func (s *LinkedListDeque) PeekLast() any {
if s.IsEmpty() {
// peekLast 访问队尾元素
func (s *linkedListDeque) peekLast() any {
if s.isEmpty() {
return nil
}
e := s.data.Back()
return e.Value
}
// Size 获取队列的长度
func (s *LinkedListDeque) Size() int {
// size 获取队列的长度
func (s *linkedListDeque) size() int {
return s.data.Len()
}
// IsEmpty 判断队列是否为空
func (s *LinkedListDeque) IsEmpty() bool {
// isEmpty 判断队列是否为空
func (s *linkedListDeque) isEmpty() bool {
return s.data.Len() == 0
}
// 获取 List 用于打印
func (s *LinkedListDeque) toList() *list.List {
func (s *linkedListDeque) toList() *list.List {
return s.data
}

View File

@@ -9,26 +9,26 @@ import (
)
/* 基于链表实现的队列 */
type LinkedListQueue struct {
type linkedListQueue struct {
// 使用内置包 list 来实现队列
data *list.List
}
// NewLinkedListQueue 初始化链表
func NewLinkedListQueue() *LinkedListQueue {
return &LinkedListQueue{
// newLinkedListQueue 初始化链表
func newLinkedListQueue() *linkedListQueue {
return &linkedListQueue{
data: list.New(),
}
}
// Offer 入队
func (s *LinkedListQueue) Offer(value any) {
// offer 入队
func (s *linkedListQueue) offer(value any) {
s.data.PushBack(value)
}
// Poll 出队
func (s *LinkedListQueue) Poll() any {
if s.IsEmpty() {
// poll 出队
func (s *linkedListQueue) poll() any {
if s.isEmpty() {
return nil
}
e := s.data.Front()
@@ -36,26 +36,26 @@ func (s *LinkedListQueue) Poll() any {
return e.Value
}
// Peek 访问队首元素
func (s *LinkedListQueue) Peek() any {
if s.IsEmpty() {
// peek 访问队首元素
func (s *linkedListQueue) peek() any {
if s.isEmpty() {
return nil
}
e := s.data.Front()
return e.Value
}
// Size 获取队列的长度
func (s *LinkedListQueue) Size() int {
// size 获取队列的长度
func (s *linkedListQueue) size() int {
return s.data.Len()
}
// IsEmpty 判断队列是否为空
func (s *LinkedListQueue) IsEmpty() bool {
// isEmpty 判断队列是否为空
func (s *linkedListQueue) isEmpty() bool {
return s.data.Len() == 0
}
// 获取 List 用于打印
func (s *LinkedListQueue) toList() *list.List {
func (s *linkedListQueue) toList() *list.List {
return s.data
}

View File

@@ -9,26 +9,26 @@ import (
)
/* 基于链表实现的栈 */
type LinkedListStack struct {
type linkedListStack struct {
// 使用内置包 list 来实现栈
data *list.List
}
// NewLinkedListStack 初始化链表
func NewLinkedListStack() *LinkedListStack {
return &LinkedListStack{
// newLinkedListStack 初始化链表
func newLinkedListStack() *linkedListStack {
return &linkedListStack{
data: list.New(),
}
}
// Push 入栈
func (s *LinkedListStack) Push(value int) {
// push 入栈
func (s *linkedListStack) push(value int) {
s.data.PushBack(value)
}
// Pop 出栈
func (s *LinkedListStack) Pop() any {
if s.IsEmpty() {
// pop 出栈
func (s *linkedListStack) pop() any {
if s.isEmpty() {
return nil
}
e := s.data.Back()
@@ -36,26 +36,26 @@ func (s *LinkedListStack) Pop() any {
return e.Value
}
// Peek 访问栈顶元素
func (s *LinkedListStack) Peek() any {
if s.IsEmpty() {
// peek 访问栈顶元素
func (s *linkedListStack) peek() any {
if s.isEmpty() {
return nil
}
e := s.data.Back()
return e.Value
}
// Size 获取栈的长度
func (s *LinkedListStack) Size() int {
// size 获取栈的长度
func (s *linkedListStack) size() int {
return s.data.Len()
}
// IsEmpty 判断栈是否为空
func (s *LinkedListStack) IsEmpty() bool {
// isEmpty 判断栈是否为空
func (s *linkedListStack) isEmpty() bool {
return s.data.Len() == 0
}
// 获取 List 用于打印
func (s *LinkedListStack) toList() *list.List {
func (s *linkedListStack) toList() *list.List {
return s.data
}

View File

@@ -48,87 +48,87 @@ func TestQueue(t *testing.T) {
func TestArrayQueue(t *testing.T) {
// 初始化队列,使用队列的通用接口
capacity := 10
queue := NewArrayQueue(capacity)
queue := newArrayQueue(capacity)
// 元素入队
queue.Offer(1)
queue.Offer(3)
queue.Offer(2)
queue.Offer(5)
queue.Offer(4)
queue.offer(1)
queue.offer(3)
queue.offer(2)
queue.offer(5)
queue.offer(4)
fmt.Print("队列 queue = ")
PrintSlice(queue.toSlice())
// 访问队首元素
peek := queue.Peek()
peek := queue.peek()
fmt.Println("队首元素 peek =", peek)
// 元素出队
poll := queue.Poll()
poll := queue.poll()
fmt.Print("出队元素 poll = ", poll, ", 出队后 queue = ")
PrintSlice(queue.toSlice())
// 获取队的长度
size := queue.Size()
size := queue.size()
fmt.Println("队的长度 size =", size)
// 判断是否为空
isEmpty := queue.IsEmpty()
isEmpty := queue.isEmpty()
fmt.Println("队是否为空 =", isEmpty)
}
func TestLinkedListQueue(t *testing.T) {
// 初始化队
queue := NewLinkedListQueue()
queue := newLinkedListQueue()
// 元素入队
queue.Offer(1)
queue.Offer(3)
queue.Offer(2)
queue.Offer(5)
queue.Offer(4)
queue.offer(1)
queue.offer(3)
queue.offer(2)
queue.offer(5)
queue.offer(4)
fmt.Print("队列 queue = ")
PrintList(queue.toList())
// 访问队首元素
peek := queue.Peek()
peek := queue.peek()
fmt.Println("队首元素 peek =", peek)
// 元素出队
poll := queue.Poll()
poll := queue.poll()
fmt.Print("出队元素 poll = ", poll, ", 出队后 queue = ")
PrintList(queue.toList())
// 获取队的长度
size := queue.Size()
size := queue.size()
fmt.Println("队的长度 size =", size)
// 判断是否为空
isEmpty := queue.IsEmpty()
isEmpty := queue.isEmpty()
fmt.Println("队是否为空 =", isEmpty)
}
// BenchmarkArrayQueue 8 ns/op in Mac M1 Pro
func BenchmarkArrayQueue(b *testing.B) {
capacity := 1000
stack := NewArrayQueue(capacity)
stack := newArrayQueue(capacity)
// use b.N for looping
for i := 0; i < b.N; i++ {
stack.Offer(777)
stack.offer(777)
}
for i := 0; i < b.N; i++ {
stack.Poll()
stack.poll()
}
}
// BenchmarkLinkedQueue 62.66 ns/op in Mac M1 Pro
func BenchmarkLinkedQueue(b *testing.B) {
stack := NewLinkedListQueue()
stack := newLinkedListQueue()
// use b.N for looping
for i := 0; i < b.N; i++ {
stack.Offer(777)
stack.offer(777)
}
for i := 0; i < b.N; i++ {
stack.Poll()
stack.poll()
}
}

View File

@@ -46,85 +46,85 @@ func TestStack(t *testing.T) {
func TestArrayStack(t *testing.T) {
// 初始化栈, 使用接口承接
stack := NewArrayStack()
stack := newArrayStack()
// 元素入栈
stack.Push(1)
stack.Push(3)
stack.Push(2)
stack.Push(5)
stack.Push(4)
stack.push(1)
stack.push(3)
stack.push(2)
stack.push(5)
stack.push(4)
fmt.Print("栈 stack = ")
PrintSlice(stack.toSlice())
// 访问栈顶元素
peek := stack.Peek()
peek := stack.peek()
fmt.Println("栈顶元素 peek =", peek)
// 元素出栈
pop := stack.Pop()
pop := stack.pop()
fmt.Print("出栈元素 pop = ", pop, ", 出栈后 stack = ")
PrintSlice(stack.toSlice())
// 获取栈的长度
size := stack.Size()
size := stack.size()
fmt.Println("栈的长度 size =", size)
// 判断是否为空
isEmpty := stack.IsEmpty()
isEmpty := stack.isEmpty()
fmt.Println("栈是否为空 =", isEmpty)
}
func TestLinkedListStack(t *testing.T) {
// 初始化栈
stack := NewLinkedListStack()
stack := newLinkedListStack()
// 元素入栈
stack.Push(1)
stack.Push(3)
stack.Push(2)
stack.Push(5)
stack.Push(4)
stack.push(1)
stack.push(3)
stack.push(2)
stack.push(5)
stack.push(4)
fmt.Print("栈 stack = ")
PrintList(stack.toList())
// 访问栈顶元素
peek := stack.Peek()
peek := stack.peek()
fmt.Println("栈顶元素 peek =", peek)
// 元素出栈
pop := stack.Pop()
pop := stack.pop()
fmt.Print("出栈元素 pop = ", pop, ", 出栈后 stack = ")
PrintList(stack.toList())
// 获取栈的长度
size := stack.Size()
size := stack.size()
fmt.Println("栈的长度 size =", size)
// 判断是否为空
isEmpty := stack.IsEmpty()
isEmpty := stack.isEmpty()
fmt.Println("栈是否为空 =", isEmpty)
}
// BenchmarkArrayStack 8 ns/op in Mac M1 Pro
func BenchmarkArrayStack(b *testing.B) {
stack := NewArrayStack()
stack := newArrayStack()
// use b.N for looping
for i := 0; i < b.N; i++ {
stack.Push(777)
stack.push(777)
}
for i := 0; i < b.N; i++ {
stack.Pop()
stack.pop()
}
}
// BenchmarkLinkedListStack 65.02 ns/op in Mac M1 Pro
func BenchmarkLinkedListStack(b *testing.B) {
stack := NewLinkedListStack()
stack := newLinkedListStack()
// use b.N for looping
for i := 0; i < b.N; i++ {
stack.Push(777)
stack.push(777)
}
for i := 0; i < b.N; i++ {
stack.Pop()
stack.pop()
}
}