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
110 lines
2.8 KiB
Kotlin
110 lines
2.8 KiB
Kotlin
/**
|
|
* File: array_queue.kt
|
|
* Created Time: 2024-01-25
|
|
* Author: curtishd (1023632660@qq.com)
|
|
*/
|
|
|
|
package chapter_stack_and_queue
|
|
|
|
/* Queue based on circular array implementation */
|
|
class ArrayQueue(capacity: Int) {
|
|
private val nums: IntArray = IntArray(capacity) // Array for storing queue elements
|
|
private var front: Int = 0 // Front pointer, points to the front of the queue element
|
|
private var queSize: Int = 0 // Queue length
|
|
|
|
/* Get the capacity of the queue */
|
|
fun capacity(): Int {
|
|
return nums.size
|
|
}
|
|
|
|
/* Get the length of the queue */
|
|
fun size(): Int {
|
|
return queSize
|
|
}
|
|
|
|
/* Check if the queue is empty */
|
|
fun isEmpty(): Boolean {
|
|
return queSize == 0
|
|
}
|
|
|
|
/* Enqueue */
|
|
fun push(num: Int) {
|
|
if (queSize == capacity()) {
|
|
println("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
|
|
val rear = (front + queSize) % capacity()
|
|
// Front pointer moves one position backward
|
|
nums[rear] = num
|
|
queSize++
|
|
}
|
|
|
|
/* Dequeue */
|
|
fun pop(): Int {
|
|
val num = peek()
|
|
// Move front pointer backward by one position, if it passes the tail, return to array head
|
|
front = (front + 1) % capacity()
|
|
queSize--
|
|
return num
|
|
}
|
|
|
|
/* Return list for printing */
|
|
fun peek(): Int {
|
|
if (isEmpty()) throw IndexOutOfBoundsException()
|
|
return nums[front]
|
|
}
|
|
|
|
/* Return array */
|
|
fun toArray(): IntArray {
|
|
// Elements enqueue
|
|
val res = IntArray(queSize)
|
|
var i = 0
|
|
var j = front
|
|
while (i < queSize) {
|
|
res[i] = nums[j % capacity()]
|
|
i++
|
|
j++
|
|
}
|
|
return res
|
|
}
|
|
}
|
|
|
|
/* Driver Code */
|
|
fun main() {
|
|
/* Access front of the queue element */
|
|
val capacity = 10
|
|
val queue = ArrayQueue(capacity)
|
|
|
|
/* Elements enqueue */
|
|
queue.push(1)
|
|
queue.push(3)
|
|
queue.push(2)
|
|
queue.push(5)
|
|
queue.push(4)
|
|
println("Queue queue = ${queue.toArray().contentToString()}")
|
|
|
|
/* Return list for printing */
|
|
val peek = queue.peek()
|
|
println("Front element peek = $peek")
|
|
|
|
/* Element dequeue */
|
|
val pop = queue.pop()
|
|
println("Dequeue element pop = ${pop}, after dequeue queue = ${queue.toArray().contentToString()}")
|
|
|
|
/* Get the length of the queue */
|
|
val size = queue.size()
|
|
println("Queue length size = $size")
|
|
|
|
/* Check if the queue is empty */
|
|
val isEmpty = queue.isEmpty()
|
|
println("Is queue empty = $isEmpty")
|
|
|
|
/* Test circular array */
|
|
for (i in 0..9) {
|
|
queue.push(i)
|
|
queue.pop()
|
|
println("After round $i enqueue + dequeue, queue = ${queue.toArray().contentToString()}")
|
|
}
|
|
} |