Add Go codes to docs, including

the chapter of stack and queue, the chapter of tree.
This commit is contained in:
Yudong Jin
2022-12-03 20:25:24 +08:00
parent 1d9a076cdd
commit ebf9024136
41 changed files with 898 additions and 486 deletions

View File

@@ -46,7 +46,7 @@ comments: true
deque.offerFirst(3); // 添加至队首
deque.offerFirst(1);
/* 访问队首元素 */
/* 访问元素 */
int peekFirst = deque.peekFirst(); // 队首元素
int peekLast = deque.peekLast(); // 队尾元素
@@ -74,7 +74,7 @@ comments: true
deque.push_front(3); // 添加至队首
deque.push_front(1);
/* 访问队首元素 */
/* 访问元素 */
int front = deque.front(); // 队首元素
int back = deque.back(); // 队尾元素
@@ -102,7 +102,7 @@ comments: true
duque.appendleft(3) # 添加至队首
duque.appendleft(1)
""" 访问队首元素 """
""" 访问元素 """
front = duque[0] # 队首元素
rear = duque[-1] # 队尾元素
@@ -120,7 +120,30 @@ comments: true
=== "Go"
```go title="deque.go"
/* 初始化双向队列 */
// 在 Go 中,将 list 作为双向队列使用
deque := list.New()
/* 元素入队 */
deque.PushBack(2) // 添加至队尾
deque.PushBack(5)
deque.PushBack(4)
deque.PushFront(3) // 添加至队首
deque.PushFront(1)
/* 访问元素 */
front := deque.Front() // 队首元素
rear := deque.Back() // 队尾元素
/* 元素出队 */
deque.Remove(front) // 队首元素出队
deque.Remove(rear) // 队尾元素出队
/* 获取双向队列的长度 */
size := deque.Len()
/* 判断双向队列是否为空 */
isEmpty := deque.Len() == 0
```
=== "JavaScript"

View File

@@ -115,7 +115,29 @@ comments: true
=== "Go"
```go title="queue.go"
/* 初始化队列 */
// 在 Go 中,将 list 作为队列来使用
queue := list.New()
/* 元素入队 */
queue.PushBack(1)
queue.PushBack(3)
queue.PushBack(2)
queue.PushBack(5)
queue.PushBack(4)
/* 访问队首元素 */
peek := queue.Front()
/* 元素出队 */
poll := queue.Front()
queue.Remove(poll)
/* 获取队列的长度 */
size := queue.Len()
/* 判断队列是否为空 */
isEmpty := queue.Len() == 0
```
=== "JavaScript"
@@ -309,7 +331,52 @@ comments: true
=== "Go"
```go title="linkedlist_queue.go"
/* 基于链表实现的队列 */
type LinkedListQueue struct {
// 使用内置包 list 来实现队列
data *list.List
}
// NewLinkedListQueue 初始化链表
func NewLinkedListQueue() *LinkedListQueue {
return &LinkedListQueue{
data: list.New(),
}
}
// Offer 入队
func (s *LinkedListQueue) Offer(value any) {
s.data.PushBack(value)
}
// Poll 出队
func (s *LinkedListQueue) Poll() any {
if s.IsEmpty() {
return nil
}
e := s.data.Front()
s.data.Remove(e)
return e.Value
}
// Peek 访问队首元素
func (s *LinkedListQueue) Peek() any {
if s.IsEmpty() {
return nil
}
e := s.data.Front()
return e.Value
}
// Size 获取队列的长度
func (s *LinkedListQueue) Size() int {
return s.data.Len()
}
// IsEmpty 判断队列是否为空
func (s *LinkedListQueue) IsEmpty() bool {
return s.data.Len() == 0
}
```
=== "JavaScript"
@@ -540,7 +607,66 @@ comments: true
=== "Go"
```go title="array_queue.go"
/* 基于环形数组实现的队列 */
type ArrayQueue struct {
data []int // 用于存储队列元素的数组
capacity int // 队列容量(即最多容量的元素个数)
front int // 头指针,指向队首
rear int // 尾指针,指向队尾 + 1
}
// NewArrayQueue 基于环形数组实现的队列
func NewArrayQueue(capacity int) *ArrayQueue {
return &ArrayQueue{
data: make([]int, capacity),
capacity: capacity,
front: 0,
rear: 0,
}
}
// Size 获取队列的长度
func (q *ArrayQueue) Size() int {
size := (q.capacity + q.rear - q.front) % q.capacity
return size
}
// IsEmpty 判断队列是否为空
func (q *ArrayQueue) IsEmpty() bool {
return q.rear-q.front == 0
}
// Offer 入队
func (q *ArrayQueue) Offer(v int) {
// 当 rear == capacity 表示队列已满
if q.Size() == q.capacity {
return
}
// 尾结点后添加
q.data[q.rear] = v
// 尾指针向后移动一位,越过尾部后返回到数组头部
q.rear = (q.rear + 1) % q.capacity
}
// Poll 出队
func (q *ArrayQueue) Poll() any {
if q.IsEmpty() {
return nil
}
v := q.data[q.front]
// 队头指针向后移动一位,若越过尾部则返回到数组头部
q.front = (q.front + 1) % q.capacity
return v
}
// Peek 访问队首元素
func (q *ArrayQueue) Peek() any {
if q.IsEmpty() {
return nil
}
v := q.data[q.front]
return v
}
```
=== "JavaScript"

View File

@@ -115,7 +115,29 @@ comments: true
=== "Go"
```go title="stack.go"
/* 初始化栈 */
// 在 Go 中,推荐将 Slice 当作栈来使用
var stack []int
/* 元素入栈 */
stack = append(stack, 1)
stack = append(stack, 3)
stack = append(stack, 2)
stack = append(stack, 5)
stack = append(stack, 4)
/* 访问栈顶元素 */
peek := stack[len(stack)-1]
/* 元素出栈 */
pop := stack[len(stack)-1]
stack = stack[:len(stack)-1]
/* 获取栈的长度 */
size := len(stack)
/* 判断是否为空 */
isEmpty := len(stack) == 0
```
=== "JavaScript"
@@ -282,7 +304,52 @@ comments: true
=== "Go"
```go title="linkedlist_stack.go"
/* 基于链表实现的栈 */
type LinkedListStack struct {
// 使用内置包 list 来实现栈
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
}
e := s.data.Back()
s.data.Remove(e)
return e.Value
}
// Peek 访问栈顶元素
func (s *LinkedListStack) Peek() any {
if s.IsEmpty() {
return nil
}
e := s.data.Back()
return e.Value
}
// Size 获取栈的长度
func (s *LinkedListStack) Size() int {
return s.data.Len()
}
// IsEmpty 判断栈是否为空
func (s *LinkedListStack) IsEmpty() bool {
return s.data.Len() == 0
}
```
=== "JavaScript"
@@ -426,7 +493,53 @@ comments: true
=== "Go"
```go title="array_stack.go"
/* 基于数组实现的栈 */
type ArrayStack struct {
data []int // 数据
}
func NewArrayStack() *ArrayStack {
return &ArrayStack{
// 设置栈的长度为 0容量为 16
data: make([]int, 0, 16),
}
}
// 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
}
val := s.data[len(s.data)-1]
return val
}
```
=== "JavaScript"