mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-04 03:34:21 +00: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:
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* File: array_queue.swift
|
||||
* Created Time: 2023-01-11
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
class ArrayQueue {
|
||||
private var nums: [Int] // Array for storing queue elements
|
||||
private var front: Int // Front pointer, points to the front of the queue element
|
||||
private var _size: Int // Queue length
|
||||
|
||||
init(capacity: Int) {
|
||||
// Initialize array
|
||||
nums = Array(repeating: 0, count: capacity)
|
||||
front = 0
|
||||
_size = 0
|
||||
}
|
||||
|
||||
/* Get the capacity of the queue */
|
||||
func capacity() -> Int {
|
||||
nums.count
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
func size() -> Int {
|
||||
_size
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
func isEmpty() -> Bool {
|
||||
size() == 0
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
func push(num: Int) {
|
||||
if size() == capacity() {
|
||||
print("Queue is full")
|
||||
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
|
||||
let rear = (front + size()) % capacity()
|
||||
// Front pointer moves one position backward
|
||||
nums[rear] = num
|
||||
_size += 1
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
@discardableResult
|
||||
func pop() -> Int {
|
||||
let num = peek()
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
front = (front + 1) % capacity()
|
||||
_size -= 1
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
func peek() -> Int {
|
||||
if isEmpty() {
|
||||
fatalError("Queue is empty")
|
||||
}
|
||||
return nums[front]
|
||||
}
|
||||
|
||||
/* Return array */
|
||||
func toArray() -> [Int] {
|
||||
// Elements enqueue
|
||||
(front ..< front + size()).map { nums[$0 % capacity()] }
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum _ArrayQueue {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Access front of the queue element */
|
||||
let capacity = 10
|
||||
let queue = ArrayQueue(capacity: capacity)
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.push(num: 1)
|
||||
queue.push(num: 3)
|
||||
queue.push(num: 2)
|
||||
queue.push(num: 5)
|
||||
queue.push(num: 4)
|
||||
print("Queue queue = \(queue.toArray())")
|
||||
|
||||
/* Return list for printing */
|
||||
let peek = queue.peek()
|
||||
print("Front element peek = \(peek)")
|
||||
|
||||
/* Element dequeue */
|
||||
let pop = queue.pop()
|
||||
print("Dequeue element pop = \(pop), after dequeue queue = \(queue.toArray())")
|
||||
|
||||
/* Get the length of the queue */
|
||||
let size = queue.size()
|
||||
print("Queue length size = \(size)")
|
||||
|
||||
/* Check if the queue is empty */
|
||||
let isEmpty = queue.isEmpty()
|
||||
print("Is queue empty = \(isEmpty)")
|
||||
|
||||
/* Test circular array */
|
||||
for i in 0 ..< 10 {
|
||||
queue.push(num: i)
|
||||
queue.pop()
|
||||
print("After round \(i) enqueue + dequeue, queue = \(queue.toArray())")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user