mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-03 22:24:16 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -6,15 +6,15 @@ package chapter_stack_and_queue
|
||||
|
||||
import "fmt"
|
||||
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
/* Deque implemented with a circular array */
|
||||
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)
|
||||
nums []int // Array that stores the deque elements
|
||||
front int // Front pointer, pointing to the first element
|
||||
queSize int // Deque size
|
||||
queCapacity int // Queue capacity (the maximum number of elements)
|
||||
}
|
||||
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
func newArrayDeque(queCapacity int) *arrayDeque {
|
||||
return &arrayDeque{
|
||||
nums: make([]int, queCapacity),
|
||||
@@ -24,64 +24,64 @@ func newArrayDeque(queCapacity int) *arrayDeque {
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
func (q *arrayDeque) size() int {
|
||||
return q.queSize
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
func (q *arrayDeque) isEmpty() bool {
|
||||
return q.queSize == 0
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
/* Calculate an index in the circular array */
|
||||
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
|
||||
// Use modulo to connect the end of the array to its beginning
|
||||
// When i passes the end of the array, wrap around to the beginning
|
||||
// When i passes the beginning of the array, wrap around to the end
|
||||
return (i + q.queCapacity) % q.queCapacity
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
/* Enqueue at the front */
|
||||
func (q *arrayDeque) pushFirst(num int) {
|
||||
if q.queSize == q.queCapacity {
|
||||
fmt.Println("Double-ended queue is full")
|
||||
fmt.Println("Deque 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
|
||||
// Move the front pointer one position to the left
|
||||
// Use modulo to wrap front to the end when it passes the beginning of the array
|
||||
q.front = q.index(q.front - 1)
|
||||
// Add num to front of queue
|
||||
// Add num to the front
|
||||
q.nums[q.front] = num
|
||||
q.queSize++
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
/* Enqueue at the rear */
|
||||
func (q *arrayDeque) pushLast(num int) {
|
||||
if q.queSize == q.queCapacity {
|
||||
fmt.Println("Double-ended queue is full")
|
||||
fmt.Println("Deque is full")
|
||||
return
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
// Calculate the rear pointer, which points one position past the last element
|
||||
rear := q.index(q.front + q.queSize)
|
||||
// Front pointer moves one position backward
|
||||
// Add num to the rear
|
||||
q.nums[rear] = num
|
||||
q.queSize++
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
/* Dequeue from the front */
|
||||
func (q *arrayDeque) popFirst() any {
|
||||
num := q.peekFirst()
|
||||
if num == nil {
|
||||
return nil
|
||||
}
|
||||
// Move front pointer backward by one position
|
||||
// Advance the front pointer by one position
|
||||
q.front = q.index(q.front + 1)
|
||||
q.queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
/* Dequeue from the rear */
|
||||
func (q *arrayDeque) popLast() any {
|
||||
num := q.peekLast()
|
||||
if num == nil {
|
||||
@@ -91,7 +91,7 @@ func (q *arrayDeque) popLast() any {
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
func (q *arrayDeque) peekFirst() any {
|
||||
if q.isEmpty() {
|
||||
return nil
|
||||
@@ -99,19 +99,19 @@ func (q *arrayDeque) peekFirst() any {
|
||||
return q.nums[q.front]
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
func (q *arrayDeque) peekLast() any {
|
||||
if q.isEmpty() {
|
||||
return nil
|
||||
}
|
||||
// Initialize double-ended queue
|
||||
// Calculate the index of the last element
|
||||
last := q.index(q.front + q.queSize - 1)
|
||||
return q.nums[last]
|
||||
}
|
||||
|
||||
/* Get Slice for printing */
|
||||
/* Get a slice for printing */
|
||||
func (q *arrayDeque) toSlice() []int {
|
||||
// Elements enqueue
|
||||
// Convert only the list elements within the occupied range
|
||||
res := make([]int, q.queSize)
|
||||
for i, j := 0, q.front; i < q.queSize; i++ {
|
||||
res[i] = q.nums[q.index(j)]
|
||||
|
||||
@@ -4,15 +4,15 @@
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
/* Queue implemented with a circular array */
|
||||
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)
|
||||
nums []int // Array that stores the queue elements
|
||||
front int // Front pointer, pointing to the first element
|
||||
queSize int // Queue size
|
||||
queCapacity int // Queue capacity (the maximum number of elements)
|
||||
}
|
||||
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
func newArrayQueue(queCapacity int) *arrayQueue {
|
||||
return &arrayQueue{
|
||||
nums: make([]int, queCapacity),
|
||||
@@ -22,26 +22,26 @@ func newArrayQueue(queCapacity int) *arrayQueue {
|
||||
}
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
func (q *arrayQueue) size() int {
|
||||
return q.queSize
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether 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
|
||||
// The queue is full when queSize == queCapacity
|
||||
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
|
||||
// Calculate the rear pointer, which points one position past the last element
|
||||
// Use modulo to wrap rear to the beginning when it passes the end of the array
|
||||
rear := (q.front + q.queSize) % q.queCapacity
|
||||
// Front pointer moves one position backward
|
||||
// Add num to the rear
|
||||
q.nums[rear] = num
|
||||
q.queSize++
|
||||
}
|
||||
@@ -53,13 +53,13 @@ func (q *arrayQueue) pop() any {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
// Advance the front pointer by one position, wrapping to the beginning if it passes the end
|
||||
q.front = (q.front + 1) % q.queCapacity
|
||||
q.queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
func (q *arrayQueue) peek() any {
|
||||
if q.isEmpty() {
|
||||
return nil
|
||||
@@ -67,7 +67,7 @@ func (q *arrayQueue) peek() any {
|
||||
return q.nums[q.front]
|
||||
}
|
||||
|
||||
/* Get Slice for printing */
|
||||
/* Get a slice for printing */
|
||||
func (q *arrayQueue) toSlice() []int {
|
||||
rear := (q.front + q.queSize)
|
||||
if rear >= q.queCapacity {
|
||||
|
||||
@@ -4,32 +4,32 @@
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Stack based on array implementation */
|
||||
/* Stack implemented with an array */
|
||||
type arrayStack struct {
|
||||
data []int // Data
|
||||
}
|
||||
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
func newArrayStack() *arrayStack {
|
||||
return &arrayStack{
|
||||
// Set stack length to 0, capacity to 16
|
||||
// Set the stack length to 0 and its capacity to 16
|
||||
data: make([]int, 0, 16),
|
||||
}
|
||||
}
|
||||
|
||||
/* Stack length */
|
||||
/* Stack size */
|
||||
func (s *arrayStack) size() int {
|
||||
return len(s.data)
|
||||
}
|
||||
|
||||
/* Is stack empty */
|
||||
/* Check whether the stack is empty */
|
||||
func (s *arrayStack) isEmpty() bool {
|
||||
return s.size() == 0
|
||||
}
|
||||
|
||||
/* Push */
|
||||
func (s *arrayStack) push(v int) {
|
||||
// Slice will automatically expand
|
||||
// Appending grows the slice capacity automatically when needed
|
||||
s.data = append(s.data, v)
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func (s *arrayStack) pop() any {
|
||||
return val
|
||||
}
|
||||
|
||||
/* Get stack top element */
|
||||
/* Get the top element */
|
||||
func (s *arrayStack) peek() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
@@ -49,7 +49,7 @@ func (s *arrayStack) peek() any {
|
||||
return val
|
||||
}
|
||||
|
||||
/* Get Slice for printing */
|
||||
/* Get a slice for printing */
|
||||
func (s *arrayStack) toSlice() []int {
|
||||
return s.data
|
||||
}
|
||||
|
||||
@@ -13,90 +13,90 @@ import (
|
||||
)
|
||||
|
||||
func TestDeque(t *testing.T) {
|
||||
/* Get the length of the double-ended queue */
|
||||
// In Go, use list as deque
|
||||
/* Initialize the deque */
|
||||
// In Go, list can be used as a deque
|
||||
deque := list.New()
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.PushBack(2)
|
||||
deque.PushBack(5)
|
||||
deque.PushBack(4)
|
||||
deque.PushFront(3)
|
||||
deque.PushFront(1)
|
||||
fmt.Print("Double-ended queue deque = ")
|
||||
fmt.Print("Deque deque = ")
|
||||
PrintList(deque)
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
front := deque.Front()
|
||||
fmt.Println("Front element front =", front.Value)
|
||||
rear := deque.Back()
|
||||
fmt.Println("Rear element rear =", rear.Value)
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
deque.Remove(front)
|
||||
fmt.Print("Front dequeue element front = ", front.Value, ", after front dequeue, deque = ")
|
||||
fmt.Print("Element dequeued from the front, front = ", front.Value, ", after dequeuing from the front, deque = ")
|
||||
PrintList(deque)
|
||||
deque.Remove(rear)
|
||||
fmt.Print("Rear dequeue element rear = ", rear.Value, ", after rear dequeue, deque = ")
|
||||
fmt.Print("Element dequeued from the rear, rear = ", rear.Value, ", after dequeuing from the rear, deque = ")
|
||||
PrintList(deque)
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
size := deque.Len()
|
||||
fmt.Println("Deque length size =", size)
|
||||
fmt.Println("Deque size =", size)
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
isEmpty := deque.Len() == 0
|
||||
fmt.Println("Is deque empty =", isEmpty)
|
||||
fmt.Println("Is the deque empty =", isEmpty)
|
||||
}
|
||||
|
||||
func TestArrayDeque(t *testing.T) {
|
||||
/* Get the length of the double-ended queue */
|
||||
// In Go, use list as deque
|
||||
/* Initialize the deque */
|
||||
// Use the custom circular-array deque implementation
|
||||
deque := newArrayDeque(16)
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.pushLast(3)
|
||||
deque.pushLast(2)
|
||||
deque.pushLast(5)
|
||||
fmt.Print("Double-ended queue deque = ")
|
||||
fmt.Print("Deque deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
|
||||
/* Update element */
|
||||
/* Access an element */
|
||||
peekFirst := deque.peekFirst()
|
||||
fmt.Println("Front element peekFirst =", peekFirst)
|
||||
peekLast := deque.peekLast()
|
||||
fmt.Println("Rear element peekLast =", peekLast)
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
deque.pushLast(4)
|
||||
fmt.Print("After element 4 enqueues at rear, deque = ")
|
||||
fmt.Print("After enqueuing element 4 at the rear, deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
deque.pushFirst(1)
|
||||
fmt.Print("After element 1 enqueues at front, deque = ")
|
||||
fmt.Print("After enqueuing element 1 at the front, deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
popFirst := deque.popFirst()
|
||||
fmt.Print("Front dequeue element popFirst = ", popFirst, ", after front dequeue, deque = ")
|
||||
fmt.Print("Element dequeued from the front, popFirst = ", popFirst, ", after dequeuing from the front, deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
popLast := deque.popLast()
|
||||
fmt.Print("Back dequeue element popLast = ", popLast, ", after rear dequeue, deque = ")
|
||||
fmt.Print("Element dequeued from the rear, popLast = ", popLast, ", after dequeuing from the rear, deque = ")
|
||||
PrintSlice(deque.toSlice())
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
/* Get the deque size */
|
||||
size := deque.size()
|
||||
fmt.Println("Deque length size =", size)
|
||||
fmt.Println("Deque size =", size)
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Check whether the deque is empty */
|
||||
isEmpty := deque.isEmpty()
|
||||
fmt.Println("Is deque empty =", isEmpty)
|
||||
fmt.Println("Is the deque empty =", isEmpty)
|
||||
}
|
||||
|
||||
func TestLinkedListDeque(t *testing.T) {
|
||||
// Access front of the queue element
|
||||
// Initialize the queue
|
||||
deque := newLinkedListDeque()
|
||||
|
||||
// Elements enqueue
|
||||
// Enqueue elements
|
||||
deque.pushLast(2)
|
||||
deque.pushLast(5)
|
||||
deque.pushLast(4)
|
||||
@@ -105,33 +105,33 @@ func TestLinkedListDeque(t *testing.T) {
|
||||
fmt.Print("Deque deque = ")
|
||||
PrintList(deque.toList())
|
||||
|
||||
// Return list for printing
|
||||
// Access the front element
|
||||
front := deque.peekFirst()
|
||||
fmt.Println("Front element front =", front)
|
||||
rear := deque.peekLast()
|
||||
fmt.Println("Rear element rear =", rear)
|
||||
|
||||
// Element dequeue
|
||||
// Dequeue an element
|
||||
popFirst := deque.popFirst()
|
||||
fmt.Print("Front dequeue element popFirst = ", popFirst, ", after front dequeue, deque = ")
|
||||
fmt.Print("Element dequeued from the front, popFirst = ", popFirst, ", after dequeuing from the front, deque = ")
|
||||
PrintList(deque.toList())
|
||||
popLast := deque.popLast()
|
||||
fmt.Print("Back dequeue element popLast = ", popLast, ", after rear dequeue, deque = ")
|
||||
fmt.Print("Element dequeued from the rear, popLast = ", popLast, ", after dequeuing from the rear, deque = ")
|
||||
PrintList(deque.toList())
|
||||
|
||||
// Get queue length
|
||||
// Get the deque size
|
||||
size := deque.size()
|
||||
fmt.Println("Queue length size =", size)
|
||||
fmt.Println("Deque size =", size)
|
||||
|
||||
// Check if empty
|
||||
// Check whether it is empty
|
||||
isEmpty := deque.isEmpty()
|
||||
fmt.Println("Is queue empty =", isEmpty)
|
||||
fmt.Println("Is the deque empty =", isEmpty)
|
||||
}
|
||||
|
||||
// BenchmarkLinkedListDeque 67.92 ns/op in Mac M1 Pro
|
||||
// BenchmarkLinkedListDeque: 67.92 ns/op on a Mac M1 Pro
|
||||
func BenchmarkLinkedListDeque(b *testing.B) {
|
||||
deque := newLinkedListDeque()
|
||||
// use b.N for looping
|
||||
// Use b.N as the iteration count
|
||||
for i := 0; i < b.N; i++ {
|
||||
deque.pushLast(777)
|
||||
}
|
||||
|
||||
@@ -8,30 +8,30 @@ import (
|
||||
"container/list"
|
||||
)
|
||||
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
/* Deque implemented with a doubly linked list */
|
||||
type linkedListDeque struct {
|
||||
// Use built-in package list
|
||||
// Use the standard library list package
|
||||
data *list.List
|
||||
}
|
||||
|
||||
/* Initialize deque */
|
||||
/* Initialize the deque */
|
||||
func newLinkedListDeque() *linkedListDeque {
|
||||
return &linkedListDeque{
|
||||
data: list.New(),
|
||||
}
|
||||
}
|
||||
|
||||
/* Front element enqueue */
|
||||
/* Enqueue an element at the front */
|
||||
func (s *linkedListDeque) pushFirst(value any) {
|
||||
s.data.PushFront(value)
|
||||
}
|
||||
|
||||
/* Rear element enqueue */
|
||||
/* Enqueue an element at the rear */
|
||||
func (s *linkedListDeque) pushLast(value any) {
|
||||
s.data.PushBack(value)
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
/* Dequeue the front element */
|
||||
func (s *linkedListDeque) popFirst() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
@@ -41,7 +41,7 @@ func (s *linkedListDeque) popFirst() any {
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Rear element dequeue */
|
||||
/* Dequeue the rear element */
|
||||
func (s *linkedListDeque) popLast() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
@@ -51,7 +51,7 @@ func (s *linkedListDeque) popLast() any {
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
func (s *linkedListDeque) peekFirst() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
@@ -60,7 +60,7 @@ func (s *linkedListDeque) peekFirst() any {
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access the rear element */
|
||||
func (s *linkedListDeque) peekLast() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
@@ -69,17 +69,17 @@ func (s *linkedListDeque) peekLast() any {
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
func (s *linkedListDeque) size() int {
|
||||
return s.data.Len()
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
func (s *linkedListDeque) isEmpty() bool {
|
||||
return s.data.Len() == 0
|
||||
}
|
||||
|
||||
/* Get List for printing */
|
||||
/* Get the list for printing */
|
||||
func (s *linkedListDeque) toList() *list.List {
|
||||
return s.data
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@ import (
|
||||
"container/list"
|
||||
)
|
||||
|
||||
/* Queue based on linked list implementation */
|
||||
/* Queue implemented with a linked list */
|
||||
type linkedListQueue struct {
|
||||
// Use built-in package list to implement queue
|
||||
// Use the standard library list package to implement the queue
|
||||
data *list.List
|
||||
}
|
||||
|
||||
/* Access front of the queue element */
|
||||
/* Initialize the queue */
|
||||
func newLinkedListQueue() *linkedListQueue {
|
||||
return &linkedListQueue{
|
||||
data: list.New(),
|
||||
@@ -36,7 +36,7 @@ func (s *linkedListQueue) pop() any {
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
func (s *linkedListQueue) peek() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
@@ -45,17 +45,17 @@ func (s *linkedListQueue) peek() any {
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
func (s *linkedListQueue) size() int {
|
||||
return s.data.Len()
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
func (s *linkedListQueue) isEmpty() bool {
|
||||
return s.data.Len() == 0
|
||||
}
|
||||
|
||||
/* Get List for printing */
|
||||
/* Get the list for printing */
|
||||
func (s *linkedListQueue) toList() *list.List {
|
||||
return s.data
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@ import (
|
||||
"container/list"
|
||||
)
|
||||
|
||||
/* Stack based on linked list implementation */
|
||||
/* Stack implemented with a linked list */
|
||||
type linkedListStack struct {
|
||||
// Use built-in package list to implement stack
|
||||
// Use the standard library list package to implement the stack
|
||||
data *list.List
|
||||
}
|
||||
|
||||
/* Access top of the stack element */
|
||||
/* Initialize the stack */
|
||||
func newLinkedListStack() *linkedListStack {
|
||||
return &linkedListStack{
|
||||
data: list.New(),
|
||||
@@ -36,7 +36,7 @@ func (s *linkedListStack) pop() any {
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
func (s *linkedListStack) peek() any {
|
||||
if s.isEmpty() {
|
||||
return nil
|
||||
@@ -45,17 +45,17 @@ func (s *linkedListStack) peek() any {
|
||||
return e.Value
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
func (s *linkedListStack) size() int {
|
||||
return s.data.Len()
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
/* Check whether the stack is empty */
|
||||
func (s *linkedListStack) isEmpty() bool {
|
||||
return s.data.Len() == 0
|
||||
}
|
||||
|
||||
/* Get List for printing */
|
||||
/* Get the list for printing */
|
||||
func (s *linkedListStack) toList() *list.List {
|
||||
return s.data
|
||||
}
|
||||
|
||||
@@ -13,11 +13,11 @@ import (
|
||||
)
|
||||
|
||||
func TestQueue(t *testing.T) {
|
||||
/* Access front of the queue element */
|
||||
// In Go, use list as queue
|
||||
/* Initialize the queue */
|
||||
// In Go, use list as a queue
|
||||
queue := list.New()
|
||||
|
||||
/* Elements enqueue */
|
||||
/* Enqueue elements */
|
||||
queue.PushBack(1)
|
||||
queue.PushBack(3)
|
||||
queue.PushBack(2)
|
||||
@@ -26,35 +26,35 @@ func TestQueue(t *testing.T) {
|
||||
fmt.Print("Queue queue = ")
|
||||
PrintList(queue)
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the front element */
|
||||
peek := queue.Front()
|
||||
fmt.Println("Front element peek =", peek.Value)
|
||||
|
||||
/* Element dequeue */
|
||||
/* Dequeue an element */
|
||||
pop := queue.Front()
|
||||
queue.Remove(pop)
|
||||
fmt.Print("Dequeue element pop = ", pop.Value, ", after dequeue, queue = ")
|
||||
fmt.Print("Dequeued element pop = ", pop.Value, ", after dequeuing, queue = ")
|
||||
PrintList(queue)
|
||||
|
||||
/* Get the length of the queue */
|
||||
/* Get the queue size */
|
||||
size := queue.Len()
|
||||
fmt.Println("Queue length size =", size)
|
||||
fmt.Println("Queue size =", size)
|
||||
|
||||
/* Check if the queue is empty */
|
||||
/* Check whether the queue is empty */
|
||||
isEmpty := queue.Len() == 0
|
||||
fmt.Println("Is queue empty =", isEmpty)
|
||||
fmt.Println("Is the queue empty =", isEmpty)
|
||||
}
|
||||
|
||||
func TestArrayQueue(t *testing.T) {
|
||||
|
||||
// Initialize queue using queue's common interface
|
||||
// Initialize the queue and use its standard queue operations
|
||||
capacity := 10
|
||||
queue := newArrayQueue(capacity)
|
||||
if queue.pop() != nil {
|
||||
t.Errorf("want:%v,got:%v", nil, queue.pop())
|
||||
}
|
||||
|
||||
// Elements enqueue
|
||||
// Enqueue elements
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
@@ -63,37 +63,37 @@ func TestArrayQueue(t *testing.T) {
|
||||
fmt.Print("Queue queue = ")
|
||||
PrintSlice(queue.toSlice())
|
||||
|
||||
// Return list for printing
|
||||
// Access the front element
|
||||
peek := queue.peek()
|
||||
fmt.Println("Front element peek =", peek)
|
||||
|
||||
// Element dequeue
|
||||
// Dequeue an element
|
||||
pop := queue.pop()
|
||||
fmt.Print("Dequeue element pop = ", pop, ", after dequeue, queue = ")
|
||||
fmt.Print("Dequeued element pop = ", pop, ", after dequeuing, queue = ")
|
||||
PrintSlice(queue.toSlice())
|
||||
|
||||
// Get queue length
|
||||
// Get the queue size
|
||||
size := queue.size()
|
||||
fmt.Println("Queue length size =", size)
|
||||
fmt.Println("Queue size =", size)
|
||||
|
||||
// Check if empty
|
||||
// Check whether it is empty
|
||||
isEmpty := queue.isEmpty()
|
||||
fmt.Println("Is queue empty =", isEmpty)
|
||||
fmt.Println("Is the queue empty =", isEmpty)
|
||||
|
||||
/* Test circular array */
|
||||
/* Test the circular array */
|
||||
for i := 0; i < 10; i++ {
|
||||
queue.push(i)
|
||||
queue.pop()
|
||||
fmt.Print("Round ", i, " enqueue + dequeue, queue =")
|
||||
fmt.Print("Round #", i, ": after enqueue + dequeue, queue =")
|
||||
PrintSlice(queue.toSlice())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinkedListQueue(t *testing.T) {
|
||||
// Initialize queue
|
||||
// Initialize the queue
|
||||
queue := newLinkedListQueue()
|
||||
|
||||
// Elements enqueue
|
||||
// Enqueue elements
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
@@ -102,29 +102,29 @@ func TestLinkedListQueue(t *testing.T) {
|
||||
fmt.Print("Queue queue = ")
|
||||
PrintList(queue.toList())
|
||||
|
||||
// Return list for printing
|
||||
// Access the front element
|
||||
peek := queue.peek()
|
||||
fmt.Println("Front element peek =", peek)
|
||||
|
||||
// Element dequeue
|
||||
// Dequeue an element
|
||||
pop := queue.pop()
|
||||
fmt.Print("Dequeue element pop = ", pop, ", after dequeue, queue = ")
|
||||
fmt.Print("Dequeued element pop = ", pop, ", after dequeuing, queue = ")
|
||||
PrintList(queue.toList())
|
||||
|
||||
// Get queue length
|
||||
// Get the queue size
|
||||
size := queue.size()
|
||||
fmt.Println("Queue length size =", size)
|
||||
fmt.Println("Queue size =", size)
|
||||
|
||||
// Check if empty
|
||||
// Check whether it is empty
|
||||
isEmpty := queue.isEmpty()
|
||||
fmt.Println("Is queue empty =", isEmpty)
|
||||
fmt.Println("Is the queue empty =", isEmpty)
|
||||
}
|
||||
|
||||
// BenchmarkArrayQueue 8 ns/op in Mac M1 Pro
|
||||
// BenchmarkArrayQueue: 8 ns/op on a Mac M1 Pro
|
||||
func BenchmarkArrayQueue(b *testing.B) {
|
||||
capacity := 1000
|
||||
queue := newArrayQueue(capacity)
|
||||
// use b.N for looping
|
||||
// Use b.N as the iteration count
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.push(777)
|
||||
}
|
||||
@@ -133,10 +133,10 @@ func BenchmarkArrayQueue(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkLinkedQueue 62.66 ns/op in Mac M1 Pro
|
||||
// BenchmarkLinkedQueue: 62.66 ns/op on a Mac M1 Pro
|
||||
func BenchmarkLinkedQueue(b *testing.B) {
|
||||
queue := newLinkedListQueue()
|
||||
// use b.N for looping
|
||||
// Use b.N as the iteration count
|
||||
for i := 0; i < b.N; i++ {
|
||||
queue.push(777)
|
||||
}
|
||||
|
||||
@@ -12,11 +12,11 @@ import (
|
||||
)
|
||||
|
||||
func TestStack(t *testing.T) {
|
||||
/* Access top of the stack element */
|
||||
// In Go, recommended to use Slice as stack
|
||||
/* Initialize the stack */
|
||||
// In Go, a slice is the recommended way to implement a stack
|
||||
var stack []int
|
||||
|
||||
/* Elements push onto stack */
|
||||
/* Push elements onto the stack */
|
||||
stack = append(stack, 1)
|
||||
stack = append(stack, 3)
|
||||
stack = append(stack, 2)
|
||||
@@ -25,30 +25,30 @@ func TestStack(t *testing.T) {
|
||||
fmt.Print("Stack stack = ")
|
||||
PrintSlice(stack)
|
||||
|
||||
/* Return list for printing */
|
||||
/* Access the top element */
|
||||
peek := stack[len(stack)-1]
|
||||
fmt.Println("Stack top element peek =", peek)
|
||||
fmt.Println("Top element peek =", peek)
|
||||
|
||||
/* Element pop from stack */
|
||||
/* Pop an element from the stack */
|
||||
pop := stack[len(stack)-1]
|
||||
stack = stack[:len(stack)-1]
|
||||
fmt.Print("Pop element pop = ", pop, ", after pop, stack = ")
|
||||
fmt.Print("Popped element pop = ", pop, ", after popping, stack = ")
|
||||
PrintSlice(stack)
|
||||
|
||||
/* Get the length of the stack */
|
||||
/* Get the stack size */
|
||||
size := len(stack)
|
||||
fmt.Println("Stack length size =", size)
|
||||
fmt.Println("Stack size =", size)
|
||||
|
||||
/* Check if empty */
|
||||
/* Check whether it is empty */
|
||||
isEmpty := len(stack) == 0
|
||||
fmt.Println("Is stack empty =", isEmpty)
|
||||
fmt.Println("Is the stack empty =", isEmpty)
|
||||
}
|
||||
|
||||
func TestArrayStack(t *testing.T) {
|
||||
// Initialize stack using interface
|
||||
// Initialize the stack for use through its stack operations
|
||||
stack := newArrayStack()
|
||||
|
||||
// Elements push onto stack
|
||||
// Push elements onto the stack
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
@@ -57,28 +57,28 @@ func TestArrayStack(t *testing.T) {
|
||||
fmt.Print("Stack stack = ")
|
||||
PrintSlice(stack.toSlice())
|
||||
|
||||
// Return list for printing
|
||||
// Access the top element
|
||||
peek := stack.peek()
|
||||
fmt.Println("Stack top element peek =", peek)
|
||||
fmt.Println("Top element peek =", peek)
|
||||
|
||||
// Element pop from stack
|
||||
// Pop an element from the stack
|
||||
pop := stack.pop()
|
||||
fmt.Print("Pop element pop = ", pop, ", after pop, stack = ")
|
||||
fmt.Print("Popped element pop = ", pop, ", after popping, stack = ")
|
||||
PrintSlice(stack.toSlice())
|
||||
|
||||
// Get the length of the stack
|
||||
// Get the stack size
|
||||
size := stack.size()
|
||||
fmt.Println("Stack length size =", size)
|
||||
fmt.Println("Stack size =", size)
|
||||
|
||||
// Check if empty
|
||||
// Check whether it is empty
|
||||
isEmpty := stack.isEmpty()
|
||||
fmt.Println("Is stack empty =", isEmpty)
|
||||
fmt.Println("Is the stack empty =", isEmpty)
|
||||
}
|
||||
|
||||
func TestLinkedListStack(t *testing.T) {
|
||||
// Access top of the stack element
|
||||
// Initialize the stack
|
||||
stack := newLinkedListStack()
|
||||
// Elements push onto stack
|
||||
// Push elements onto the stack
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
@@ -87,28 +87,28 @@ func TestLinkedListStack(t *testing.T) {
|
||||
fmt.Print("Stack stack = ")
|
||||
PrintList(stack.toList())
|
||||
|
||||
// Return list for printing
|
||||
// Access the top element
|
||||
peek := stack.peek()
|
||||
fmt.Println("Stack top element peek =", peek)
|
||||
fmt.Println("Top element peek =", peek)
|
||||
|
||||
// Element pop from stack
|
||||
// Pop an element from the stack
|
||||
pop := stack.pop()
|
||||
fmt.Print("Pop element pop = ", pop, ", after pop, stack = ")
|
||||
fmt.Print("Popped element pop = ", pop, ", after popping, stack = ")
|
||||
PrintList(stack.toList())
|
||||
|
||||
// Get the length of the stack
|
||||
// Get the stack size
|
||||
size := stack.size()
|
||||
fmt.Println("Stack length size =", size)
|
||||
fmt.Println("Stack size =", size)
|
||||
|
||||
// Check if empty
|
||||
// Check whether it is empty
|
||||
isEmpty := stack.isEmpty()
|
||||
fmt.Println("Is stack empty =", isEmpty)
|
||||
fmt.Println("Is the stack empty =", isEmpty)
|
||||
}
|
||||
|
||||
// BenchmarkArrayStack 8 ns/op in Mac M1 Pro
|
||||
// BenchmarkArrayStack: 8 ns/op on a Mac M1 Pro
|
||||
func BenchmarkArrayStack(b *testing.B) {
|
||||
stack := newArrayStack()
|
||||
// use b.N for looping
|
||||
// Use b.N as the iteration count
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.push(777)
|
||||
}
|
||||
@@ -117,10 +117,10 @@ func BenchmarkArrayStack(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkLinkedListStack 65.02 ns/op in Mac M1 Pro
|
||||
// BenchmarkLinkedListStack: 65.02 ns/op on a Mac M1 Pro
|
||||
func BenchmarkLinkedListStack(b *testing.B) {
|
||||
stack := newLinkedListStack()
|
||||
// use b.N for looping
|
||||
// Use b.N as the iteration count
|
||||
for i := 0; i < b.N; i++ {
|
||||
stack.push(777)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user