Add build script for Go and update Go codes.

This commit is contained in:
krahets
2023-02-09 04:45:06 +08:00
parent 12c085a088
commit e8c78f89f0
39 changed files with 391 additions and 1468 deletions

View File

@@ -12,7 +12,7 @@ type arrayQueue struct {
queCapacity int // 队列容量(即最大容纳元素数量)
}
// newArrayQueue 基于环形数组实现的队列
/* 初始化队列 */
func newArrayQueue(queCapacity int) *arrayQueue {
return &arrayQueue{
nums: make([]int, queCapacity),
@@ -22,17 +22,17 @@ func newArrayQueue(queCapacity int) *arrayQueue {
}
}
// size 获取队列的长度
/* 获取队列的长度 */
func (q *arrayQueue) size() int {
return q.queSize
}
// isEmpty 判断队列是否为空
/* 判断队列是否为空 */
func (q *arrayQueue) isEmpty() bool {
return q.queSize == 0
}
// push 入队
/* 入队 */
func (q *arrayQueue) push(num int) {
// 当 rear == queCapacity 表示队列已满
if q.queSize == q.queCapacity {
@@ -46,7 +46,7 @@ func (q *arrayQueue) push(num int) {
q.queSize++
}
// poll 出队
/* 出队 */
func (q *arrayQueue) poll() any {
num := q.peek()
// 队首指针向后移动一位,若越过尾部则返回到数组头部
@@ -55,7 +55,7 @@ func (q *arrayQueue) poll() any {
return num
}
// peek 访问队首元素
/* 访问队首元素 */
func (q *arrayQueue) peek() any {
if q.isEmpty() {
return nil
@@ -63,7 +63,7 @@ func (q *arrayQueue) peek() any {
return q.nums[q.front]
}
// 获取 Slice 用于打印
/* 获取 Slice 用于打印 */
func (q *arrayQueue) toSlice() []int {
rear := (q.front + q.queSize)
if rear >= q.queCapacity {

View File

@@ -9,6 +9,7 @@ type arrayStack struct {
data []int // 数据
}
/* 初始化栈 */
func newArrayStack() *arrayStack {
return &arrayStack{
// 设置栈的长度为 0容量为 16
@@ -16,34 +17,30 @@ func newArrayStack() *arrayStack {
}
}
// size 栈的长度
/* 栈的长度 */
func (s *arrayStack) size() int {
return len(s.data)
}
// isEmpty 栈是否为空
/* 栈是否为空 */
func (s *arrayStack) isEmpty() bool {
return s.size() == 0
}
// push 入栈
/* 入栈 */
func (s *arrayStack) push(v int) {
// 切片会自动扩容
s.data = append(s.data, v)
}
// pop 出栈
/* 出栈 */
func (s *arrayStack) pop() any {
// 弹出栈前,先判断是否为空
if s.isEmpty() {
return nil
}
val := s.peek()
s.data = s.data[:len(s.data)-1]
return val
}
// peek 获取栈顶元素
/* 获取栈顶元素 */
func (s *arrayStack) peek() any {
if s.isEmpty() {
return nil
@@ -52,7 +49,7 @@ func (s *arrayStack) peek() any {
return val
}
// 获取 Slice 用于打印
/* 获取 Slice 用于打印 */
func (s *arrayStack) toSlice() []int {
return s.data
}

View File

@@ -8,29 +8,30 @@ import (
"container/list"
)
// linkedListDeque 基于链表实现的双端队列, 使用内置包 list 来实现栈
/* 基于链表实现的双端队列 */
type linkedListDeque struct {
// 使用内置包 list 来实现栈
data *list.List
}
// newLinkedListDeque 初始化双端队列
/* 初始化双端队列 */
func newLinkedListDeque() *linkedListDeque {
return &linkedListDeque{
data: list.New(),
}
}
// pushFirst 队首元素入队
/* 队首元素入队 */
func (s *linkedListDeque) pushFirst(value any) {
s.data.PushFront(value)
}
// pushLast 队尾元素入队
/* 队尾元素入队 */
func (s *linkedListDeque) pushLast(value any) {
s.data.PushBack(value)
}
// pollFirst 队首元素出队
/* 队首元素出队 */
func (s *linkedListDeque) pollFirst() any {
if s.isEmpty() {
return nil
@@ -40,7 +41,7 @@ func (s *linkedListDeque) pollFirst() any {
return e.Value
}
// pollLast 队尾元素出队
/* 队尾元素出队 */
func (s *linkedListDeque) pollLast() any {
if s.isEmpty() {
return nil
@@ -50,7 +51,7 @@ func (s *linkedListDeque) pollLast() any {
return e.Value
}
// peekFirst 访问队首元素
/* 访问队首元素 */
func (s *linkedListDeque) peekFirst() any {
if s.isEmpty() {
return nil
@@ -59,7 +60,7 @@ func (s *linkedListDeque) peekFirst() any {
return e.Value
}
// peekLast 访问队尾元素
/* 访问队尾元素 */
func (s *linkedListDeque) peekLast() any {
if s.isEmpty() {
return nil
@@ -68,17 +69,17 @@ func (s *linkedListDeque) peekLast() any {
return e.Value
}
// size 获取队列的长度
/* 获取队列的长度 */
func (s *linkedListDeque) size() int {
return s.data.Len()
}
// isEmpty 判断队列是否为空
/* 判断队列是否为空 */
func (s *linkedListDeque) isEmpty() bool {
return s.data.Len() == 0
}
// 获取 List 用于打印
/* 获取 List 用于打印 */
func (s *linkedListDeque) toList() *list.List {
return s.data
}

View File

@@ -14,19 +14,19 @@ type linkedListQueue struct {
data *list.List
}
// newLinkedListQueue 初始化链表
/* 初始化队列 */
func newLinkedListQueue() *linkedListQueue {
return &linkedListQueue{
data: list.New(),
}
}
// push 入队
/* 入队 */
func (s *linkedListQueue) push(value any) {
s.data.PushBack(value)
}
// poll 出队
/* 出队 */
func (s *linkedListQueue) poll() any {
if s.isEmpty() {
return nil
@@ -36,7 +36,7 @@ func (s *linkedListQueue) poll() any {
return e.Value
}
// peek 访问队首元素
/* 访问队首元素 */
func (s *linkedListQueue) peek() any {
if s.isEmpty() {
return nil
@@ -45,17 +45,17 @@ func (s *linkedListQueue) peek() any {
return e.Value
}
// size 获取队列的长度
/* 获取队列的长度 */
func (s *linkedListQueue) size() int {
return s.data.Len()
}
// isEmpty 判断队列是否为空
/* 判断队列是否为空 */
func (s *linkedListQueue) isEmpty() bool {
return s.data.Len() == 0
}
// 获取 List 用于打印
/* 获取 List 用于打印 */
func (s *linkedListQueue) toList() *list.List {
return s.data
}

View File

@@ -14,19 +14,19 @@ type linkedListStack struct {
data *list.List
}
// newLinkedListStack 初始化链表
/* 初始化栈 */
func newLinkedListStack() *linkedListStack {
return &linkedListStack{
data: list.New(),
}
}
// push 入栈
/* 入栈 */
func (s *linkedListStack) push(value int) {
s.data.PushBack(value)
}
// pop 出栈
/* 出栈 */
func (s *linkedListStack) pop() any {
if s.isEmpty() {
return nil
@@ -36,7 +36,7 @@ func (s *linkedListStack) pop() any {
return e.Value
}
// peek 访问栈顶元素
/* 访问栈顶元素 */
func (s *linkedListStack) peek() any {
if s.isEmpty() {
return nil
@@ -45,17 +45,17 @@ func (s *linkedListStack) peek() any {
return e.Value
}
// size 获取栈的长度
/* 获取栈的长度 */
func (s *linkedListStack) size() int {
return s.data.Len()
}
// isEmpty 判断栈是否为空
/* 判断栈是否为空 */
func (s *linkedListStack) isEmpty() bool {
return s.data.Len() == 0
}
// 获取 List 用于打印
/* 获取 List 用于打印 */
func (s *linkedListStack) toList() *list.List {
return s.data
}