mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-24 18:43:59 +08:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
121
en/codes/go/chapter_stack_and_queue/array_deque.go
Normal file
121
en/codes/go/chapter_stack_and_queue/array_deque.go
Normal file
@@ -0,0 +1,121 @@
|
||||
// File: array_deque.go
|
||||
// Created Time: 2023-03-13
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import "fmt"
|
||||
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
type arrayDeque struct {
|
||||
nums []int // Array for storing double-ended queue elements
|
||||
front int // Front pointer, points to the front of the queue element
|
||||
queSize int // Double-ended queue length
|
||||
queCapacity int // Queue capacity (maximum number of elements)
|
||||
}
|
||||
|
||||
/* Access front of the queue element */
|
||||
func newArrayDeque(queCapacity int) *arrayDeque {
|
||||
return &arrayDeque{
|
||||
nums: make([]int, queCapacity),
|
||||
queCapacity: queCapacity,
|
||||
front: 0,
|
||||
queSize: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
func (q *arrayDeque) size() int {
|
||||
return q.queSize
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
func (q *arrayDeque) isEmpty() bool {
|
||||
return q.queSize == 0
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
func (q *arrayDeque) index(i int) int {
|
||||
// Use modulo operation to wrap the array head and tail together
|
||||
// When i passes the tail of the array, return to the head
|
||||
// When i passes the head of the array, return to the tail
|
||||
return (i + q.queCapacity) % q.queCapacity
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
func (q *arrayDeque) pushFirst(num int) {
|
||||
if q.queSize == q.queCapacity {
|
||||
fmt.Println("Double-ended queue is full")
|
||||
return
|
||||
}
|
||||
// Use modulo operation to wrap front around to the tail after passing the head of the array
|
||||
// Add num to the front of the queue
|
||||
q.front = q.index(q.front - 1)
|
||||
// Add num to front of queue
|
||||
q.nums[q.front] = num
|
||||
q.queSize++
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
func (q *arrayDeque) pushLast(num int) {
|
||||
if q.queSize == q.queCapacity {
|
||||
fmt.Println("Double-ended queue is full")
|
||||
return
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
rear := q.index(q.front + q.queSize)
|
||||
// Front pointer moves one position backward
|
||||
q.nums[rear] = num
|
||||
q.queSize++
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
func (q *arrayDeque) popFirst() any {
|
||||
num := q.peekFirst()
|
||||
if num == nil {
|
||||
return nil
|
||||
}
|
||||
// Move front pointer backward by one position
|
||||
q.front = q.index(q.front + 1)
|
||||
q.queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
func (q *arrayDeque) popLast() any {
|
||||
num := q.peekLast()
|
||||
if num == nil {
|
||||
return nil
|
||||
}
|
||||
q.queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
func (q *arrayDeque) peekFirst() any {
|
||||
if q.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
return q.nums[q.front]
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
func (q *arrayDeque) peekLast() any {
|
||||
if q.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
// Initialize double-ended queue
|
||||
last := q.index(q.front + q.queSize - 1)
|
||||
return q.nums[last]
|
||||
}
|
||||
|
||||
/* Get Slice for printing */
|
||||
func (q *arrayDeque) toSlice() []int {
|
||||
// Elements enqueue
|
||||
res := make([]int, q.queSize)
|
||||
for i, j := 0, q.front; i < q.queSize; i++ {
|
||||
res[i] = q.nums[q.index(j)]
|
||||
j++
|
||||
}
|
||||
return res
|
||||
}
|
||||
78
en/codes/go/chapter_stack_and_queue/array_queue.go
Normal file
78
en/codes/go/chapter_stack_and_queue/array_queue.go
Normal file
@@ -0,0 +1,78 @@
|
||||
// File: array_queue.go
|
||||
// Created Time: 2022-11-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
type arrayQueue struct {
|
||||
nums []int // Array for storing queue elements
|
||||
front int // Front pointer, points to the front of the queue element
|
||||
queSize int // Queue length
|
||||
queCapacity int // Queue capacity (maximum number of elements)
|
||||
}
|
||||
|
||||
/* Access front of the queue element */
|
||||
func newArrayQueue(queCapacity int) *arrayQueue {
|
||||
return &arrayQueue{
|
||||
nums: make([]int, queCapacity),
|
||||
queCapacity: queCapacity,
|
||||
front: 0,
|
||||
queSize: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
func (q *arrayQueue) size() int {
|
||||
return q.queSize
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
func (q *arrayQueue) isEmpty() bool {
|
||||
return q.queSize == 0
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
func (q *arrayQueue) push(num int) {
|
||||
// When rear == queCapacity, queue is full
|
||||
if q.queSize == q.queCapacity {
|
||||
return
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
// Add num to the rear of the queue
|
||||
rear := (q.front + q.queSize) % q.queCapacity
|
||||
// Front pointer moves one position backward
|
||||
q.nums[rear] = num
|
||||
q.queSize++
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
func (q *arrayQueue) pop() any {
|
||||
num := q.peek()
|
||||
if num == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
q.front = (q.front + 1) % q.queCapacity
|
||||
q.queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
func (q *arrayQueue) peek() any {
|
||||
if q.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
return q.nums[q.front]
|
||||
}
|
||||
|
||||
/* Get Slice for printing */
|
||||
func (q *arrayQueue) toSlice() []int {
|
||||
rear := (q.front + q.queSize)
|
||||
if rear >= q.queCapacity {
|
||||
rear %= q.queCapacity
|
||||
return append(q.nums[q.front:], q.nums[:rear]...)
|
||||
}
|
||||
return q.nums[q.front:rear]
|
||||
}
|
||||
55
en/codes/go/chapter_stack_and_queue/array_stack.go
Normal file
55
en/codes/go/chapter_stack_and_queue/array_stack.go
Normal file
@@ -0,0 +1,55 @@
|
||||
// File: array_stack.go
|
||||
// Created Time: 2022-11-26
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Stack based on array implementation */
|
||||
type arrayStack struct {
|
||||
data []int // Data
|
||||
}
|
||||
|
||||
/* Access top of the stack element */
|
||||
func newArrayStack() *arrayStack {
|
||||
return &arrayStack{
|
||||
// Set stack length to 0, capacity to 16
|
||||
data: make([]int, 0, 16),
|
||||
}
|
||||
}
|
||||
|
||||
/* Stack length */
|
||||
func (s *arrayStack) size() int {
|
||||
return len(s.data)
|
||||
}
|
||||
|
||||
/* Is stack empty */
|
||||
func (s *arrayStack) isEmpty() bool {
|
||||
return s.size() == 0
|
||||
}
|
||||
|
||||
/* Push */
|
||||
func (s *arrayStack) push(v int) {
|
||||
// Slice will automatically expand
|
||||
s.data = append(s.data, v)
|
||||
}
|
||||
|
||||
/* Pop */
|
||||
func (s *arrayStack) pop() any {
|
||||
val := s.peek()
|
||||
s.data = s.data[:len(s.data)-1]
|
||||
return val
|
||||
}
|
||||
|
||||
/* Get stack top element */
|
||||
func (s *arrayStack) peek() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
val := s.data[len(s.data)-1]
|
||||
return val
|
||||
}
|
||||
|
||||
/* Get Slice for printing */
|
||||
func (s *arrayStack) toSlice() []int {
|
||||
return s.data
|
||||
}
|
||||
141
en/codes/go/chapter_stack_and_queue/deque_test.go
Normal file
141
en/codes/go/chapter_stack_and_queue/deque_test.go
Normal file
@@ -0,0 +1,141 @@
|
||||
// File: deque_test.go
|
||||
// Created Time: 2022-11-29
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestDeque(t *testing.T) {
|
||||
/* Get the length of the double-ended queue */
|
||||
// In Go, use list as deque
|
||||
deque := list.New()
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.PushBack(2)
|
||||
deque.PushBack(5)
|
||||
deque.PushBack(4)
|
||||
deque.PushFront(3)
|
||||
deque.PushFront(1)
|
||||
fmt.Print("Double-ended queue deque = ")
|
||||
PrintList(deque)
|
||||
|
||||
/* Update element */
|
||||
front := deque.Front()
|
||||
fmt.Println("Front element front =", front.Value)
|
||||
rear := deque.Back()
|
||||
fmt.Println("Rear element rear =", rear.Value)
|
||||
|
||||
/* Element dequeue */
|
||||
deque.Remove(front)
|
||||
fmt.Print("Front dequeue element front = ", front.Value, ", after front dequeue, deque = ")
|
||||
PrintList(deque)
|
||||
deque.Remove(rear)
|
||||
fmt.Print("Rear dequeue element rear = ", rear.Value, ", after rear dequeue, deque = ")
|
||||
PrintList(deque)
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
size := deque.Len()
|
||||
fmt.Println("Deque length size =", size)
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
isEmpty := deque.Len() == 0
|
||||
fmt.Println("Is deque empty =", isEmpty)
|
||||
}
|
||||
|
||||
func TestArrayDeque(t *testing.T) {
|
||||
/* Get the length of the double-ended queue */
|
||||
// In Go, use list as deque
|
||||
deque := newArrayDeque(16)
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.pushLast(3)
|
||||
deque.pushLast(2)
|
||||
deque.pushLast(5)
|
||||
fmt.Print("Double-ended queue deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
|
||||
/* Update element */
|
||||
peekFirst := deque.peekFirst()
|
||||
fmt.Println("Front element peekFirst =", peekFirst)
|
||||
peekLast := deque.peekLast()
|
||||
fmt.Println("Rear element peekLast =", peekLast)
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.pushLast(4)
|
||||
fmt.Print("After element 4 enqueues at rear, deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
deque.pushFirst(1)
|
||||
fmt.Print("After element 1 enqueues at front, deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
|
||||
/* Element dequeue */
|
||||
popFirst := deque.popFirst()
|
||||
fmt.Print("Front dequeue element popFirst = ", popFirst, ", after front dequeue, deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
popLast := deque.popLast()
|
||||
fmt.Print("Back dequeue element popLast = ", popLast, ", after rear dequeue, deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
size := deque.size()
|
||||
fmt.Println("Deque length size =", size)
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
isEmpty := deque.isEmpty()
|
||||
fmt.Println("Is deque empty =", isEmpty)
|
||||
}
|
||||
|
||||
func TestLinkedListDeque(t *testing.T) {
|
||||
// Access front of the queue element
|
||||
deque := newLinkedListDeque()
|
||||
|
||||
// Elements enqueue
|
||||
deque.pushLast(2)
|
||||
deque.pushLast(5)
|
||||
deque.pushLast(4)
|
||||
deque.pushFirst(3)
|
||||
deque.pushFirst(1)
|
||||
fmt.Print("Deque deque = ")
|
||||
PrintList(deque.toList())
|
||||
|
||||
// Return list for printing
|
||||
front := deque.peekFirst()
|
||||
fmt.Println("Front element front =", front)
|
||||
rear := deque.peekLast()
|
||||
fmt.Println("Rear element rear =", rear)
|
||||
|
||||
// Element dequeue
|
||||
popFirst := deque.popFirst()
|
||||
fmt.Print("Front dequeue element popFirst = ", popFirst, ", after front dequeue, deque = ")
|
||||
PrintList(deque.toList())
|
||||
popLast := deque.popLast()
|
||||
fmt.Print("Back dequeue element popLast = ", popLast, ", after rear dequeue, deque = ")
|
||||
PrintList(deque.toList())
|
||||
|
||||
// Get queue length
|
||||
size := deque.size()
|
||||
fmt.Println("Queue length size =", size)
|
||||
|
||||
// Check if empty
|
||||
isEmpty := deque.isEmpty()
|
||||
fmt.Println("Is queue empty =", isEmpty)
|
||||
}
|
||||
|
||||
// BenchmarkLinkedListDeque 67.92 ns/op in Mac M1 Pro
|
||||
func BenchmarkLinkedListDeque(b *testing.B) {
|
||||
deque := newLinkedListDeque()
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
deque.pushLast(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
deque.popFirst()
|
||||
}
|
||||
}
|
||||
85
en/codes/go/chapter_stack_and_queue/linkedlist_deque.go
Normal file
85
en/codes/go/chapter_stack_and_queue/linkedlist_deque.go
Normal file
@@ -0,0 +1,85 @@
|
||||
// File: linkedlist_deque.go
|
||||
// Created Time: 2022-11-29
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
)
|
||||
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
type linkedListDeque struct {
|
||||
// Use built-in package list
|
||||
data *list.List
|
||||
}
|
||||
|
||||
/* Initialize deque */
|
||||
func newLinkedListDeque() *linkedListDeque {
|
||||
return &linkedListDeque{
|
||||
data: list.New(),
|
||||
}
|
||||
}
|
||||
|
||||
/* Front element enqueue */
|
||||
func (s *linkedListDeque) pushFirst(value any) {
|
||||
s.data.PushFront(value)
|
||||
}
|
||||
|
||||
/* Rear element enqueue */
|
||||
func (s *linkedListDeque) pushLast(value any) {
|
||||
s.data.PushBack(value)
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
func (s *linkedListDeque) popFirst() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Front()
|
||||
s.data.Remove(e)
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Rear element dequeue */
|
||||
func (s *linkedListDeque) popLast() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Back()
|
||||
s.data.Remove(e)
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
func (s *linkedListDeque) peekFirst() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Front()
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
func (s *linkedListDeque) peekLast() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Back()
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
func (s *linkedListDeque) size() int {
|
||||
return s.data.Len()
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
func (s *linkedListDeque) isEmpty() bool {
|
||||
return s.data.Len() == 0
|
||||
}
|
||||
|
||||
/* Get List for printing */
|
||||
func (s *linkedListDeque) toList() *list.List {
|
||||
return s.data
|
||||
}
|
||||
61
en/codes/go/chapter_stack_and_queue/linkedlist_queue.go
Normal file
61
en/codes/go/chapter_stack_and_queue/linkedlist_queue.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// File: linkedlist_queue.go
|
||||
// Created Time: 2022-11-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
)
|
||||
|
||||
/* Queue based on linked list implementation */
|
||||
type linkedListQueue struct {
|
||||
// Use built-in package list to implement queue
|
||||
data *list.List
|
||||
}
|
||||
|
||||
/* Access front of the queue element */
|
||||
func newLinkedListQueue() *linkedListQueue {
|
||||
return &linkedListQueue{
|
||||
data: list.New(),
|
||||
}
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
func (s *linkedListQueue) push(value any) {
|
||||
s.data.PushBack(value)
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
func (s *linkedListQueue) pop() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Front()
|
||||
s.data.Remove(e)
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
func (s *linkedListQueue) peek() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Front()
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
func (s *linkedListQueue) size() int {
|
||||
return s.data.Len()
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
func (s *linkedListQueue) isEmpty() bool {
|
||||
return s.data.Len() == 0
|
||||
}
|
||||
|
||||
/* Get List for printing */
|
||||
func (s *linkedListQueue) toList() *list.List {
|
||||
return s.data
|
||||
}
|
||||
61
en/codes/go/chapter_stack_and_queue/linkedlist_stack.go
Normal file
61
en/codes/go/chapter_stack_and_queue/linkedlist_stack.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// File: linkedlist_stack.go
|
||||
// Created Time: 2022-11-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
)
|
||||
|
||||
/* Stack based on linked list implementation */
|
||||
type linkedListStack struct {
|
||||
// Use built-in package list to implement stack
|
||||
data *list.List
|
||||
}
|
||||
|
||||
/* Access top of the stack element */
|
||||
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
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
func (s *linkedListStack) peek() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
e := s.data.Back()
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
func (s *linkedListStack) size() int {
|
||||
return s.data.Len()
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
func (s *linkedListStack) isEmpty() bool {
|
||||
return s.data.Len() == 0
|
||||
}
|
||||
|
||||
/* Get List for printing */
|
||||
func (s *linkedListStack) toList() *list.List {
|
||||
return s.data
|
||||
}
|
||||
146
en/codes/go/chapter_stack_and_queue/queue_test.go
Normal file
146
en/codes/go/chapter_stack_and_queue/queue_test.go
Normal file
@@ -0,0 +1,146 @@
|
||||
// File: queue_test.go
|
||||
// Created Time: 2022-11-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestQueue(t *testing.T) {
|
||||
/* Access front of the queue element */
|
||||
// In Go, use list as queue
|
||||
queue := list.New()
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.PushBack(1)
|
||||
queue.PushBack(3)
|
||||
queue.PushBack(2)
|
||||
queue.PushBack(5)
|
||||
queue.PushBack(4)
|
||||
fmt.Print("Queue queue = ")
|
||||
PrintList(queue)
|
||||
|
||||
/* Return list for printing */
|
||||
peek := queue.Front()
|
||||
fmt.Println("Front element peek =", peek.Value)
|
||||
|
||||
/* Element dequeue */
|
||||
pop := queue.Front()
|
||||
queue.Remove(pop)
|
||||
fmt.Print("Dequeue element pop = ", pop.Value, ", after dequeue, queue = ")
|
||||
PrintList(queue)
|
||||
|
||||
/* Get the length of the queue */
|
||||
size := queue.Len()
|
||||
fmt.Println("Queue length size =", size)
|
||||
|
||||
/* Check if the queue is empty */
|
||||
isEmpty := queue.Len() == 0
|
||||
fmt.Println("Is queue empty =", isEmpty)
|
||||
}
|
||||
|
||||
func TestArrayQueue(t *testing.T) {
|
||||
|
||||
// Initialize queue using queue's common interface
|
||||
capacity := 10
|
||||
queue := newArrayQueue(capacity)
|
||||
if queue.pop() != nil {
|
||||
t.Errorf("want:%v,got:%v", nil, queue.pop())
|
||||
}
|
||||
|
||||
// Elements enqueue
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
fmt.Print("Queue queue = ")
|
||||
PrintSlice(queue.toSlice())
|
||||
|
||||
// Return list for printing
|
||||
peek := queue.peek()
|
||||
fmt.Println("Front element peek =", peek)
|
||||
|
||||
// Element dequeue
|
||||
pop := queue.pop()
|
||||
fmt.Print("Dequeue element pop = ", pop, ", after dequeue, queue = ")
|
||||
PrintSlice(queue.toSlice())
|
||||
|
||||
// Get queue length
|
||||
size := queue.size()
|
||||
fmt.Println("Queue length size =", size)
|
||||
|
||||
// Check if empty
|
||||
isEmpty := queue.isEmpty()
|
||||
fmt.Println("Is queue empty =", isEmpty)
|
||||
|
||||
/* Test circular array */
|
||||
for i := 0; i < 10; i++ {
|
||||
queue.push(i)
|
||||
queue.pop()
|
||||
fmt.Print("Round ", i, " enqueue + dequeue, queue =")
|
||||
PrintSlice(queue.toSlice())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinkedListQueue(t *testing.T) {
|
||||
// Initialize queue
|
||||
queue := newLinkedListQueue()
|
||||
|
||||
// Elements enqueue
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
fmt.Print("Queue queue = ")
|
||||
PrintList(queue.toList())
|
||||
|
||||
// Return list for printing
|
||||
peek := queue.peek()
|
||||
fmt.Println("Front element peek =", peek)
|
||||
|
||||
// Element dequeue
|
||||
pop := queue.pop()
|
||||
fmt.Print("Dequeue element pop = ", pop, ", after dequeue, queue = ")
|
||||
PrintList(queue.toList())
|
||||
|
||||
// Get queue length
|
||||
size := queue.size()
|
||||
fmt.Println("Queue length size =", size)
|
||||
|
||||
// Check if empty
|
||||
isEmpty := queue.isEmpty()
|
||||
fmt.Println("Is queue empty =", isEmpty)
|
||||
}
|
||||
|
||||
// BenchmarkArrayQueue 8 ns/op in Mac M1 Pro
|
||||
func BenchmarkArrayQueue(b *testing.B) {
|
||||
capacity := 1000
|
||||
queue := newArrayQueue(capacity)
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.pop()
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkLinkedQueue 62.66 ns/op in Mac M1 Pro
|
||||
func BenchmarkLinkedQueue(b *testing.B) {
|
||||
queue := newLinkedListQueue()
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.pop()
|
||||
}
|
||||
}
|
||||
130
en/codes/go/chapter_stack_and_queue/stack_test.go
Normal file
130
en/codes/go/chapter_stack_and_queue/stack_test.go
Normal file
@@ -0,0 +1,130 @@
|
||||
// File: stack_test.go
|
||||
// Created Time: 2022-11-28
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
func TestStack(t *testing.T) {
|
||||
/* Access top of the stack element */
|
||||
// In Go, recommended to use Slice as stack
|
||||
var stack []int
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack = append(stack, 1)
|
||||
stack = append(stack, 3)
|
||||
stack = append(stack, 2)
|
||||
stack = append(stack, 5)
|
||||
stack = append(stack, 4)
|
||||
fmt.Print("Stack stack = ")
|
||||
PrintSlice(stack)
|
||||
|
||||
/* Return list for printing */
|
||||
peek := stack[len(stack)-1]
|
||||
fmt.Println("Stack top element peek =", peek)
|
||||
|
||||
/* Element pop from stack */
|
||||
pop := stack[len(stack)-1]
|
||||
stack = stack[:len(stack)-1]
|
||||
fmt.Print("Pop element pop = ", pop, ", after pop, stack = ")
|
||||
PrintSlice(stack)
|
||||
|
||||
/* Get the length of the stack */
|
||||
size := len(stack)
|
||||
fmt.Println("Stack length size =", size)
|
||||
|
||||
/* Check if empty */
|
||||
isEmpty := len(stack) == 0
|
||||
fmt.Println("Is stack empty =", isEmpty)
|
||||
}
|
||||
|
||||
func TestArrayStack(t *testing.T) {
|
||||
// Initialize stack using interface
|
||||
stack := newArrayStack()
|
||||
|
||||
// Elements push onto stack
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
fmt.Print("Stack stack = ")
|
||||
PrintSlice(stack.toSlice())
|
||||
|
||||
// Return list for printing
|
||||
peek := stack.peek()
|
||||
fmt.Println("Stack top element peek =", peek)
|
||||
|
||||
// Element pop from stack
|
||||
pop := stack.pop()
|
||||
fmt.Print("Pop element pop = ", pop, ", after pop, stack = ")
|
||||
PrintSlice(stack.toSlice())
|
||||
|
||||
// Get the length of the stack
|
||||
size := stack.size()
|
||||
fmt.Println("Stack length size =", size)
|
||||
|
||||
// Check if empty
|
||||
isEmpty := stack.isEmpty()
|
||||
fmt.Println("Is stack empty =", isEmpty)
|
||||
}
|
||||
|
||||
func TestLinkedListStack(t *testing.T) {
|
||||
// Access top of the stack element
|
||||
stack := newLinkedListStack()
|
||||
// Elements push onto stack
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
fmt.Print("Stack stack = ")
|
||||
PrintList(stack.toList())
|
||||
|
||||
// Return list for printing
|
||||
peek := stack.peek()
|
||||
fmt.Println("Stack top element peek =", peek)
|
||||
|
||||
// Element pop from stack
|
||||
pop := stack.pop()
|
||||
fmt.Print("Pop element pop = ", pop, ", after pop, stack = ")
|
||||
PrintList(stack.toList())
|
||||
|
||||
// Get the length of the stack
|
||||
size := stack.size()
|
||||
fmt.Println("Stack length size =", size)
|
||||
|
||||
// Check if empty
|
||||
isEmpty := stack.isEmpty()
|
||||
fmt.Println("Is stack empty =", isEmpty)
|
||||
}
|
||||
|
||||
// BenchmarkArrayStack 8 ns/op in Mac M1 Pro
|
||||
func BenchmarkArrayStack(b *testing.B) {
|
||||
stack := newArrayStack()
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.pop()
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkLinkedListStack 65.02 ns/op in Mac M1 Pro
|
||||
func BenchmarkLinkedListStack(b *testing.B) {
|
||||
stack := newLinkedListStack()
|
||||
// use b.N for looping
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.push(777)
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.pop()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user