mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-28 08:34:28 +00:00
2778a6f9c7
* 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
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
// 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]
|
|
}
|