mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-24 10:33:34 +08:00
Many bug fixes and improvements (#1270)
* Add Ruby and Kotlin icons Add the avatar of @curtishd * Update README * Synchronize zh-hant and zh versions. * Translate the pythontutor blocks to traditional Chinese * Fix en/mkdocs.yml * Update the landing page of the en version. * Fix the Dockerfile * Refine the en landingpage * Fix en landing page * Reset the README.md
This commit is contained in:
@@ -7,10 +7,11 @@
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* 基於環形陣列實現的雙向佇列 */
|
||||
/* 建構子 */
|
||||
class ArrayDeque(capacity: Int) {
|
||||
private var nums = IntArray(capacity) // 用於儲存雙向佇列元素的陣列
|
||||
private var front = 0 // 佇列首指標,指向佇列首元素
|
||||
private var queSize = 0 // 雙向佇列長度
|
||||
private var nums: IntArray = IntArray(capacity) // 用於儲存雙向佇列元素的陣列
|
||||
private var front: Int = 0 // 佇列首指標,指向佇列首元素
|
||||
private var queSize: Int = 0 // 雙向佇列長度
|
||||
|
||||
/* 獲取雙向佇列的容量 */
|
||||
fun capacity(): Int {
|
||||
@@ -71,7 +72,7 @@ class ArrayDeque(capacity: Int) {
|
||||
return num
|
||||
}
|
||||
|
||||
/* 訪問佇列尾元素 */
|
||||
/* 佇列尾出列 */
|
||||
fun popLast(): Int {
|
||||
val num = peekLast()
|
||||
queSize--
|
||||
|
||||
@@ -8,9 +8,9 @@ package chapter_stack_and_queue
|
||||
|
||||
/* 基於環形陣列實現的佇列 */
|
||||
class ArrayQueue(capacity: Int) {
|
||||
private val nums = IntArray(capacity) // 用於儲存佇列元素的陣列
|
||||
private var front = 0 // 佇列首指標,指向佇列首元素
|
||||
private var queSize = 0 // 佇列長度
|
||||
private val nums: IntArray = IntArray(capacity) // 用於儲存佇列元素的陣列
|
||||
private var front: Int = 0 // 佇列首指標,指向佇列首元素
|
||||
private var queSize: Int = 0 // 佇列長度
|
||||
|
||||
/* 獲取佇列的容量 */
|
||||
fun capacity(): Int {
|
||||
|
||||
@@ -9,7 +9,7 @@ package chapter_stack_and_queue
|
||||
/* 基於陣列實現的堆疊 */
|
||||
class ArrayStack {
|
||||
// 初始化串列(動態陣列)
|
||||
private val stack = ArrayList<Int>()
|
||||
private val stack = mutableListOf<Int>()
|
||||
|
||||
/* 獲取堆疊的長度 */
|
||||
fun size(): Int {
|
||||
@@ -40,7 +40,7 @@ class ArrayStack {
|
||||
|
||||
/* 將 List 轉化為 Array 並返回 */
|
||||
fun toArray(): Array<Any> {
|
||||
return stack.toArray()
|
||||
return stack.toTypedArray()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ fun main() {
|
||||
|
||||
/* 元素出堆疊 */
|
||||
val pop = stack.pop()
|
||||
println("出堆疊元素 pop = ${pop},出堆疊後 stack = ${stack.toArray().contentToString()}")
|
||||
println("出堆疊元素 pop = $pop,出堆疊後 stack = ${stack.toArray().contentToString()}")
|
||||
|
||||
/* 獲取堆疊的長度 */
|
||||
val size = stack.size()
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
package chapter_stack_and_queue
|
||||
|
||||
/* 雙向鏈結串列節點 */
|
||||
class ListNode(var value: Int) {
|
||||
class ListNode(var _val: Int) {
|
||||
// 節點值
|
||||
var next: ListNode? = null // 後繼節點引用
|
||||
var prev: ListNode? = null // 前驅節點引用
|
||||
@@ -15,9 +15,9 @@ class ListNode(var value: Int) {
|
||||
|
||||
/* 基於雙向鏈結串列實現的雙向佇列 */
|
||||
class LinkedListDeque {
|
||||
private var front: ListNode? = null // 頭節點 front ,尾節點 rear
|
||||
private var rear: ListNode? = null
|
||||
private var queSize = 0 // 雙向佇列的長度
|
||||
private var front: ListNode? = null // 頭節點 front
|
||||
private var rear: ListNode? = null // 尾節點 rear
|
||||
private var queSize: Int = 0 // 雙向佇列的長度
|
||||
|
||||
/* 獲取雙向佇列的長度 */
|
||||
fun size(): Int {
|
||||
@@ -64,12 +64,12 @@ class LinkedListDeque {
|
||||
|
||||
/* 出列操作 */
|
||||
fun pop(isFront: Boolean): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
|
||||
val value: Int
|
||||
if (isEmpty())
|
||||
throw IndexOutOfBoundsException()
|
||||
val _val: Int
|
||||
// 佇列首出列操作
|
||||
if (isFront) {
|
||||
value = front!!.value // 暫存頭節點值
|
||||
_val = front!!._val // 暫存頭節點值
|
||||
// 刪除頭節點
|
||||
val fNext = front!!.next
|
||||
if (fNext != null) {
|
||||
@@ -79,7 +79,7 @@ class LinkedListDeque {
|
||||
front = fNext // 更新頭節點
|
||||
// 佇列尾出列操作
|
||||
} else {
|
||||
value = rear!!.value // 暫存尾節點值
|
||||
_val = rear!!._val // 暫存尾節點值
|
||||
// 刪除尾節點
|
||||
val rPrev = rear!!.prev
|
||||
if (rPrev != null) {
|
||||
@@ -89,7 +89,7 @@ class LinkedListDeque {
|
||||
rear = rPrev // 更新尾節點
|
||||
}
|
||||
queSize-- // 更新佇列長度
|
||||
return value
|
||||
return _val
|
||||
}
|
||||
|
||||
/* 佇列首出列 */
|
||||
@@ -104,17 +104,14 @@ class LinkedListDeque {
|
||||
|
||||
/* 訪問佇列首元素 */
|
||||
fun peekFirst(): Int {
|
||||
if (isEmpty()) {
|
||||
throw IndexOutOfBoundsException()
|
||||
|
||||
}
|
||||
return front!!.value
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return front!!._val
|
||||
}
|
||||
|
||||
/* 訪問佇列尾元素 */
|
||||
fun peekLast(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return rear!!.value
|
||||
return rear!!._val
|
||||
}
|
||||
|
||||
/* 返回陣列用於列印 */
|
||||
@@ -122,7 +119,7 @@ class LinkedListDeque {
|
||||
var node = front
|
||||
val res = IntArray(size())
|
||||
for (i in res.indices) {
|
||||
res[i] = node!!.value
|
||||
res[i] = node!!._val
|
||||
node = node.next
|
||||
}
|
||||
return res
|
||||
|
||||
@@ -52,7 +52,7 @@ class LinkedListQueue(
|
||||
/* 訪問佇列首元素 */
|
||||
fun peek(): Int {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return front!!.value
|
||||
return front!!._val
|
||||
}
|
||||
|
||||
/* 將鏈結串列轉化為 Array 並返回 */
|
||||
@@ -60,7 +60,7 @@ class LinkedListQueue(
|
||||
var node = front
|
||||
val res = IntArray(size())
|
||||
for (i in res.indices) {
|
||||
res[i] = node!!.value
|
||||
res[i] = node!!._val
|
||||
node = node.next
|
||||
}
|
||||
return res
|
||||
@@ -95,4 +95,4 @@ fun main() {
|
||||
/* 判斷佇列是否為空 */
|
||||
val isEmpty = queue.isEmpty()
|
||||
println("佇列是否為空 = $isEmpty")
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ class LinkedListStack(
|
||||
/* 訪問堆疊頂元素 */
|
||||
fun peek(): Int? {
|
||||
if (isEmpty()) throw IndexOutOfBoundsException()
|
||||
return stackPeek?.value
|
||||
return stackPeek?._val
|
||||
}
|
||||
|
||||
/* 將 List 轉化為 Array 並返回 */
|
||||
@@ -49,7 +49,7 @@ class LinkedListStack(
|
||||
var node = stackPeek
|
||||
val res = IntArray(size())
|
||||
for (i in res.size - 1 downTo 0) {
|
||||
res[i] = node?.value!!
|
||||
res[i] = node?._val!!
|
||||
node = node.next
|
||||
}
|
||||
return res
|
||||
|
||||
Reference in New Issue
Block a user