mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-16 08:56:05 +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,145 @@
|
||||
/**
|
||||
* File: array_deque.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Double-ended queue based on circular array implementation */
|
||||
/* Constructor */
|
||||
class ArrayDeque(capacity: Int) {
|
||||
private var nums: IntArray = IntArray(capacity) // Array for storing double-ended queue elements
|
||||
private var front: Int = 0 // Front pointer, points to the front of the queue element
|
||||
private var queSize: Int = 0 // Double-ended queue length
|
||||
|
||||
/* Get the capacity of the double-ended queue */
|
||||
fun capacity(): Int {
|
||||
return nums.size
|
||||
}
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
fun size(): Int {
|
||||
return queSize
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
return queSize == 0
|
||||
}
|
||||
|
||||
/* Calculate circular array index */
|
||||
private fun 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 + capacity()) % capacity()
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
fun pushFirst(num: Int) {
|
||||
if (queSize == capacity()) {
|
||||
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
|
||||
front = index(front - 1)
|
||||
// Add num to front of queue
|
||||
nums[front] = num
|
||||
queSize++
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
fun pushLast(num: Int) {
|
||||
if (queSize == capacity()) {
|
||||
println("Double-ended queue is full")
|
||||
return
|
||||
}
|
||||
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
||||
val rear = index(front + queSize)
|
||||
// Front pointer moves one position backward
|
||||
nums[rear] = num
|
||||
queSize++
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
fun popFirst(): Int {
|
||||
val num = peekFirst()
|
||||
// Move front pointer backward by one position
|
||||
front = index(front + 1)
|
||||
queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
fun popLast(): Int {
|
||||
val num = peekLast()
|
||||
queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
fun peekFirst(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return nums[front]
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun peekLast(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
// Initialize double-ended queue
|
||||
val last = index(front + queSize - 1)
|
||||
return nums[last]
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
fun toArray(): IntArray {
|
||||
// Elements enqueue
|
||||
val res = IntArray(queSize)
|
||||
var i = 0
|
||||
var j = front
|
||||
while (i < queSize) {
|
||||
res[i] = nums[index(j)]
|
||||
i++
|
||||
j++
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* Get the length of the double-ended queue */
|
||||
val deque = ArrayDeque(10)
|
||||
deque.pushLast(3)
|
||||
deque.pushLast(2)
|
||||
deque.pushLast(5)
|
||||
println("Deque deque = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Update element */
|
||||
val peekFirst = deque.peekFirst()
|
||||
println("Front element peekFirst = $peekFirst")
|
||||
val peekLast = deque.peekLast()
|
||||
println("Rear element peekLast = $peekLast")
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.pushLast(4)
|
||||
println("After element 4 enqueues at rear, deque = ${deque.toArray().contentToString()}")
|
||||
deque.pushFirst(1)
|
||||
println("After element 1 enqueues at front, deque = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Element dequeue */
|
||||
val popLast = deque.popLast()
|
||||
println("Dequeue rear element = ${popLast}, after rear dequeue deque = ${deque.toArray().contentToString()}")
|
||||
val popFirst = deque.popFirst()
|
||||
println("Dequeue front element = ${popFirst}, after front dequeue deque = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
val size = deque.size()
|
||||
println("Deque length size = $size")
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
val isEmpty = deque.isEmpty()
|
||||
println("Is deque empty = $isEmpty")
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* 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()}")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* File: array_stack.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Stack based on array implementation */
|
||||
class ArrayStack {
|
||||
// Initialize list (dynamic array)
|
||||
private val stack = mutableListOf<Int>()
|
||||
|
||||
/* Get the length of the stack */
|
||||
fun size(): Int {
|
||||
return stack.size
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
return size() == 0
|
||||
}
|
||||
|
||||
/* Push */
|
||||
fun push(num: Int) {
|
||||
stack.add(num)
|
||||
}
|
||||
|
||||
/* Pop */
|
||||
fun pop(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return stack.removeAt(size() - 1)
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
fun peek(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return stack[size() - 1]
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
fun toArray(): Array<Any> {
|
||||
return stack.toTypedArray()
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* Access top of the stack element */
|
||||
val stack = ArrayStack()
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
println("Stack stack = ${stack.toArray().contentToString()}")
|
||||
|
||||
/* Return list for printing */
|
||||
val peek = stack.peek()
|
||||
println("Top element peek = $peek")
|
||||
|
||||
/* Element pop from stack */
|
||||
val pop = stack.pop()
|
||||
println("Pop element pop = $pop, after pop stack = ${stack.toArray().contentToString()}")
|
||||
|
||||
/* Get the length of the stack */
|
||||
val size = stack.size()
|
||||
println("Stack length size = $size")
|
||||
|
||||
/* Check if empty */
|
||||
val isEmpty = stack.isEmpty()
|
||||
println("Is stack empty = $isEmpty")
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* File: deque.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import java.util.*
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* Get the length of the double-ended queue */
|
||||
val deque = LinkedList<Int>()
|
||||
deque.offerLast(3)
|
||||
deque.offerLast(2)
|
||||
deque.offerLast(5)
|
||||
println("Deque deque = $deque")
|
||||
|
||||
/* Update element */
|
||||
val peekFirst = deque.peekFirst()
|
||||
println("Front element peekFirst = $peekFirst")
|
||||
val peekLast = deque.peekLast()
|
||||
println("Rear element peekLast = $peekLast")
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.offerLast(4)
|
||||
println("After element 4 enqueues at rear, deque = $deque")
|
||||
deque.offerFirst(1)
|
||||
println("After element 1 enqueues at front, deque = $deque")
|
||||
|
||||
/* Element dequeue */
|
||||
val popLast = deque.pollLast()
|
||||
println("Dequeue rear element = $popLast, after rear dequeue deque = $deque")
|
||||
val popFirst = deque.pollFirst()
|
||||
println("Dequeue front element = $popFirst, after front dequeue deque = $deque")
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
val size = deque.size
|
||||
println("Deque length size = $size")
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
val isEmpty = deque.isEmpty()
|
||||
println("Is deque empty = $isEmpty")
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* File: linkedlist_deque.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Doubly linked list node */
|
||||
class ListNode(var _val: Int) {
|
||||
// Node value
|
||||
var next: ListNode? = null // Successor node reference
|
||||
var prev: ListNode? = null // Predecessor node reference
|
||||
}
|
||||
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
class LinkedListDeque {
|
||||
private var front: ListNode? = null // Head node front
|
||||
private var rear: ListNode? = null // Tail node rear
|
||||
private var queSize: Int = 0 // Length of the double-ended queue
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
fun size(): Int {
|
||||
return queSize
|
||||
}
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
return size() == 0
|
||||
}
|
||||
|
||||
/* Enqueue operation */
|
||||
fun push(num: Int, isFront: Boolean) {
|
||||
val node = ListNode(num)
|
||||
// If the linked list is empty, make both front and rear point to node
|
||||
if (isEmpty()) {
|
||||
rear = node
|
||||
front = rear
|
||||
// Front of the queue enqueue operation
|
||||
} else if (isFront) {
|
||||
// Add node to the head of the linked list
|
||||
front?.prev = node
|
||||
node.next = front
|
||||
front = node // Update head node
|
||||
// Rear of the queue enqueue operation
|
||||
} else {
|
||||
// Add node to the tail of the linked list
|
||||
rear?.next = node
|
||||
node.prev = rear
|
||||
rear = node // Update tail node
|
||||
}
|
||||
queSize++ // Update queue length
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue */
|
||||
fun pushFirst(num: Int) {
|
||||
push(num, true)
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue */
|
||||
fun pushLast(num: Int) {
|
||||
push(num, false)
|
||||
}
|
||||
|
||||
/* Dequeue operation */
|
||||
fun pop(isFront: Boolean): Int {
|
||||
if (isEmpty())
|
||||
throw IndexOutOfBoundsException()
|
||||
val _val: Int
|
||||
// Temporarily store head node value
|
||||
if (isFront) {
|
||||
_val = front!!._val // Delete head node
|
||||
// Delete head node
|
||||
val fNext = front!!.next
|
||||
if (fNext != null) {
|
||||
fNext.prev = null
|
||||
front!!.next = null
|
||||
}
|
||||
front = fNext // Update head node
|
||||
// Temporarily store tail node value
|
||||
} else {
|
||||
_val = rear!!._val // Delete tail node
|
||||
// Update tail node
|
||||
val rPrev = rear!!.prev
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null
|
||||
rear!!.prev = null
|
||||
}
|
||||
rear = rPrev // Update tail node
|
||||
}
|
||||
queSize-- // Update queue length
|
||||
return _val
|
||||
}
|
||||
|
||||
/* Rear of the queue dequeue */
|
||||
fun popFirst(): Int {
|
||||
return pop(true)
|
||||
}
|
||||
|
||||
/* Access rear of the queue element */
|
||||
fun popLast(): Int {
|
||||
return pop(false)
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
fun peekFirst(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return front!!._val
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun peekLast(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return rear!!._val
|
||||
}
|
||||
|
||||
/* Return array for printing */
|
||||
fun toArray(): IntArray {
|
||||
var node = front
|
||||
val res = IntArray(size())
|
||||
for (i in res.indices) {
|
||||
res[i] = node!!._val
|
||||
node = node.next
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* Get the length of the double-ended queue */
|
||||
val deque = LinkedListDeque()
|
||||
deque.pushLast(3)
|
||||
deque.pushLast(2)
|
||||
deque.pushLast(5)
|
||||
println("Deque deque = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Update element */
|
||||
val peekFirst = deque.peekFirst()
|
||||
println("Front element peekFirst = $peekFirst")
|
||||
val peekLast = deque.peekLast()
|
||||
println("Rear element peekLast = $peekLast")
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.pushLast(4)
|
||||
println("After element 4 enqueues at rear, deque = ${deque.toArray().contentToString()}")
|
||||
deque.pushFirst(1)
|
||||
println("After element 1 enqueues at front, deque = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Element dequeue */
|
||||
val popLast = deque.popLast()
|
||||
println("Dequeue rear element = ${popLast}, after rear dequeue deque = ${deque.toArray().contentToString()}")
|
||||
val popFirst = deque.popFirst()
|
||||
println("Dequeue front element = ${popFirst}, after front dequeue deque = ${deque.toArray().contentToString()}")
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
val size = deque.size()
|
||||
println("Deque length size = $size")
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
val isEmpty = deque.isEmpty()
|
||||
println("Is deque empty = $isEmpty")
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* File: linkedlist_queue.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Queue based on linked list implementation */
|
||||
class LinkedListQueue(
|
||||
// Head node front, tail node rear
|
||||
private var front: ListNode? = null,
|
||||
private var rear: ListNode? = null,
|
||||
private var queSize: Int = 0
|
||||
) {
|
||||
|
||||
/* Get the length of the queue */
|
||||
fun size(): Int {
|
||||
return queSize
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
return size() == 0
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
fun push(num: Int) {
|
||||
// Add num after the tail node
|
||||
val node = ListNode(num)
|
||||
// If the queue is empty, make both front and rear point to the node
|
||||
if (front == null) {
|
||||
front = node
|
||||
rear = node
|
||||
// If the queue is not empty, add the node after the tail node
|
||||
} else {
|
||||
rear?.next = node
|
||||
rear = node
|
||||
}
|
||||
queSize++
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
fun pop(): Int {
|
||||
val num = peek()
|
||||
// Delete head node
|
||||
front = front?.next
|
||||
queSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
fun peek(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return front!!._val
|
||||
}
|
||||
|
||||
/* Convert linked list to Array and return */
|
||||
fun toArray(): IntArray {
|
||||
var node = front
|
||||
val res = IntArray(size())
|
||||
for (i in res.indices) {
|
||||
res[i] = node!!._val
|
||||
node = node.next
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* Access front of the queue element */
|
||||
val queue = LinkedListQueue()
|
||||
|
||||
/* 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")
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* File: linkedlist_stack.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* Stack based on linked list implementation */
|
||||
class LinkedListStack(
|
||||
private var stackPeek: ListNode? = null, // Use head node as stack top
|
||||
private var stkSize: Int = 0 // Stack length
|
||||
) {
|
||||
|
||||
/* Get the length of the stack */
|
||||
fun size(): Int {
|
||||
return stkSize
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
fun isEmpty(): Boolean {
|
||||
return size() == 0
|
||||
}
|
||||
|
||||
/* Push */
|
||||
fun push(num: Int) {
|
||||
val node = ListNode(num)
|
||||
node.next = stackPeek
|
||||
stackPeek = node
|
||||
stkSize++
|
||||
}
|
||||
|
||||
/* Pop */
|
||||
fun pop(): Int? {
|
||||
val num = peek()
|
||||
stackPeek = stackPeek?.next
|
||||
stkSize--
|
||||
return num
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
fun peek(): Int? {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return stackPeek?._val
|
||||
}
|
||||
|
||||
/* Convert List to Array and return */
|
||||
fun toArray(): IntArray {
|
||||
var node = stackPeek
|
||||
val res = IntArray(size())
|
||||
for (i in res.size - 1 downTo 0) {
|
||||
res[i] = node?._val!!
|
||||
node = node.next
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* Access top of the stack element */
|
||||
val stack = LinkedListStack()
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
println("Stack stack = ${stack.toArray().contentToString()}")
|
||||
|
||||
/* Return list for printing */
|
||||
val peek = stack.peek()!!
|
||||
println("Top element peek = $peek")
|
||||
|
||||
/* Element pop from stack */
|
||||
val pop = stack.pop()!!
|
||||
println("Pop element pop = $pop, after pop stack = ${stack.toArray().contentToString()}")
|
||||
|
||||
/* Get the length of the stack */
|
||||
val size = stack.size()
|
||||
println("Stack length size = $size")
|
||||
|
||||
/* Check if empty */
|
||||
val isEmpty = stack.isEmpty()
|
||||
println("Is stack empty = $isEmpty")
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: queue.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import java.util.*
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* Access front of the queue element */
|
||||
val queue = LinkedList<Int>()
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.offer(1)
|
||||
queue.offer(3)
|
||||
queue.offer(2)
|
||||
queue.offer(5)
|
||||
queue.offer(4)
|
||||
println("Queue queue = $queue")
|
||||
|
||||
/* Return list for printing */
|
||||
val peek = queue.peek()
|
||||
println("Front element peek = $peek")
|
||||
|
||||
/* Element dequeue */
|
||||
val pop = queue.poll()
|
||||
println("Dequeue element pop = $pop, after dequeue queue = $queue")
|
||||
|
||||
/* 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")
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: stack.kt
|
||||
* Created Time: 2024-01-25
|
||||
* Author: curtishd (1023632660@qq.com)
|
||||
*/
|
||||
|
||||
package chapter_stack_and_queue
|
||||
|
||||
import java.util.*
|
||||
|
||||
/* Driver Code */
|
||||
fun main() {
|
||||
/* Access top of the stack element */
|
||||
val stack = Stack<Int>()
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack.push(1)
|
||||
stack.push(3)
|
||||
stack.push(2)
|
||||
stack.push(5)
|
||||
stack.push(4)
|
||||
println("Stack stack = $stack")
|
||||
|
||||
/* Return list for printing */
|
||||
val peek = stack.peek()
|
||||
println("Top element peek = $peek")
|
||||
|
||||
/* Element pop from stack */
|
||||
val pop = stack.pop()
|
||||
println("Pop element pop = $pop, after pop stack = $stack")
|
||||
|
||||
/* Get the length of the stack */
|
||||
val size = stack.size
|
||||
println("Stack length size = $size")
|
||||
|
||||
/* Check if empty */
|
||||
val isEmpty = stack.isEmpty()
|
||||
println("Is stack empty = $isEmpty")
|
||||
}
|
||||
Reference in New Issue
Block a user